【问题标题】:Using Sleep method inside a For loop (on hold)在 For 循环中使用 Sleep 方法(暂停)
【发布时间】:2013-11-25 01:59:17
【问题描述】:

我正在开发一个在 jTable 中打印 100 个数字的程序。此外,还会有一个 if 语句来验证结果,并将根据打印的值将 jPanel 设置为特定颜色。我需要稍微慢一点打印这些值,并确保 jPanel 根据每个值更改它的颜色。我尝试了以下代码,但似乎有错误:

try{ 
  int n = 100;      
  int m = 1513;
  int a = 19713;
  double x = 177963;
  int c = 1397; 
  double r;     
  int i;

  Object[] res = new Object[n]; 

  for(i=0;i< n;i++){

    r = (a*x+c)%m;
    x = r;
    r = r/m;
    res[i] = r;
    Thread.sleep(1000);

    if(r>=0.3){
      jPanel3.setBackground(Color.green);                           
    }else{
      jPanel3.setBackground(Color.red);
    }
  }

  DefaultTableModel dtm = new DefaultTableModel();

  dtm.addColumn("Results", res);
  // dtm.addColumn("resultado2", res);
  jTable1.setModel(dtm);
}catch(Exception e){
  Thread.currentThread().interrupt();
}

【问题讨论】:

  • 不要使用sleep。用户摇摆Timer
  • 你太懒了,不仅没有从上一个问题中学到任何东西,而且它已被关闭,你似乎已经逐字转发了它,包括标题中的“(搁置)” ?!

标签: java swing concurrency event-dispatch-thread


【解决方案1】:

但似乎有错误...

什么错误?

请注意,您永远不想在 Swing 应用程序的事件线程中调用 Thread.sleep(...)。改用摇摆计时器。

【讨论】:

    【解决方案2】:

    Swing 是一个单线程框架。事件调度线程负责处理重绘请求等。任何阻止 EDT 运行的操作都会阻止它处理任何重绘请求和其他事件,从而使您的应用程序看起来像是挂起......

    您还需要确保对 UI 的所有更新都是在 EDT 的上下文中进行的。

    在您的情况下,您正在执行一个循环并使用 Thread.sleep,这是处理 Swing 时的两个大不,不...

    看看Concurrency In Swing

    正如您在duplicate question 中提到的,您应该使用javax.swing.Timer

    这意味着您将不得不修改循环条件才能工作,例如...

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class ColorPane {
    
        public static void main(String[] args) {
            new ColorPane();
        }
    
        public ColorPane() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE};
            private int colorIndex = -1;
    
            public TestPane() {
                Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        colorIndex++;
                        if (colorIndex >= colors.length) {
                            colorIndex = 0;
                        }
                        setBackground(colors[colorIndex]);
                    }
                });
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多