📄 test.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet {
private JProgressBar pb = new JProgressBar();
public void init() {
Container contentPane = getContentPane();
final JButton startButton = new JButton("start");
contentPane.setLayout(new FlowLayout());
contentPane.add(startButton);
contentPane.add(pb);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
(new GetInfoThread(pb)).start();
}
});
pb.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int min = pb.getMinimum(), max = pb.getMaximum();
int value = pb.getValue();
showStatus("Min: " + min + ", Max: " + max +
", Value: " + value);
}
});
}
}
class GetInfoThread extends Thread {
Runnable update, finish;
JProgressBar pb;
int value, min, max, increment;
public GetInfoThread(JProgressBar progressBar) {
pb = progressBar;
max = pb.getMaximum();
min = pb.getMinimum();
update = new Runnable() {
public void run() {
value = pb.getValue() + increment;
pb.setValue(value);
}
};
finish = new Runnable() {
public void run() {
value = min;
pb.setValue(value);
}
};
}
public void run() {
while(value + increment <= max) {
simulateTimeConsumingActivity();
SwingUtilities.invokeLater(update);
}
SwingUtilities.invokeLater(finish);
}
private void simulateTimeConsumingActivity() {
try {
Thread.currentThread().sleep(1000);
increment = (max - min) / 10;
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -