如何在 java JPanel 中禁用系统几秒钟

How to disable the system for a few seconds in java JPanel

我是图形方面的新手,我尝试禁用代码几秒钟(没有 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);
    } 
} 

背景,长话短说;博士

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

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

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

如果你需要等待一些操作完成,它会变得有点复杂。在这些情况下,您应该考虑使用 Worker Threads and SwingWorker, which makes it easier to execute functionality off the Event Dispatching Thread (see The Event Dispatch Thread 了解更多详细信息),但提供了将信息重新同步回 EDT 的功能。

摆动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();
        }

    }
}