我想在某些操作后显示进度条。实际代码很复杂,但我准备了一个样例。
我希望进度条填充框架的宽度,但我希望它有特定/正常的高度并且不填充框架。我该如何做到这一点?
最好不要改变框架的布局(BorderLayout),因为这可能会导致我的实际代码出现问题。如果无法避免,则请更改,并查看结果。
我希望进度条填充框架的宽度,但我希望它有特定/正常的高度并且不填充框架。我该如何做到这一点?
最好不要改变框架的布局(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();
}
}