我有框架JFrame,我添加了gui JPanel,它由多个不同的JPanel组成,其中一个是不同的类。
问题是我无法将其他类JPanel添加到我的gui JPanel我没有错误但没有显示任何内容
PurchaseStock.java
guiC.gridx=0;
guiC.gridy=0;
gui.add(StockDropdown.getP(), guiC);
guiC.gridx=1;
guiC.gridy=0;
gui.add(quantityP, guiC);
guiC.insets = new Insets(30,0,0,0);
guiC.gridwidth=2;
guiC.gridx=0;
guiC.gridy=1;
gui.add(checkout, guiC);
frame.add(gui);
frame.pack(); //Size to components
frame.setResizable(false);
frame.setVisible(true);
StackDropdown.java
public class StockDropdown {
private static JPanel ui = new JPanel(new BorderLayout());
private JButton bt = new JButton("tsfdsfds");
private JPanel top = new JPanel();
private JPanel middle = new JPanel();
public StockDropdown(){
ui.add("North", top);
top.add(bt);
}
public static JPanel getP(){
return ui;
}
}
更新:
PurchaseStock.java
public class PurchaseStock extends JFrame implements ActionListener {
JFrame frame = new JFrame();
JPanel gui = new JPanel(new GridBagLayout());
GridBagConstraints guiC = new GridBagConstraints();
public PurchaseStock(){
frame.setTitle("Update Stock");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
guiC.gridx=0;
guiC.gridy=0;
gui.add(new StockDropdown(), guiC);
frame.add(gui);
frame.pack(); //Size to components
frame.setResizable(false);
frame.setVisible(true);
}
}
StackDropdown.java
package stock;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class StockDropdown extends JPanel{
private JPanel ui = new JPanel(new BorderLayout());
private JButton bt = new JButton("tsfdsfds");
private JPanel top = new JPanel();
private JPanel middle = new JPanel();
public StockDropdown(){
ui.add("North", top);
top.add(bt);
}
}
更新2:
StockDropdown.java
package stock;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class StockDropdown extends JPanel{
private JComboBox itemsDropdown = new JComboBox();
private JPanel top = new JPanel();
private JPanel middle = new JPanel();
JButton t = new JButton("tt");
public StockDropdown(){
add("North", top);
add("Center", middle);
top.add(itemsDropdown);
for(String key : StockData.getStock().keySet()) {
itemsDropdown.addItem(StockData.getName(key));
}
middle.add(t);
}
private ImageIcon setImageSize(String path) {
ImageIcon image1 = new ImageIcon(path);
Image image2 = image1.getImage().getScaledInstance(100,100,0);
return new ImageIcon(image2);
};
}
答案 0 :(得分:2)
你确实得到了另一个JPanel但你没有添加任何内容,因为组件被添加到它的构造函数中,而且从未调用StockDropdown构造函数。我除去了静态字段和方法,除非你打算创建一个真正有效的静态工厂方法,调用相应的构造函数。
如,
import javax.swing.*;
public class StockDropdown extends JPanel {
private JButton bt = new JButton("tsfdsfds");
private JPanel top = new JPanel();
private JPanel middle = new JPanel();
// make this private if using factory method
private StockDropdown(){
add("North", top);
top.add(bt);
}
// public static factory method
public static StockDropdown getInstance() {
StockDropdown ui = new StockDropdown();
return ui;
}
}
请注意,如果StockDropdown扩展了JPanel,请删除ui变量,因为有了它,您将组件添加到未显示的容器中:
public class StockDropdown extends JPanel {
private JButton bt = new JButton("tsfdsfds");
private JPanel top = new JPanel();
private JPanel middle = new JPanel();
public StockDropdown() {
setLayout(new BorderLayout());
top.add(bt);
// add("North", top);
add(top, BorderLayout.PAGE_START);
}
}