简体   繁体   English

Java:MouseListener,KeyListener和PaintComponent的JFrame问题

[英]Java: JFrame problems with MouseListener, KeyListener, and PaintComponent

I have an issue in which my mouseListener is unresponsive. 我的mouseListener无法响应,这是一个问题。 I have tried to add a mouseListener to one object, a keyListener to another, and a paintComponent to another and then put all the objects onto the JFrame. 我试图将mouseListener添加到一个对象,将keyListener添加到另一个,并将paintComponent添加到另一个,然后将所有对象放到JFrame上。 My problem is that the mouseListener isn't responding but the keyListener and the paintComponent are. 我的问题是mouseListener没有响应,但是keyListener和paintComponent是响应。 Here is my code: 这是我的代码:

public JFrame frame = new JFrame("JFrame Test");

public static void main(String[]args) {
    new JFrameTest();
}

public JFrameTest() {
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(new keys());
    frame.add(new mouse());
    frame.add(new render());
    frame.setVisible(true);
}

@SuppressWarnings("serial")
public class render extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(0, 0, frame.getWidth() / 2, frame.getHeight() / 2);
        repaint();
    }
}

@SuppressWarnings("serial")
public class keys extends JPanel {
    public keys() {
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                System.out.println("Key Pressed!");
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("Key Released!");
            }
        });
    }
}

@SuppressWarnings("serial")
public class mouse extends JPanel {
    public mouse() {
        setFocusable(true);
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                System.out.println("Mouse Pressed!");
            }

            public void mouseReleased(MouseEvent e) {
                System.out.println("Mouse Released!");
            }
        });
    }
}

I made sure the mouseListener and the keyListener are both setFocusable(true) and the paintComponent was added last so it actually displays. 我确保mouseListener和keyListener都为setFocusable(true),并且最后添加了paintComponent以便实际显示。 It appears as though if I switch the order of adding the keyListener object and the mouseListener object then neither works. 好像我切换了keyListener对象和mouseListener对象的添加顺序一样,但这两个都不起作用。 If you have any ideas on how to fix it I would be grateful. 如果您有任何解决办法的想法,我将不胜感激。 Thanks! 谢谢!

I'll tell you what the problem is, the JFrame uses BroderLayout as its default layout manager. 我将告诉您问题是什么,JFrame使用BroderLayout作为其默认布局管理器。 So when you do frame.add() three times in a row without specifying the location of where the JPanel is being added you are over-writing the same panel 3 times in a row. 因此,当您连续三遍执行frame.add()而未指定要添加JPanel的位置时,您将连续三遍覆盖同一面板。

So the last thing you add will be there. 因此,您添加的最后一件事将在那里。

Try this... 尝试这个...

frame.add(new keys(), BorderLayout.NORTH);
frame.add(new mouse(), BorderLayout.SOUTH);
frame.add(new render(), BorderLayout.CENTER);

Just as a side note, this looks like a really weird way to program. 顺便提一下,这看起来是一种非常奇怪的编程方式。 You are better off creating 1 class that extends JPanel and adding a key listener and mouse listener to it. 最好创建1个扩展JPanel的类,并向其添加一个键侦听器和鼠标侦听器。 And override its paintComponent() method to do your rendering. 并重写其paintComponent()方法进行渲染。

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

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