[英]How to run Swing.timer concurrently without lag or slow performance
我們如何同時運行多個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
事件,而且還處理這些事件的后果,例如
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.