mouseEntered中的图像和光标不匹配

时间:2018-06-04 08:37:16

标签: java swing user-interface

我正在开发游戏GUI。

但是我在编程时遇到了一个小问题。

我按下按钮开始并居中按钮。

我重写mouseEnteredmouseExited

当我运行程序时,Image位于中心,但光标从远处反应。

我不知道为什么图像和光标不匹配......

这是我的主要代码。

    package PoET;

    public class Main {

        public static final int SCREEN_WIDTH=600;
        public static final int SCREEN_HEIGHT=800;

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new Display();
        }

    }

这是我的显示代码。

package PoET;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame; 
import javax.swing.JLabel;

public class Display extends JFrame {

private Image screenImage;
private Graphics screenGraphic;

private Image explainImage = new ImageIcon(Main.class.getResource("../images/explainSample.jpg")).getImage();
private Image introBackground = new ImageIcon(Main.class.getResource("../images/background.jpg")).getImage();
private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png")));

private ImageIcon quitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/quitButtonBasic.png"));
private ImageIcon quitButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/quitButtonPressed.png"));

private ImageIcon startButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
private ImageIcon startButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
private ImageIcon developerButtonBasicImage = new ImageIcon(Main.class.getResource("../images/developerButtonBasic.png"));
private ImageIcon developerButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/developerButtonEntered.png"));

private ImageIcon goButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
private ImageIcon goButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));

private JButton quitButton = new JButton(
        quitButtonBasicImage);
private JButton startButton = new JButton(
        startButtonBasicImage);
private JButton developerButton = new JButton(
        developerButtonBasicImage);
private JButton goButton = new JButton(
        new ImageIcon(Main.class.getResource("../images/startButtonEntered.png")));


private int mouseX, mouseY;
private boolean isExplainScreen=false;

    public Display() {
    setUndecorated(true);
    setTitle("RogueLike PoET");
    setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
    setResizable(false); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setBackground(new Color(0, 0, 0, 0));
    setLayout(null);

    //quitButton.setBounds(560, 30, 30, 30);
    quitButton.setBounds(400, 200, 30, 30);
    quitButton.setBorderPainted(false);
    quitButton.setContentAreaFilled(false);
    quitButton.setFocusPainted(false);
    quitButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            quitButton.setIcon(quitButtonEnteredImage);
            quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            quitButton.setIcon(quitButtonBasicImage);
            quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        }
        @Override
        public void mousePressed(MouseEvent e) {
            System.exit(0);
        }
    });
    add(quitButton);

    menuBar.setBounds(0, 0, 600, 30);
    menuBar.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            mouseX=e.getX();
            mouseY=e.getY();
        }
    });
    menuBar.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            int x=e.getXOnScreen();
            int y=e.getYOnScreen();
            setLocation(x-mouseX,y-mouseY);
        }
    });
    add(menuBar);


    startButton.setBounds(150, 540, 300, 60);
    startButton.setBorderPainted(false);
    startButton.setContentAreaFilled(false);
    startButton.setFocusPainted(false);
    startButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            startButton.setIcon(startButtonEnteredImage);
            startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            startButton.setIcon(startButtonBasicImage);
            startButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

        }

        @Override
        public void mousePressed(MouseEvent e) {
            startButton.setVisible(false);
            goButton.setVisible(true);
            introBackground=new ImageIcon(Main.class.getResource("../images/background2.jpg")).getImage();
            isExplainScreen=true;
        }
    });
    add(startButton);

    developerButton.setBounds(150, 610, 300, 60);
    developerButton.setBorderPainted(false);
    developerButton.setContentAreaFilled(false);
    developerButton.setFocusPainted(false);
    developerButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            developerButton.setIcon(developerButtonEnteredImage);
            developerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            developerButton.setIcon(developerButtonBasicImage);
            developerButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

        }

        @Override
        public void mousePressed(MouseEvent e) {
            developerButton.setVisible(false);
            goButton.setVisible(true);
            introBackground=new ImageIcon(Main.class.getResource("../images/background2.jpg")).getImage();
            isExplainScreen=false;
        }
    });
    add(developerButton);

    goButton.setVisible(false);
    goButton.setBounds(150, 720, 300, 60);
    goButton.setBorderPainted(false);
    goButton.setContentAreaFilled(false);
    goButton.setFocusPainted(false);
    goButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            goButton.setIcon(goButtonEnteredImage);
            goButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            goButton.setIcon(goButtonBasicImage);
            goButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

        }

        @Override
        public void mousePressed(MouseEvent e) {
            goButton.setVisible(false);
            introBackground=new ImageIcon(Main.class.getResource("../images/background2.jpg")).getImage();
            isExplainScreen=true;
        }
    });
    add(goButton);

    Music introMusic = new Music("introMusic.mp3", true);
    introMusic.start();
}

