gpt4 book ai didi

java - 将 JPanel 从一个类添加到另一类中的 JPanel

转载 作者:行者123 更新时间:2023-11-30 03:05:13 25 4
gpt4 key购买 nike

我有一个框架 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);
};

}

最佳答案

您确实获得了另一个 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);
}
}

关于java - 将 JPanel 从一个类添加到另一类中的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com