[英]Adding a JPanel to a JPanel with layouts
我有一個JPanel可以顯示我的主要GUI。 在該主面板上,我想添加其他JPanels來顯示項目的描述,並允許您購買和出售它。 我的主面板和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的漂亮布局。
您沒有在用於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 ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.