简体   繁体   English

如何在JFrame中的其他东西下面添加一个mouselistener?

[英]How to add a mouselistener below other things in the JFrame?

If my question sounds stupid, I'm sorry, I'm still new to java. 如果我的问题听起来很愚蠢,我很抱歉,我还是java新手。 I'm gonna show you this bit of code: 我要告诉你这段代码:

public class Window extends JFrame{


    private static final long serialVersionUID = -2139430787087799923L;

    private TransparentJPanel mousepanel;

    public Window(int width, int height, String title, Game game) {
        super(title);   
        HandlerClass handler = new HandlerClass();
        mousepanel = new TransparentJPanel();
        mousepanel.addMouseListener(handler);

        setPreferredSize(new Dimension(width,height));
        setMaximumSize(new Dimension(width,height));
        setMinimumSize(new Dimension(width,height));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);

        setVisible(true);
        add(game);      <-- here I add the game
        add(mousepanel);    <-- here I add the panel with the mouselistener
        game.start();
    }
}

the TransparentJPanel class: TransparentJPanel类:

private class TransparentJPanel extends JPanel {
    private static final long serialVersionUID = 8855609189172945035L;

    TransparentJPanel() {
        super() ;
        this.setOpaque( false ) ;
        this.setLayout( null ) ;
    }
}

when I add the game first, the game shows up and the mouse listener doesn't work, but when I add the jpanel first, the game doesnt show up, but the mouselistener works. 当我首先添加游戏时,游戏出现并且鼠标监听器不起作用,但是当我首先添加jpanel时,游戏不会显示,但是mouselistener可以工作。 Here's my HandlerClass class: 这是我的HandlerClass类:

private class HandlerClass implements MouseListener {
    public void mouseClicked(MouseEvent event) {
        System.out.println(String.format("Clicked at %d,%d", event.getX(), event.getY()));
    }

    public void mouseEntered(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);

    }
    public void mouseExited(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);

    }
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Pressed frame at X: " + x + " - Y: " + y);

    }

    public void mouseReleased(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Released at X: " + x + " - Y: " + y);

    }

}

So, that's my problem. 所以,这是我的问题。 Please help. 请帮忙。

The solution turned out to be simple! 解决方案结果很简单!

instead of creating a panel for the mouse listener, you can add a mouselistener to the game object, namely as follows 您可以将鼠标滑块添加到游戏对象,而不是为鼠标侦听器创建面板,如下所示

public Window(int width, int title, String title, Game game) {
    super(title);
    HandlerClass handler = new HandlerClass();

    setPreferredSize(new Dimension(width,height));
    setMaximumSize(new Dimension(width,height));
    setMinimumSize(new Dimension(width,height));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);

    setVisible(true);
    add(game, BorderLayout.CENTER);
    game.addMouseListener(handler);     
    game.start();
}

Hope this helps! 希望这可以帮助!

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

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