0

我正在制作一个钢琴界面,并试图让它在用户点击白键时变成浅灰色,然后当他们释放它时。当我按下键时,它是前景的白键并隐藏了黑键。

在点击任何东西之前:

http://i.imgur.com/gwZAbJV.png

点击几个键后:

http://i.imgur.com/Ax7dw6w.png

键是 JLayeredPanel 内的 JPanel,我正在设置背景颜色以更改颜色。当我单击底部键时,我希望黑键保持在顶部。有什么办法可以做到这一点吗?我正在使用 Netbeans GUI Builder

4

1 回答 1

2

首先仔细看看如何使用分层窗格。您需要指定您希望每个组件驻留的层,否则它们将相互重叠

万一你错过了,这里的关键因素是使用JLayeredPane#setLayer

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestLayer {

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

    public TestLayer() {
        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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JLayeredPane {

        public TestPane() {
            setLayout(new GridBagLayout());
            ColorPane background = new ColorPane(Color.WHITE) {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(200, 200);
                }
            };
            ColorPane foreground = new ColorPane(Color.BLACK) {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(100, 100);
                }
            };

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(background, gbc);
            setLayer(background, 0);

            add(foreground, gbc);
            setLayer(foreground, 1);
        }

        @Override
        public void doLayout() {
            super.doLayout();
        }

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

        public class ColorPane extends JPanel {

            public ColorPane(Color backGround) {
                setBackground(backGround);
                setBorder(new LineBorder(Color.RED));
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        setBackground(Color.RED);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        setBackground(backGround);
                    }

                });
            }

        }

    }

}
于 2014-12-30T21:50:00.973 回答