简体   繁体   English

Java Swing JFrame突然停止响应鼠标输入,但仍然需要键盘输入

[英]Java Swing JFrame suddenly stops responding to mouse input, but still takes keyboard inputs

I have a game that uses a JFrame that displays the game info. 我有一个游戏,它使用一个显示游戏信息的JFrame。 The window updates whenever a player sends a move object to the server. 只要玩家将移动对象发送到服务器,窗口就会更新。 It works perfectly fine for any number of move objects. 它适用于任何数量的移动对象。 However once the 3nd turn starts it hits a wall and here is what happens: 然而,一旦第3次转弯开始它撞到一堵墙,这就是发生的事情:

  • The Jframe completely stops responding to left and right mouse clicks (it makes a windows ding sound when you try to click) Jframe完全停止响应鼠标左键和右键(当您尝试单击时,它会发出窗口响声)
  • The JFrame still responds to mouse scrolls and keyboard inputs JFrame仍然响应鼠标滚动和键盘输入
  • The JFrame vanishes from the alt-tab program list. JFrame从alt-tab程序列表中消失。
  • NO error message or stack trace. 没有错误消息或堆栈跟踪。
  • Using souts it appears that the code reaches all points of necessary code properly 使用souts似乎代码正确地到达了所有必要代码点
  • I can't even click the "X" Window button or right-click close on the task bar 我甚至无法单击“X”窗口按钮或右键单击任务栏上的关闭
  • The 3rd turn object is structurally identical to previous turn objects 第3转弯对象在结构上与先前的转弯对象相同

what on earth can cause a program to do this?? 究竟是什么导致程序这样做?

The event dispatch thread has thrown an exception. 事件派发线程抛出异常。 It is automatically restarted, but your program remains in the state you describe. 它会自动重新启动,但您的程序仍处于您描述的状态。 See also How can I catch Event Dispatch Thread (EDT) exceptions and this answer . 另请参见如何捕获事件调度线程(EDT)异常和此答案

Addendum: How uncaught exceptions are handled and Uncaught exceptions in GUI applications may be helpful. 附录: 如何处理 未捕获的异常,GUI应用程序中的未捕获异常可能会有所帮助。 Also check for empty exception handlers. 还要检查空的异常处理程序。

Addendum: Here's an example. 附录:这是一个例子。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/** @see https://stackoverflow.com/a/9935287/230513 */
public class Fail extends JPanel {

    private static final JLabel label = new JLabel(
        "12345678901234567890", JLabel.CENTER);

    public Fail() {
        this.setLayout(new GridLayout(0, 1));
        this.add(label);
        this.add(new JButton(new AbstractAction("Kill me, now!") {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b = (JButton) e.getSource();
                b.setText(String.valueOf(1 / 0));
            }
        }));
        new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(String.valueOf(System.nanoTime()));
            }
        }).start();
    }

    private void display() {
        JFrame f = new JFrame("Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Fail().display();
            }
        });
    }
}

Check if your frame class do not overrides isEnabled() method. 检查您的帧类是否不重写isEnabled()方法。 I spent couple of hours searching for exception but the responce was pretty trivial: I have implemented interface with such method. 我花了几个小时搜索异常,但响应非常简单:我用这种方法实现了接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM