gpt4 book ai didi

java - JPanel 未显示

转载 作者:行者123 更新时间:2023-12-01 16:40:26 25 4
gpt4 key购买 nike

为什么 UI 没有显示在我的下面的代码中:

public class GUI extends JPanel{

public GUI(String name, String address, List<String> reviews, Icon icon){
setSize(600,600);
setLayout(new BorderLayout());
JLabel iconLabel = new JLabel(icon);
JLabel nameLabel = new JLabel(name);
JLabel addressLabel = new JLabel(address);
JPanel southReviewPanel = new JPanel();
southReviewPanel.setLayout(new BoxLayout(southReviewPanel, BoxLayout.Y_AXIS));
for (String review: reviews) {
southReviewPanel.add(new JTextArea(review));
}
add(southReviewPanel);
add(iconLabel, BorderLayout.WEST);
JPanel northPane = new JPanel();
northPane.add(nameLabel);
northPane.add(addressLabel);
add(northPane, BorderLayout.NORTH);
}


public static void main(String[] args) {
ImageIcon ic = new ImageIcon();
List<String> list = new ArrayList<String>();
list.add("review1");
list.add("review2");
list.add("review3");
list.add("review4");
GUI test = new GUI("test", "test", list, ic);

test.setVisible(true);

}

}

最佳答案

我猜 JPanel 不能是顶级容器。它必须放在 JFrame 或 JWindow 中才能显示

JFrame f=new JFrame();
f.add(test);
f.setVisible(true);

关于java - JPanel 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4211675/

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