【问题标题】:Adding a JPanel to a JPanel with layouts将 JPanel 添加到具有布局的 JPanel
【发布时间】:2013-11-08 15:20:15
【问题描述】:

我有一个 JPanel,上面显示了我的主要 gui。在该主面板上,我想添加其他 JPanel,以显示项目的描述并允许您购买和出售它。我的主面板和 ItemPanel 都使用 FlowLayout。以下是课程:

主面板

package me.phination.clicker.gui;

import javax.swing.JPanel;

public class MainPanel extends JPanel {

    private JLabel batchLabel;
    private JButton btnMakeBatch;
    private JButton btnSellBatch;
    private JLabel moneyLabel;
    private JTextArea textArea;
    private JPanel itemPanel;


    public MainPanel() {
        setBackground(new Color(153, 51, 0));
        SpringLayout springLayout = new SpringLayout();
        setLayout(springLayout);

            //add the item panel to this panel
        itemPanel = new ItemPanel();
        itemPanel.setSize(200, 100);
        add(itemPanel);
    }

    public void consoleMsg(String msg) {
        textArea.append(msg + "\n");
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}

项目面板

package me.phination.clicker.game;

import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;

public class ItemPanel extends JPanel {
        public ItemPanel() {
                SpringLayout springLayout = new SpringLayout();
                setLayout(springLayout);

                JLabel lblItemName = new JLabel("Item name");
                lblItemName.setFont(new Font("Lucida Grande", Font.BOLD, 18));
                springLayout.putConstraint(SpringLayout.NORTH, lblItemName, 10, SpringLayout.NORTH, this);
                add(lblItemName);

                JLabel lblAmountOwned = new JLabel("Amount owned");
                springLayout.putConstraint(SpringLayout.WEST, lblAmountOwned, 10, SpringLayout.WEST, this);
                springLayout.putConstraint(SpringLayout.WEST, lblItemName, 0, SpringLayout.WEST, lblAmountOwned);
                springLayout.putConstraint(SpringLayout.SOUTH, lblAmountOwned, -10, SpringLayout.SOUTH, this);
                add(lblAmountOwned);

                JButton btnPurchase = new JButton("Purchase");
                springLayout.putConstraint(SpringLayout.NORTH, btnPurchase, 0, SpringLayout.NORTH, lblItemName);
                springLayout.putConstraint(SpringLayout.EAST, btnPurchase, -10, SpringLayout.EAST, this);
                add(btnPurchase);

                JButton btnSell = new JButton("Sell");
                springLayout.putConstraint(SpringLayout.SOUTH, btnSell, -10, SpringLayout.SOUTH, this);
                springLayout.putConstraint(SpringLayout.EAST, btnSell, 0, SpringLayout.EAST, btnPurchase);
                add(btnSell);

                JLabel lblDescription = new JLabel("Description");
                springLayout.putConstraint(SpringLayout.NORTH, lblDescription, 6, SpringLayout.SOUTH, lblItemName);
                springLayout.putConstraint(SpringLayout.WEST, lblDescription, 0, SpringLayout.WEST, lblItemName);
                add(lblDescription);
        }
}

我遇到的问题是,如果我在 ItemPanel 中使用 Spring,当我将它添加到主面板时,它什么也不显示。如果我不在 ItemPanel 中使用布局,它显示得非常好,但是如果我将它自己添加到 JFrame 中,它就没有 ItemPanel 所具有的漂亮布局。

【问题讨论】:

  • 邮政编码,不是链接。
  • 将一个 JPanel 添加到另一个 JPanel 是没有问题的,它们都有自己的布局。确保每个面板上的每个元素仅在该面板上。在此处发布代码,而不是在其他网站上。

标签: java swing jpanel


【解决方案1】:

您没有对 SpringLayout 的 ItemPanel 施加任何限制,因此它不会扩展或不知道在 MainPanel 中占用多少。当我向面板添加约束时,它出现了。尝试添加它,看看您的面板是否出现。

springLayout.putConstraint(SpringLayout.NORTH, itemPanel, 22, SpringLayout.SOUTH, btnMakeBatch);
springLayout.putConstraint(SpringLayout.WEST, itemPanel, 43, SpringLayout.WEST, this);
springLayout.putConstraint(SpringLayout.EAST, itemPanel, -104, SpringLayout.EAST, this);

您是手动设计然后按运行查看它,还是使用例如WindowBuilder

【讨论】:

  • 我决定尝试 WindowBuilder 来了解不同的布局是如何工作的。过去,我总是不使用布局,而是手动完成所有位置。现在我更好地理解了 SpringLayout 的工作原理,我将尽量远离 WindowBuilder。不过那行得通,谢谢。
  • 我的意思是如果你不使用它,你应该使用它。当我在 WindowBuilder 中打开 GUI 时,我看到你的 ItemPanel 在角落里缩小了,因为它没有附加锚点。拖动它可以调整大小并正确锚定。基本上,如果某些东西看起来不像,请尝试在 Windowbuilder 的拖放 GUI 中手动调整它,直到看起来正确,然后在代码中微调您真正想要它做的事情。
猜你喜欢
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
相关资源
最近更新 更多