Java JFrame组件之间的触发延迟

Java JFrame组件之间的触发延迟,java,swing,timer,jframe,delay,Java,Swing,Timer,Jframe,Delay,我想展示如何使用JFrame直观地执行合并排序。我要做的是使后续的JLabel可见,并带有一定的时间延迟。我尝试了很多方法,但它们都出现在同一时刻,没有中间的延迟 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // jLabel1.setVisible(false); jLabel2

我想展示如何使用
JFrame
直观地执行合并排序。我要做的是使后续的
JLabel
可见,并带有一定的时间延迟。我尝试了很多方法,但它们都出现在同一时刻,没有中间的延迟

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();

但当我点击按钮时,所有标签都会在同一时刻出现,尽管我一直在延迟。

您需要更新计时器动作侦听器中的图标,如图所示。您可以实现
图标
界面来渲染大小与元素比较值成比例的图标,如图所示

附录:你能说得具体一点吗


您希望以某种初始随机顺序对大小为
N
列表进行排序的中间步骤设置动画
Number
子类实现了
compariable
,因此
comparieto()
已经完成。可以使用
JLabel
GridLayout(1,0)
来显示值,每个都有一个
图标。显示如何创建具有比例大小的图标;您需要在间隔期间改变高度
[0,N)
&Mad演示如何按一定顺序设置图标显示的动画。

您需要更新计时器动作侦听器中的图标,如图所示。您可以实现
图标
界面来渲染与元素比较值成比例的图标,如图所示

附录:你能说得具体一点吗


您希望以某种初始随机顺序对大小为
N
列表进行排序的中间步骤设置动画。
Number
子类实现
Comparable
,因此
compareTo()
已经完成。a
GridLayout(1,0)
JLabel
每个都有一个
图标
可用于显示值。演示如何创建成比例大小的图标;您需要在间隔内改变高度
[0,N)
&Mad演示如何按一定顺序设置图标的显示动画。

这不起作用的原因有很多。首先,
javax.swing.Timer
不这样工作。它在后台等待给定的延迟过去,然后调用注册的
ActionListener
s
actionPerformed
方法

其次,如果它确实以这种方式工作,它将阻塞事件调度线程,阻止它处理重绘请求

我想你会觉得有用的

公共类闪烁{
公共静态void main(字符串[]args){
新的闪烁();
}
公众眨眼(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
专用JLabel[]标签;
私人int[]延迟;
私人定时器;
私有整数索引;
公共测试窗格(){
setLayout(新的GridLayout(0,1));
标签=新的JLabel[7];
对于(int-index=0;index<7;index++){
labels[index]=新的JLabel(“Label”+(index+1));
添加(标签[索引]);
}
延迟=新整数[]{2000,3000,2000,2000,2000,2000,2000,2000};
JButton hide=新JButton(“hide”);
添加(隐藏);
hide.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(“单击”);
指数=0;
标签[index].setVisible(false);
timer.setDelay(延迟[索引]);
timer.start();
}
});
计时器=新计时器(延迟[0],新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
系统输出打印项次(“勾选”);
timer.stop();
索引++;
如果(指数<7){
标签[index].setVisible(false);
timer.setDelay(延迟[索引]);
timer.start();
}
}
});
timer.setRepeats(假);
timer.setCoalesce(真);
}
}
}

这不起作用的原因有很多。首先,
javax.swing.Timer
不起作用。它在后台等待给定的延迟过去,然后调用注册的
ActionListener
s
actionPerformed
方法

其次,如果它确实以这种方式工作,它将阻塞事件调度线程,阻止它处理重绘请求

我想你会觉得有用的

公共类闪烁{
公共静态void main(字符串[]args){
新的闪烁();
}
公众眨眼(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaul
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);
        }
    }

}