У меня есть JPanel, который показывает мой главный gui на нем. На этой основной панели я хочу добавить другие JPanels, которые показывают описание элемента и позволяют вам его покупать и продавать. Я использую FlowLayout для моей основной панели и моей ItemPanel. Вот классы:Добавление JPanel в JPanel с макетами
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);
}
}
Вопрос, который я имею, если я использую Весна в ItemPanel, когда я добавляю его к на главной панели ничего не видно. Если я не использую макет в ItemPanel, он выглядит отлично, но у него нет красивого макета, который имеет ItemPanel, если я добавлю его в JFrame сам по себе.
Почтовый код, а не ссылки. –
Нет проблем с добавлением JPanel к другому JPanel, оба из которых имеют свои собственные макеты. Убедитесь, что каждый элемент на каждой панели находится только на этой панели. Почтовый код здесь, а не на другом сайте. – arcy