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("暂停");
}
}
}
}
解决方案 »
- 这个acm试题,谁能帮我翻译一下!
- Swing怎么释放内存
- MyEclipse 我的Java 工程目录上有个X(插),但我的代码没问题。
- rmi服务端注销掉一个远程对象,客户端为什么还能访问这个远程对象的方法呢?
- 【大家好,我有几个关于创业的问题,希望有创业经验,或是现在在开公司的朋友们给一些建议?】
- jdbc可不可以直接连接数据库不通过数据库名 ????
- 原来可以的java3D小程序,现在怎么不可以了啊?急啊,在线等待!
- 怎么样让我的程序最小化时进入系统托盘(就像系统时间那个位置)?
- 编译JAVA的问题!·
- 已知树路径,怎么得到树的编号?
- 各位帮帮忙,有个java程序不知哪错了
- eclipse通过编译但是没有输出结果(I/O类)
请帮忙解决?谢谢!
上面定义了timer变量:
private Timer timer;
后面又创建了个Timer变量:
Timer timer=new Timer(50,handler);
重复了啊 ~~
不过还是谢谢2楼~~