JFrame 组件之间的触发延迟 [英] Firing delay between JFrame components
问题描述
我想展示如何使用 JFrame
直观地执行归并排序.我想要做的是在一些时间延迟后使后续 JLabel
可见.我尝试了很多方法,但所有方法都在同一时刻出现,没有中间延迟.
I want to show how merge sort perform visually using JFrame
. What I want to do is to make visible subsequent JLabel
with some time delay. I tried many way but all of them appears at same moment with no intermediate delay.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// jLabel1.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jLabel4.setVisible(false);
jLabel5.setVisible(false);
jLabel6.setVisible(false);
jLabel7.setVisible(false);
final Timer t=new Timer((4000), null);
final int delay=2000;
final ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jLabel1.setVisible(true);
t.getDelay();
jLabel2.setVisible(true);
t.setDelay(3000);
jLabel3.setVisible(true);
t.setDelay(2000);
jLabel4.setVisible(true);
t.setDelay(2000);
jLabel5.setVisible(true);
t.setDelay(2000);
jLabel6.setVisible(true);
t.setDelay(2000);
}
};
new Timer(delay, taskPerformer).start();
但是当我点击按钮时,尽管我一直拖延,但所有标签都出现在同一个时刻.
But when I click button all the lables appear at same momenet though I have kept delay.
推荐答案
这不起作用的原因有很多.首先,javax.swing.Timer
不是这样工作的.它在后台等待,直到给定的延迟过去,然后调用注册的 ActionListener
的 actionPerformed
方法.
There are a number of reasons why this won't work. Firstly, javax.swing.Timer
doesn't work this way. It waits in the background until the given delay has past and then calls the registered ActionListener
s actionPerformed
method.
其次,如果它确实以这种方式工作,它将阻塞事件调度线程,阻止它处理重绘请求.
Secondly, if it did work this way, it would block the Event Dispatching Thread, preventing it from processing repaint requests.
我想您会找到如何使用 Swing 计时器使用.
public class BlinkOut {
public static void main(String[] args) {
new BlinkOut();
}
public BlinkOut() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel[] labels;
private int[] delays;
private Timer timer;
private int index;
public TestPane() {
setLayout(new GridLayout(0, 1));
labels = new JLabel[7];
for (int index = 0; index < 7; index++) {
labels[index] = new JLabel("Label " + (index + 1));
add(labels[index]);
}
delays = new int[] {2000, 3000, 2000, 2000, 2000, 2000, 2000};
JButton hide = new JButton("Hide");
add(hide);
hide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Click");
index = 0;
labels[index].setVisible(false);
timer.setDelay(delays[index]);
timer.start();
}
});
timer = new Timer(delays[0], new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Tick");
timer.stop();
index++;
if (index < 7) {
labels[index].setVisible(false);
timer.setDelay(delays[index]);
timer.start();
}
}
});
timer.setRepeats(false);
timer.setCoalesce(true);
}
}
}
这篇关于JFrame 组件之间的触发延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!