我正在学习使用Java Swing类并为练习制作一个简单的post-it应用程序 .
我不知道如何处理这个问题:
我想,当鼠标进入窗口时,第二个面板降低,其上有按钮以最小化窗口,退出或创建一个新的(我还没有实现) .
另一个面板,主要面板,覆盖框架,所以我不能添加一个监听器来捕获鼠标的通道 .
但是,如果鼠标在顶部面板上传递,则动画不能正常工作并且存在小错误 .

我应该如何确保当鼠标在窗口上传递时,在两种情况下,如果它位于第一个面板上或第二个面板上,动画会继续不受干扰?

这是代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

class PostIt extends JFrame {

    private enum Colors {
        YELLOW, BLUE, GREEN, ORANGE, VIOLET, RED;
    }

    // stuff
    private static final long serialVersionUID = 1L;
    private static int count = 0;
    private int color;

    // components
    private final JPanel topPanel;
    private final JButton minimize;
    private final JButton exit;
    private final JButton add;

    // coords and dimensions
    private static Point componentCoords = null;
    private static final int WIDTH = 200; // 30*6
    private static final int HEIGHT = 200; // 30*7
    private static final int ORIGINALX = 700;
    private static final int ORIGINALY = 100;
    // 7*6 matrix
    // 30*30 px each cell
    private static int animationH = 10;

    public PostIt() {
        // frame
        environment();
        count++;

        // panel
        final JPanel panel = new JPanel();
        panel.setBounds(0, 0, WIDTH, animationH);

        panel.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(final MouseEvent e) {
            }

            @Override
            public void mouseDragged(final MouseEvent e) {
                final Point currentCoords = e.getLocationOnScreen();
                setLocation(currentCoords.x - componentCoords.x, currentCoords.y - componentCoords.y);
            }
        });
        panel.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(final MouseEvent e) {}

            @Override
            public void mousePressed(final MouseEvent e) {
                componentCoords = e.getPoint();
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                componentCoords = null;
            }

            @Override
            public void mouseEntered(final MouseEvent e) {}

            @Override
            public void mouseExited(final MouseEvent e) {}
        });

        // top panel
        topPanel = new JPanel((LayoutManager) null);
        topPanel.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(final MouseEvent e) {
            }

            @Override
            public void mouseDragged(final MouseEvent e) {
                final Point currentCoords = e.getLocationOnScreen();
                setLocation(currentCoords.x - componentCoords.x, currentCoords.y - componentCoords.y);
            }
        });
        topPanel.setBackground(randomColor());
        topPanel.addMouseListener(new MouseListener() {
            @Override
            public void mousePressed(final MouseEvent e) {
                componentCoords = e.getPoint();
            }

            @Override
            public void mouseClicked(final MouseEvent e) {

            }

            @Override
            public void mouseEntered(final MouseEvent e) { // expand
                new Timer(1, e1 -> {
                    panel.setSize(WIDTH, animationH);
                    animationH += 1;
                    if (animationH >= 30) {
                        ((Timer) e1.getSource()).stop();
                    }
                }).start();
            }

            @Override
            public void mouseExited(final MouseEvent e) { // compress
                new Timer(1, e1 -> {
                    panel.setSize(WIDTH, animationH);
                    animationH -= 1;
                    if (animationH <= 10) {
                        ((Timer) e1.getSource()).stop();
                    }
                }).start();
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                componentCoords = null;
            }
        });
        add(topPanel);
        panel.setBackground(topPanel.getBackground().darker());
        topPanel.add(panel);

        // exit
        exit = new JButton("x");
        exit.addActionListener(e -> {
            count--;
            dispose();
        });
        // topPanel.add(exit);

        // add
        add = new JButton("+");
        add.addActionListener(e -> {
            final PostIt p = new PostIt();
            System.out.println(count);
        });
        // topPanel.add(add);

        // add
        minimize = new JButton("_");
        minimize.addActionListener(e -> {
            setState(JFrame.ICONIFIED);
        });
        // topPanel.add(minimize);
    }

    public static boolean isMouseWithinComponent(final Component c)
    {
        final Point mousePos = MouseInfo.getPointerInfo().getLocation();
        final Rectangle bounds = c.getBounds();
        bounds.setLocation(c.getLocationOnScreen());
        return bounds.contains(mousePos);
    }

    private void save() {
    }

    private void restore() {
    }

    /*
     * private void loadImages () { try { exitIcon = new ImageIcon(ImageIO.read(new
     * File("src\\media\\exit48.png"))); addIcon = new ImageIcon(ImageIO.read(new
     * File("src\\media\\add48.png"))); minimizeIcon = new
     * ImageIcon(ImageIO.read(new File("src\\media\\minimize48.png"))); } catch
     * (Exception e) { e.printStackTrace(); } }
     */

    private Color randomColor() {
        switch (Colors.values()[randInt(Colors.values().length - 1)]) {
        case YELLOW:
            return new Color(255, 240, 120);
        case BLUE:
            return new Color(200, 240, 250);
        case GREEN:
            return new Color(205, 255, 140);
        case ORANGE:
            return new Color(250, 190, 5);
        case RED:
            return new Color(240, 140, 130);
        case VIOLET:
            return new Color(215, 175, 250);
        default:
            return null;
        }
    }

    private void environment() {
        setBounds(nextX(), nextY(), WIDTH, HEIGHT);
        setAlwaysOnTop(true);
        setUndecorated(true);
        setResizable(false);
        setVisible(true);
    }

    private static int randInt(final int min, final int max) {
        if (min < 0 || min > max || max <= 0)
            throw new IllegalArgumentException();
        return (int) (java.lang.Math.random() * (max - min + 1) + min);
    }

    private static int randInt(final int max) {
        return randInt(0, max);
    }

    private int nextX() {
        return randInt(ORIGINALX - 50, ORIGINALX + 50);
    }

    private int nextY() {
        return randInt(ORIGINALY - 50, ORIGINALY + 50);
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                new PostIt();
            } catch (final Exception ex) {
                ex.printStackTrace();
                // Logger.getLogger(PostIt.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }
}