2024-06-08 12:20:00 | 我爱编程网
你的 action 事件可以放在一个新线程中运行,不要阻挡 Swing 画图。
Swing 对事件的响应都是放在一个叫事件派发的线程中去做的,如果你的事件响应监听器本身在被事件派发线程调用时你做的事件太花时间了(比如我们来个无限循环啥的)我们就应该把这个 actionPerformed 方法中的事情放在一个新开的线程中去做:
public void actionPerformed(final ActionEvent e) {
new Thread() {
public void run() { // 你的事情都放在这里处理 }
}.start() ; // 开启一个新线程来处理,它将不再阻挡 Swing 事件派发线程去绘图。
}
// 回答完毕,采纳即可
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Bar extends JPanel implements PropertyChangeListener
{
private static final long serialVersionUID = 1L;
private JProgressBar progressBar;
private Task task;
public Bar()
{
super(new BorderLayout());
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
JPanel panel = new JPanel();
panel.add(progressBar);
add(panel, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
class Task extends SwingWorker
{
@Override
public Void doInBackground()
{
Random random = new Random();
int progress = 0;
setProgress(0);
while(progress < 100)
{
try
{
Thread.sleep(random.nextInt(1000));
}
catch(InterruptedException ignore)
{}
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
@Override
public void done()
{
Toolkit.getDefaultToolkit().beep();
System.exit(0);我爱编程网
}
}
public void propertyChange(PropertyChangeEvent evt)
{
if("progress" == evt.getPropertyName())
{
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
private void start()
{
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("bar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Bar b = new Bar();
b.setOpaque(true);
b.start();
frame.setContentPane(b);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
package socket.fox.msgdeal;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JProgressBar extends Frame implements Runnable {
int x, y; // 进度倏起始位置
int rectWidth, rectHeight, width, height, num; // 进度格倏宽,进度格高,进度倏宽,进度倏高,格子数量
int x0, d, n; // 进度,进度格之间的距离,进度n%
Image bufImg;
Graphics gBuf;
public JProgressBar() {
x = 40;
y = 50;
x0 = x;
rectWidth = 6; // 每个进度格的宽
rectHeight = 10;
d = rectWidth / 3;
num = 50; // 显示五十个进度格
width = (rectWidth + d) * num;
height = rectHeight + 4; // 进度格的高
init();
bufImg = this.createImage(getWidth(), getHeight());
gBuf = bufImg.getGraphics();
paintBack();
repaint();
}
public void init() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setSize(this.x * 2 + width, this.y * 2 + height);
this.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
}
public static void main(String[] args) {
new Thread(new JProgressBar()).start();
}
public void paintBack() {
gBuf.drawRoundRect(x, y, width, height, 6, 6);
}
public void paintProgressRect() {
Color c = gBuf.getColor();
gBuf.setColor(new Color(46, 211, 49));
x0 = x;
for (int i = 0; i < n; i++) {
gBuf.fillRect(x0, y + 2, rectWidth, rectHeight);
x0 += rectWidth + d;
}
gBuf.setColor(c);
}
public void paint(Graphics g) {
g.drawImage(bufImg, 0, 0, this);
}
/**
* 设置进度倏进度
*
* @param n
* 进度百分比
*/
public void setPercent(int n) {
this.n = n;
}
public void run() {
boolean flag = true;
while (flag) {
try {
repaint();
Thread.sleep(500);
n++;
System.out.println(n);
if (n > num) {
n = num;
flag = false;
}
paintProgressRect();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2024-04-13 00:38:12
2024-03-31 15:14:06
2024-03-21 00:22:13
2024-02-10 22:15:59
2023-12-26 22:10:45
2023-10-06 13:00:52