public void paint(Graphics g) {
    screenImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
    screenGraphic = screenImage.getGraphics();
    screenDraw(screenGraphic);
    g.drawImage(screenImage, 0, 0, null);
}

public void screenDraw(Graphics g) {
    g.drawImage(introBackground, 0, 0, null);
    paintComponents(g);
    if(isExplainScreen) {
        g.drawImage(explainImage, 50, 50,null);
    }
    this.repaint();
}

}

1 个答案:

答案 0 :(得分:0)

所以我剥离了你的例子,删除了MouseListener并只设置了按钮的光标,它工作正常 - 我重置按钮以绘制它们的边框和内容,这样我就可以看到它们的范围了,当鼠标进入/存在它们所覆盖的区域而没有问题时光标会发生变化

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Display extends JFrame {

    public static final int SCREEN_WIDTH = 600;
    public static final int SCREEN_HEIGHT = 800;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Display();
    }
    private Image screenImage;
    private Graphics screenGraphic;

//  private Image explainImage = new ImageIcon(Main.class.getResource("../images/explainSample.jpg")).getImage();
//  private Image introBackground = new ImageIcon(Main.class.getResource("../images/background.jpg")).getImage();
//  private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png")));
//
//  private ImageIcon quitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/quitButtonBasic.png"));
//  private ImageIcon quitButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/quitButtonPressed.png"));
//
//  private ImageIcon startButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
//  private ImageIcon startButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
//  private ImageIcon developerButtonBasicImage = new ImageIcon(Main.class.getResource("../images/developerButtonBasic.png"));
//  private ImageIcon developerButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/developerButtonEntered.png"));
//
//  private ImageIcon goButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
//  private ImageIcon goButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
    private JButton quitButton = new JButton(
                    "Quote");
    private JButton startButton = new JButton(
                    "Start");
    private JButton developerButton = new JButton(
                    "Developer");
    private JButton goButton = new JButton(
                    new ImageIcon("Go"));

    private int mouseX, mouseY;
    private boolean isExplainScreen = false;

    public Display() {
        setUndecorated(true);
        setTitle("RogueLike PoET");
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
//      setBackground(new Color(0, 0, 0, 0));
        setLayout(null);

        //quitButton.setBounds(560, 30, 30, 30);
        quitButton.setBounds(400, 200, 30, 30);
//      quitButton.setBorderPainted(false);
//      quitButton.setContentAreaFilled(false);
        quitButton.setFocusPainted(false);
        quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(quitButton);

        startButton.setBounds(150, 540, 300, 60);
//      startButton.setBorderPainted(false);
//      startButton.setContentAreaFilled(false);
        startButton.setFocusPainted(false);
        startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(startButton);

        developerButton.setBounds(150, 610, 300, 60);
//      developerButton.setBorderPainted(
//                      false);
//      developerButton.setContentAreaFilled(
//                      false);
        developerButton.setFocusPainted(
                        false);
        developerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(developerButton);

        goButton.setVisible(
                        false);
        goButton.setBounds(
                        150, 720, 300, 60);
//      goButton.setBorderPainted(
//                      false);
//      goButton.setContentAreaFilled(
//                      false);
        goButton.setFocusPainted(
                        false);
        goButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(goButton);
    }

    public void paint(Graphics g) {
        screenImage = createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
        screenGraphic = screenImage.getGraphics();
        screenDraw(screenGraphic);
        g.drawImage(screenImage, 0, 0, null);
    }

    public void screenDraw(Graphics g) {
//      g.drawImage(introBackground, 0, 0, null);
        paintComponents(g);
        if (isExplainScreen) {
//          g.drawImage(explainImage, 50, 50, null);
        }
        this.repaint();
    }
}

