有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在3秒钟后在侦听器中处理一个帧

我想在键入密钥3秒钟后处理帧。 这是我的密码:

frame.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {

                    Timer t = new Timer(3000, null);
                    t.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {

                            System.out.println("test");
                            frame.dispose();

                        }
                    });

                    t.start();
             }
      }

我可以从控制台看到打印的字符串,但框架没有关闭。 我见过一个类似的线程,使用计时器似乎是解决方案,但它不适合我


共 (2) 个答案

  1. # 1 楼答案

    frame.dispose()不保证立即执行。我发现先调用frame.setVisible(false)有助于加快处理过程

    编辑

    此外,您可能希望使用Key Bindings而不是键侦听器来触发事件。关键侦听器很复杂,通常不是很有用(它们需要关注与您交互的项目,它们倾向于使用事件,所以您看不到它们)

    编辑2

    进一步检查代码后,问题似乎是您需要将计时器设置为不重复(在调用start之前):

    t.setRepeats(false);
    

    这个示例对我很有用-如果您仍然遇到问题,请告诉我(如果是,请发布您遇到的问题的可运行示例-我只能猜测可能导致问题的任何其他代码):

    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class QuickTest {
    
        public QuickTest(){
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setSize(400, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
            frame.addKeyListener(new KeyAdapter() {
    
                @Override
                public void keyTyped(KeyEvent e) {
    
                    Timer t = new Timer(3000, null);
                    t.addActionListener(new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
    
                            System.out.println("test");
                            frame.dispose();
    
                        }
                    });
                    t.setRepeats(false);
                    t.start();
                }
            });     
        }
    
        public static void main(String[] args){
            new QuickTest();
        }
    }
    
  2. # 2 楼答案

    对我来说似乎很好

    确保setDefaultCloseOperation没有设置为DO_NOTHING,否则不执行任何操作

    public class TestCloseFrame {
    
        public static void main(String[] args) {
            new TestCloseFrame();
        }
    
        public TestCloseFrame() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
    
                    JButton close = new JButton("Close");
                    close.addActionListener(new CloseAction(frame, close));
    
                    frame.add(close);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class CloseAction implements ActionListener {
    
            private JButton button;
            private JFrame frame;
            private int count = 0;
    
            public CloseAction(JFrame frame, JButton button) {
                this.button = button;
                this.frame = frame;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                button.setEnabled(false);
                Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        count++;
                        button.setText(Integer.toString(4 - count));
                        if (count > 3) {
                            frame.dispose();
                            Timer timer = (Timer) e.getSource();
                            timer.stop();
                        }
                    }
                });
                timer.setInitialDelay(0);
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
        }
    }