简体   繁体   中英

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. 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:

  • The Jframe completely stops responding to left and right mouse clicks (it makes a windows ding sound when you try to click)
  • The JFrame still responds to mouse scrolls and keyboard inputs
  • The JFrame vanishes from the alt-tab program list.
  • NO error message or stack trace.
  • Using souts it appears that the code reaches all points of necessary code properly
  • I can't even click the "X" Window button or right-click close on the task bar
  • The 3rd turn object is structurally identical to previous turn objects

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 .

Addendum: How uncaught exceptions are handled and Uncaught exceptions in GUI applications may be helpful. 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. I spent couple of hours searching for exception but the responce was pretty trivial: I have implemented interface with such method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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