3秒后在听众内部放置一个框架 [英] Dispose a frame inside a listener after 3 secs

查看:155
本文介绍了3秒后在听众内部放置一个框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输入键3秒后处理框架. 这是我的代码:

I want to dispose the frame 3 seconds after I type a key. Here is my code:

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

我可以从控制台看到打印的字符串,但是框架没有关闭. 我见过类似的线程,使用Timer似乎是解决方案,但不适用于我.

I can see from the console the printed string but the frame is not closing. I've seen a similar thread and use the Timer seemed to be the solution but it's not working for me.

推荐答案

frame.dispose()不保证立即执行.我发现先致电frame.setVisible(false)有助于加快处置过程.

frame.dispose() isn't guarenteed to execute immediately. I've found calling frame.setVisible(false) first helps speed up the disposing process.

编辑

此外,您可能希望使用键绑定而不是用于触发事件的关键侦听器.关键侦听器很复杂,并且通常用处不大(它们需要关注与您交互的项目,它们倾向于消耗事件,因此您看不到它们).

Also, you might want to look at using Key Bindings instead of key listeners for triggering your event. Key Listeners are complicated and generally not very useful (they require focus on the item you're interacting with, they tend to consume events so you don't see them).

编辑2

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

After further examination of your code, the problem seems to be that you need to set the timer to not repeat (before you call start):

t.setRepeats(false);

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

This example works for me - let me know if you're still experiencing a problem (and if so, please post a runnable example of the problem you're experiencing - I can only guess at any additional code that could cause problems):

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

这篇关于3秒后在听众内部放置一个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