import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JProgressBarDemo extends JFrame
{
private JProgressBar progressBar;
private JButton stopButton,startButton;
private Timer timer;
public JProgressBarDemo()
{
super("进度条");
setSize(300,200);
//获取内容面板;
Container container=getContentPane();
container.setBackground(Color.YELLOW);
container.setLayout(new FlowLayout(FlowLayout.CENTER));
//try
// {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }catch(Exception e){}
//设置面板的背景颜色和布局;
progressBar=new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setBorderPainted(true);
progressBar.setPreferredSize(new Dimension(250,30));
progressBar.setForeground(Color.GREEN);
progressBar.setBackground(Color.WHITE);
container.add(progressBar);
//设置按钮
startButton=new JButton("开始");
stopButton=new JButton("暂停");
startButton.setBackground(Color.WHITE);
stopButton.setBackground(Color.WHITE);
startButton.setFont(new Font("seriy",Font.PLAIN,14));
stopButton.setFont(new Font("seriy",Font.PLAIN,14));
stopButton.setEnabled(false);
TimerHandler handler=new TimerHandler();
startButton.addActionListener(handler);
stopButton.addActionListener(handler);
container.add(startButton);
container.add(stopButton);
Timer timer=new Timer(50,handler);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args)
{
JProgressBarDemo application=new JProgressBarDemo();
}
class TimerHandler implements ActionListener
{
private int value= 0;
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==timer)
{
value=progressBar.getValue();
if(value < 100)
{
value++;
progressBar.setValue(value);
}
else
{
timer.stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
else if(event.getSource()==startButton)
{
if(progressBar.getValue()>=100)
progressBar.setValue(0);
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
else if(event.getActionCommand().equals("暂停"))
{
timer.stop();
stopButton.setText("重启动");
}
else if(event.getActionCommand().equals("重启动"))
{
timer.restart();
stopButton.setText("暂停");
}
}
}
}
import java.awt.*;
import java.awt.event.*;
class JProgressBarDemo extends JFrame
{
private JProgressBar progressBar;
private JButton stopButton,startButton;
private Timer timer;
public JProgressBarDemo()
{
super("进度条");
setSize(300,200);
//获取内容面板;
Container container=getContentPane();
container.setBackground(Color.YELLOW);
container.setLayout(new FlowLayout(FlowLayout.CENTER));
//try
// {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }catch(Exception e){}
//设置面板的背景颜色和布局;
progressBar=new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setBorderPainted(true);
progressBar.setPreferredSize(new Dimension(250,30));
progressBar.setForeground(Color.GREEN);
progressBar.setBackground(Color.WHITE);
container.add(progressBar);
//设置按钮
startButton=new JButton("开始");
stopButton=new JButton("暂停");
startButton.setBackground(Color.WHITE);
stopButton.setBackground(Color.WHITE);
startButton.setFont(new Font("seriy",Font.PLAIN,14));
stopButton.setFont(new Font("seriy",Font.PLAIN,14));
stopButton.setEnabled(false);
TimerHandler handler=new TimerHandler();
startButton.addActionListener(handler);
stopButton.addActionListener(handler);
container.add(startButton);
container.add(stopButton);
Timer timer=new Timer(50,handler);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args)
{
JProgressBarDemo application=new JProgressBarDemo();
}
class TimerHandler implements ActionListener
{
private int value= 0;
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==timer)
{
value=progressBar.getValue();
if(value < 100)
{
value++;
progressBar.setValue(value);
}
else
{
timer.stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
else if(event.getSource()==startButton)
{
if(progressBar.getValue()>=100)
progressBar.setValue(0);
timer.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
else if(event.getActionCommand().equals("暂停"))
{
timer.stop();
stopButton.setText("重启动");
}
else if(event.getActionCommand().equals("重启动"))
{
timer.restart();
stopButton.setText("暂停");
}
}
}
}
解决方案 »
- jpanel里怎样放置100*100的格子问题
- 如何利用mediautil來將經緯度值寫入圖片的eixf中
- 为什么运行后就提示这样的错误:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- 求定时器和多线程融合的解决方法?
- 求 判断输入日期是否大于本月最后一天的方法
- 请各位老大,指点,救命2个问题,高分回报
- 救命啊!!哪儿可以找到IBM JDK1.4啊
- 高分求CoreJava卷I中两程序的源代码!
- Java的DOM指的是什么?
- java awt 图片拖动
- 各位帮帮忙,有个java程序不知哪错了
- eclipse通过编译但是没有输出结果(I/O类)
请帮忙解决?谢谢!
上面定义了timer变量:
private Timer timer;
后面又创建了个Timer变量:
Timer timer=new Timer(50,handler);
重复了啊 ~~
不过还是谢谢2楼~~