【问题标题】:How to disable the system for a few seconds in java JPanel如何在java JPanel中禁用系统几秒钟
【发布时间】:2021-11-05 18:32:28
【问题描述】:

我是图形新手,我尝试禁用代码几秒钟(没有sleep()方法

public class MainClass
{ 
    public static void main(String args[]) 
    { 
        JPanel p = new JPanel();
        JFrame f = new JFrame("wait");
        f.setSize(600,600);
        f.setVisible(true);
        JLabel l = new JLabel("GET READY");
        p.add(l);
        l.setBounds(0,0,200,200);
        
        // wait here
        
        for(int i = 0;i<2;i++) {
            l.setText(String.valueOf(i));
            // wait here again:)
            f.repaint();
        }
       
        f.add(p);
    } 
} 

【问题讨论】:

    标签: java swing graphics jframe jpanel


    【解决方案1】:

    背景,TL;DR

    Swing 是单线程的,不是线程安全的。所以使用 Thread.sleep 或循环之类的东西是行不通的。

    在大多数情况下,最好的解决方案是使用 Swing Timer,有关详细信息,请参阅 How to Use Swing Timers

    Timer 是引入回调以在未来某个时间发生的好方法,它们可用于运行一次或无限次(直到您停止它)。

    如果您需要等待某些操作完成,它会变得有点复杂。在这些情况下,您应该考虑使用Worker Threads and SwingWorker,它可以更轻松地从事件调度线程执行功能(有关更多详细信息,请参阅The Event Dispatch Thread),但提供了将信息重新同步回 EDT 的功能。

    Swing Timer 示例

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    
        public class TestPane extends JPanel {
    
            private JLabel label;
            private Timer timer;
    
            public TestPane() {
                setLayout(new GridBagLayout());
                label = new JLabel("Please wait");
                add(label);
            }
    
            @Override
            public void addNotify() {
                super.addNotify();
                // This is all just so we can allow the UI to stablise
                // on the screen
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        startWaiting();
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void startWaiting() {
                if (timer != null && timer.isRunning()) {
                    return;
                }
                timer = new Timer(10000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        timer = null;
                        label.setText("Thanks for waiting");
                    }
                });
                timer.setRepeats(false);
                timer.start();
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2012-10-03
      • 2015-12-29
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多