在操作后显示进度条
Show progress bar after an action
我想在一些操作后显示一个进度条。
实际代码很复杂,但我准备了一个示例
我希望进度条填满框架的宽度,但我希望它具有 specific/normal 高度而不填满框架。我怎样才能做到这一点?
如果框架的布局没有改变 (BorderLayout
) 会更好,因为它可能会破坏我的实际代码中的其他内容。如果这无法避免,那么一定要改变它,我会看看结果如何。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class SwingControlDemo
{
public static int progress = 0;
private JFrame mainFrame;
public SwingControlDemo()
{
prepareGUI();
}
public static void main(String[] args)
throws Exception
{
//BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
// org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
new SwingControlDemo();
}
private void prepareGUI()
{
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new BorderLayout());
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
showProgressBarDemo();
}
});
mainFrame.add(new JLabel("mplaaaaaaaaaaaaaaaaaaa"), BorderLayout.CENTER);
mainFrame.add(startButton, BorderLayout.SOUTH);
mainFrame.setVisible(true);
}
private void showProgressBarDemo()
{
final JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BorderLayout());
controlPanel.add(progressBar, BorderLayout.CENTER);
mainFrame.add(controlPanel);
mainFrame.revalidate();
mainFrame.repaint();
final Timer timer = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
progress++;
System.out.println(String.format("Completed %d%% of task.\n", progress));
progressBar.setValue(progress);
progressBar.setString(String.format("Completed %d%% of task.\n", progress));
}
});
timer.start();
}
}
but I want it to have a specific/normal height
如果您希望它在其 preferredSize
处显示,而不是将其添加到使用 FlowLayout 的 JPanel,然后将该 JPanel 添加到您的 BorderLayout-using JPanel(如果您仍想保留 BorderLayout ).请注意,我会使用 CardLayout 交换组件。
The actual code is complex, but I've prepared a sample I want the progress bar to fill the width of the frame, but I want it to have a specific/normal height and not fill the frame.
您可以使用 GridBagLayout
,例如
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
controlPanel.add(progressBar, gbc);
有关详细信息,请参阅 How to Use GridBagLayout
您可以考虑利用框架的 "glass pane",这样您就可以将内容放在 contentPane
的顶部,但不会直接影响它。如果你向它注册了一个 MouseListener
,你甚至可以阻止鼠标事件到达 contentPane
本身。
查看 How to Use Root Panes 了解更多详情
此外,根据您的需要,您或许可以使用 ProgressMonitor
、for example
我想在一些操作后显示一个进度条。
实际代码很复杂,但我准备了一个示例
我希望进度条填满框架的宽度,但我希望它具有 specific/normal 高度而不填满框架。我怎样才能做到这一点?
如果框架的布局没有改变 (BorderLayout
) 会更好,因为它可能会破坏我的实际代码中的其他内容。如果这无法避免,那么一定要改变它,我会看看结果如何。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class SwingControlDemo
{
public static int progress = 0;
private JFrame mainFrame;
public SwingControlDemo()
{
prepareGUI();
}
public static void main(String[] args)
throws Exception
{
//BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
// org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
new SwingControlDemo();
}
private void prepareGUI()
{
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new BorderLayout());
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
showProgressBarDemo();
}
});
mainFrame.add(new JLabel("mplaaaaaaaaaaaaaaaaaaa"), BorderLayout.CENTER);
mainFrame.add(startButton, BorderLayout.SOUTH);
mainFrame.setVisible(true);
}
private void showProgressBarDemo()
{
final JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BorderLayout());
controlPanel.add(progressBar, BorderLayout.CENTER);
mainFrame.add(controlPanel);
mainFrame.revalidate();
mainFrame.repaint();
final Timer timer = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
progress++;
System.out.println(String.format("Completed %d%% of task.\n", progress));
progressBar.setValue(progress);
progressBar.setString(String.format("Completed %d%% of task.\n", progress));
}
});
timer.start();
}
}
but I want it to have a specific/normal height
如果您希望它在其 preferredSize
处显示,而不是将其添加到使用 FlowLayout 的 JPanel,然后将该 JPanel 添加到您的 BorderLayout-using JPanel(如果您仍想保留 BorderLayout ).请注意,我会使用 CardLayout 交换组件。
The actual code is complex, but I've prepared a sample I want the progress bar to fill the width of the frame, but I want it to have a specific/normal height and not fill the frame.
您可以使用 GridBagLayout
,例如
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
controlPanel.add(progressBar, gbc);
有关详细信息,请参阅 How to Use GridBagLayout
您可以考虑利用框架的 "glass pane",这样您就可以将内容放在 contentPane
的顶部,但不会直接影响它。如果你向它注册了一个 MouseListener
,你甚至可以阻止鼠标事件到达 contentPane
本身。
查看 How to Use Root Panes 了解更多详情
此外,根据您的需要,您或许可以使用 ProgressMonitor
、for example