0

我有一个 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 所具有的漂亮布局。

4

1 回答 1

0

您没有对 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);

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

于 2013-11-08T15:37:05.170 回答