[英]Adding JPanel from one class to a JPanel in a different class
I have frame JFrame to which I'm adding gui JPanel which consists of multiple different JPanels and one of which is in a different class. 我有框架JFrame,我向其中添加了gui JPanel,它由多个不同的JPanels组成,其中一个位于不同的类中。
The problem is I'm unable to add that other classes JPanel to my gui JPanel I get no errors but nothing is displayed 问题是我无法将其他类JPanel添加到我的gui JPanel中,我没有收到任何错误,但没有显示任何内容
PurchaseStock.java 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 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;
}
}
UPDATE: 更新:
PurchaseStock.java 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 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);
}
}
Update 2: 更新2:
StockDropdown.java 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);
};
}
You do get the other JPanel but you've added nothing to it since components are added in its constructor and the StockDropdown constructor is never called. 您确实获得了另一个JPanel,但由于在其构造函数中添加了组件并且从未调用过StockDropdown构造函数,因此未对其添加任何内容。 I'd get rid of the static fields and methods unless you plan to create a true valid static factory method, one that calls the appropriate constructor.
除非您打算创建一个真正的有效静态工厂方法,该方法将调用适当的构造函数,否则我将摆脱这些静态字段和方法。
eg, 例如,
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;
}
}
Note that if StockDropdown extends JPanel, get rid of the ui variable, because with it, you're adding components to a container that is not being displayed: 请注意,如果StockDropdown扩展了JPanel,请摆脱ui变量,因为使用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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.