观察

  • 不要覆盖paint等顶级容器的JFrameJFrame是一个复合组件(它有一系列构成其核心功能的子组件),覆盖paint会对这些组件的绘制方式产生不利影响。与顶级容器不同,Swing组件默认为双缓冲。
  • 此外,“可视”区域“窗口”区域是两个不同的概念。有关详细信息,请参阅How to get the EXACT middle of a screen, even when re-sized
  • 另外,你可以在窗户装饰下面画画(我知道,这是一个未修饰的窗户,但它仍然是一个坏习惯)
  • 不要尝试将所有逻辑放入单个类/绘制方法中。相反,将屏幕分解为单独的组件,并使用CardLayout之类的内容在它们之间切换

此...

setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);

通常是一个坏主意。最好让孩子的组成部分决定他们喜欢的大小,只需pack他们周围的窗口

在建立基本用户界面之前不要致电setVisible(true);,否则某些组件可能不会被绘制

我不确定你希望从setBackground(new Color(0, 0, 0, 0));获得什么样的装备,但根据你目前的设计,它似乎是浪费

不要调用this.repaint();或在绘画链中执行可能触发repaint的任何其他操作。这将为您设置一个无限循环,最终消耗所有CPU周期

setLayout(null);不明智 - 组件如何布局很多,你需要做很多工作来重现它

相反,它可能看起来像(作为一个起点)......

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Display());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Display extends JPanel {

        //  private Image explainImage = new ImageIcon(Main.class.getResource("../images/explainSample.jpg")).getImage();
        //  private Image introBackground = new ImageIcon(Main.class.getResource("../images/background.jpg")).getImage();
        //  private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png")));
        //
        //  private ImageIcon quitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/quitButtonBasic.png"));
        //  private ImageIcon quitButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/quitButtonPressed.png"));
        //
        //  private ImageIcon startButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
        //  private ImageIcon startButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
        //  private ImageIcon developerButtonBasicImage = new ImageIcon(Main.class.getResource("../images/developerButtonBasic.png"));
        //  private ImageIcon developerButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/developerButtonEntered.png"));
        //
        //  private ImageIcon goButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
        //  private ImageIcon goButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
        private JButton quitButton = new JButton(
                        "Quote");
        private JButton startButton = new JButton(
                        "Start");
        private JButton developerButton = new JButton(
                        "Developer");
        private JButton goButton = new JButton(
                        new ImageIcon("Go"));

        private int mouseX, mouseY;
        private boolean isExplainScreen = false;

        public Display() {
            setLayout(null);

            //quitButton.setBounds(560, 30, 30, 30);
            quitButton.setBounds(400, 200, 30, 30);
            //      quitButton.setBorderPainted(false);
            //      quitButton.setContentAreaFilled(false);
            quitButton.setFocusPainted(false);
            quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            add(quitButton);

            startButton.setBounds(150, 540, 300, 60);
            //      startButton.setBorderPainted(false);
            //      startButton.setContentAreaFilled(false);
            startButton.setFocusPainted(false);
            startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            add(startButton);

            developerButton.setBounds(150, 610, 300, 60);
            //      developerButton.setBorderPainted(
            //                      false);
            //      developerButton.setContentAreaFilled(
            //                      false);
            developerButton.setFocusPainted(
                            false);
            developerButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            add(developerButton);

            goButton.setVisible(
                            false);
            goButton.setBounds(
                            150, 720, 300, 60);
            //      goButton.setBorderPainted(
            //                      false);
            //      goButton.setContentAreaFilled(
            //                      false);
            goButton.setFocusPainted(
                            false);
            goButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            add(goButton);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 800);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(introBackground, 0, 0, null);

            // This should be done else where
//              if (isExplainScreen) {
//                  //          g.drawImage(explainImage, 50, 50, null);
//              }
            //this.repaint();
        }
    }
}