【问题标题】:Layered Panel GUI分层面板 GUI
【发布时间】:2015-03-31 14:56:19
【问题描述】:

我正在尝试为其他乐器标签制造商制作键盘。 我设置钢琴的方式是使用分层面板。我现在遇到的问题是,每当我尝试添加与钢琴本身不同的任何其他东西时,它都不会出现,或者钢琴不会出现,它会吗? 所以我要问的是如何在屏幕右侧添加图像同时保持其他所有内容完好无损? 谢谢。 P.s 我知道黑色按钮很乱,请忽略。

class Piano extends JPanel
{

    public static void main(String[] args)
    {

        JFrame frame = new JFrame("Test");
        JLayeredPane panel = new JLayeredPane();
        frame.add(panel);

        for (int i = 0; i < 13; i++)
        {
            JButton b = new JButton();
            b.setBackground(Color.WHITE);
            b.setLocation(i * 20, 0);
            b.setSize(20, 100);
            panel.add(b, 0, -1);
        }
        JButton b = new JButton();
        b.setBackground(Color.BLACK);
        b.setLocation(16, 0);
        b.setSize(12, 80);
        panel.add(b, 1, -1);
        //
        JButton b1 = new JButton();
        b1.setBackground(Color.BLACK);
        b1.setLocation(52, 0);
        b1.setSize(12, 80);
        panel.add(b1, 1, -1);
        JButton b2 = new JButton();
        b2.setBackground(Color.BLACK);
        b2.setLocation(76, 0);
        b2.setSize(12, 80);
        panel.add(b2, 1, -1);
        //
        JButton b3 = new JButton();
        b3.setBackground(Color.BLACK);
        b3.setLocation(112, 0);
        b3.setSize(12, 80);
        panel.add(b3, 1, -1);
        JButton b4 = new JButton();
        b4.setBackground(Color.BLACK);
        b4.setLocation(134, 0);
        b4.setSize(12, 80);
        panel.add(b4, 1, -1);
        JButton b5 = new JButton();
        b5.setBackground(Color.BLACK);
        b5.setLocation(157, 0);
        b5.setSize(12, 80);
        panel.add(b5, 1, -1);
        //
        JButton b6 = new JButton();
        b6.setBackground(Color.BLACK);
        b6.setLocation(192, 0);
        b6.setSize(12, 80);
        panel.add(b6, 1, -1);
        JButton b7 = new JButton();
        b7.setBackground(Color.BLACK);
        b7.setLocation(216, 0);
        b7.setSize(12, 80);
        panel.add(b7, 1, -1);
        //
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }
}

【问题讨论】:

  • 为什么不在 JFrame 中放一个带 BorderLayout 的 JPanel。然后将您的 LayeredPane 添加到 Center 中,并在右侧添加另一个带有图像的 Jpanel。

标签: java swing jlayeredpane


【解决方案1】:

您基本上想为您的JPanel 使用Layout,例如GridLayout。

例如某事像这样

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,2));

Here is a example in the oracle doc

【讨论】:

    【解决方案2】:

    默认情况下,JFrame 使用BorderLayout。

    frame.add(panel);
    

    当您不指定约束时,将使用“CENTER”,因此您将分层窗格添加到框架的 CENTER。

    如果您想在框架的右侧添加另一个面板,那么您可以执行以下操作:

    JPanel right = new JPanel();
    right.add(...);
    frame.add(right, BorderLayout.EAST);
    

    阅读 Layout Managers 上的 Swing 教程部分了解更多信息。

    您可能还想查看 Midi Piano 以获得完整的钢琴键盘声音实现,只是为了好玩。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2013-12-23
      • 1970-01-01
      • 2016-09-18
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多