简体   繁体   中英

Java mouseListener on JPanel doesn't work

It goes like this : I want to add a mouse listener (mouse enter, mouse leave, mouse click) on a custom SplashScreen . So far I have the basic idea, but I could not figure out how to make the mouse listener work.

How do you add a mouseListenner on a JFrame that has only one component that overrides the paintComponent method?

I tried and I'm sure I'm doing something wrong, but I can't find what it is and I've tried every thing I could think of. Here's the simple program (a SSCCE) :

package GUI.other;

public class SplashScreen2 {

    private static final String IMAGE_LOCATION = new File(".").getAbsolutePath() + "/res/splashScreen.png";

    public SplashScreen2() {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);

        //The following doesnt work

        frame.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("Pressed");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("Pressed");
            }
        });

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setVisible(true);

        try {

            Thread.sleep(5000);
            frame.dispose();

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SplashScreen2();
            }
        });
    }

    @SuppressWarnings("serial")
    public class ImagePanel extends JPanel {

        BufferedImage img;

        public ImagePanel() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            try {
                img = ImageIO.read(new File(IMAGE_LOCATION));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1080 / 2, 720 / 2);
        }
    }
}

Here's the result :

透明的初始窗格

You should add the listener to your ImagePanel

ImagePanel panel = new ImagePanel();
panel.addMouseListener(new MouseAdapter() {

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("Pressed");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("Pressed");
    }
});
frame.add(panel);

Also, Thread.sleep(5000); doesn't work because you did this:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        new SplashScreen2();
    }
});

You called the constructor on the Event Dispatching Thread, when you block it, mouse events will not be dispatched.

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