问题
https://blog.youkuaiyun.com/weixin_45491054/article/details/105848467?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-5
解决大佬在此篇博文中提出的问题
代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;;
public class test04
{
public static void main(String[] args)
{
new viewer();
}
}
class viewer extends JFrame implements ActionListener,Runnable
{
JProgressBar process;
JButton jb1;
static boolean flag=true;
public viewer()
{
super("下载进度条");
this.setBounds(0, 0, 400,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
process =new JProgressBar(0, 100);
process.setStringPainted(true);
this.add(process);
jb1=new JButton("开始下载");
this.add(jb1);
jb1.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
new Thread(this).start();
if(process.getValue()==process.getMaximum())
{
process.setValue(0);
}
}
public void run()
{
flag=!flag;
if(!flag)
{
jb1.setText("暂停下载");
}
else
jb1.setText("开始下载");
while (process.getValue() < process.getMaximum())
{
if (flag)
{
try
{
Thread.currentThread().sleep(100);
process.setValue(process.getValue() + 1);
}
catch (Exception e) {}
}
else
process.setValue(process.getValue());
}
jb1.setText("重新下载");
}
}
结果
思路提示
巧用接口来实现多重继承