[英]Adding a JPanel to a JPanel with layouts
I have a JPanel that shows my main gui on it. 我有一个JPanel可以显示我的主要GUI。 On that main panel I want to add other JPanels that show the description of a item and allow you to buy it and sell it. 在该主面板上,我想添加其他JPanels来显示项目的描述,并允许您购买和出售它。 I am using FlowLayout for both my main panel and my ItemPanel. 我的主面板和ItemPanel都使用FlowLayout。 Here are the classes: 这些是类:
MainPanel 主面板
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());
}
}
ItemPanel 项目面板
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);
}
}
The issue that I am having is if I use the Spring in ItemPanel, when I add it to the main panel it just shows nothing. 我遇到的问题是,如果我在ItemPanel中使用Spring,将其添加到主面板后,它什么也没有显示。 If I don't use a layout in ItemPanel it shows perfectly fine, but it doesn't have the pretty layout that ItemPanel has if I add it to the JFrame by itself. 如果我不在ItemPanel中使用布局,则显示效果很好,但是如果我将其单独添加到JFrame中,则它没有ItemPanel的漂亮布局。
You didn't put any constraints on your ItemPanel for SpringLayout, so it doesn't expand or know how much to take up within the MainPanel. 您没有在用于SpringLayout的ItemPanel上施加任何约束,因此它不会扩展或不知道MainPanel中要占用多少空间。 When I added constraints to the panel it appeared. 当我向面板添加约束时,它出现了。 Try adding this and see if your panel appears. 尝试添加它,看看是否出现您的面板。
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);
Are you designing this by hand and then pressing Run to view it, or using, for example, WindowBuilder ? 您是在手动设计,然后按“运行”以查看它,还是使用例如WindowBuilder ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.