简体   繁体   中英

Adding a JPanel to a JPanel with layouts

I have a JPanel that shows my main gui on it. 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. I am using FlowLayout for both my main panel and my ItemPanel. 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. 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.

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. 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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM