JFrame显示 - 鼠标监听器

时间:2014-12-13 21:12:59

标签: java swing listeners

我目前正在使用java开发游戏。当我开始游戏时,我可以通过单击框架中的某个位置在全屏和窗口模式之间进行选择。窗口模式工作正常,但我在全屏幕上有一些问题所以如果我在我的main()中只是一个简单的游戏初始化,比如new FullscreenMode(),那么游戏就完美了,听众正在工作。如果我从下面的代码启动全屏模式框架构造函数它工作得很完美,但如果我从属于我的类的函数启动它,所有的监听器将无法工作......当我说它不能正常工作时,我的意思是听众没有响应,我点击的每个地方我得不到游戏的响应,但如果从构造函数启动全屏模式框架,一切都很完美。提前谢谢你!

 public class ResolutionChoser extends JFrame implements MouseMotionListener,
        MouseListener {

    private static final long serialVersionUID = 1L;
    private BufferedImageLoader loader;
    private BufferedImage rezImg = null;


    public ResolutionChoser() {
        super("Welcome!");
        requestFocus();
        loader = new BufferedImageLoader();
        rezImg = loader.loadImage("/RezImg.png");
        this.setPreferredSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
                Game.HEIGHT * Game.SCALE + 2));
        this.setMaximumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
                Game.HEIGHT * Game.SCALE + 2));
        this.setMinimumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
                Game.HEIGHT * Game.SCALE + 2));
        this.pack();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        addMouseMotionListener(this);
        addMouseListener(this);
        JLabel jl = new JLabel() {
            private static final long serialVersionUID = 1L;

            @Override
            public void paintComponent(Graphics g) {
                g.drawImage(rezImg, 0, 0, null);
            }
        };
        setContentPane(jl);

        //Works Perfectly if I do it like so
        // launchFullScreen();


    }

    //Method that launches my fullscreen game
    public void launchFullScreen() {
        removeMouseMotionListener(this);
        removeMouseListener(this);
        MultiBufferTest.main(null);
    }

    public static void main(String[] args) {
        ResolutionChoser rc = new ResolutionChoser();
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        System.out.println("CLICKED AT: " + arg0.getX() + "--" + arg0.getY());
        launchFullScreen();
        //not working properly, the listeners for my game aren't responding

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

2 个答案:

答案 0 :(得分:1)

嗯,您正在使用launchFullScreen()方法移除侦听器:

public void launchFullScreen() {
    removeMouseMotionListener(this);
    removeMouseListener(this);
    MultiBufferTest.main(null);
}

您希望听众如何回应?

答案 1 :(得分:1)

不要将Mouseistener直接附加到一个框架,用户框架之间有一个JRootPane和内容窗格(可能还有一个玻璃窗格),其中任何一个都可能是偷鼠标事件,这将阻止您的框架接收鼠标事件。

而是直接将MouseListener附加到顶级组件

也...

JLabel jl = new JLabel() {
    private static final long serialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(rezImg, 0, 0, null);
    }
};

这不仅是一个坏主意(你已经破坏了油漆链),它也不是必需的,因为JLabel能够显示图像。

Painting in AWT and SwingPerforming Custom PaintingHow to Use Labels了解更多详情