gpt4 book ai didi

java - 如何同时运行 Swing.timer 而不会出现延迟或性能下降

转载 作者:行者123 更新时间:2023-12-01 23:05:07 25 4
gpt4 key购买 nike

我们如何同时运行多个 Swing.timer?

我的应用程序是当我点击 jframe 按钮时,它将执行两个 Swing 计时器并同时运行,但似乎此操作会使我的应用程序运行非常缓慢且滞后。

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

//************first swing timer execution************************

ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {

jLabel1.setText("Hello Girls");

}
};

javax.swing.Timer timer = new javax.swing.Timer( 0 , taskPerformer);
timer.setRepeats(true);
timer.start(); // execute first swing timer


//************second swing timer execution************************

ActionListener taskPerformer1 = new ActionListener() {
public void actionPerformed(ActionEvent evt) {

jLabel1.setText("Hello Guys");

}
};

javax.swing.Timer timer1 = new javax.swing.Timer( 0 , taskPerformer1);
timer1.setRepeats(true);
timer1.start(); //execute second swing timer
}

我注意到,如果我运行两个计时器,它会卡住我的应用程序。我怎样才能运行两个计时器而不卡住或滞后?

最佳答案

0 毫秒的延迟可能会导致 EDT 中充满 Action 事件(以及由 setText 调用生成的其他更新事件)。

您需要为 EDT 提供喘息空间,以便不仅可以处理您的 Action 事件,还可以处理这些事件的后果,例如

Timer

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TimerExample {

public static void main(String[] args) {
new TimerExample();
}

public TimerExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

private JLabel label;
private JButton button;

private Timer hisTimer;
private Timer herTimer;

public static final int DELAY = 1000;

public TestPane() {
setLayout(new BorderLayout());
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
button = new JButton("Start");

hisTimer = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("His");
}
});
hisTimer.setInitialDelay(DELAY / 2);
hisTimer.setCoalesce(true);

herTimer = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Hers");
}
});
herTimer.setCoalesce(true);

button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (hisTimer.isRunning() && herTimer.isRunning()) {
hisTimer.stop();
herTimer.stop();
button.setText("Start");
} else {
hisTimer.start();
herTimer.start();
button.setText("Stop");
}
}
});

add(button, BorderLayout.SOUTH);
add(label);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

}

}

关于java - 如何同时运行 Swing.timer 而不会出现延迟或性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22906131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com