Java 杰帕内尔没有出现
为什么UI没有显示在下面的代码中:Java 杰帕内尔没有出现,java,swing,jpanel,Java,Swing,Jpanel,为什么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 JL
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);
}
}
公共类GUI扩展了JPanel{
公共GUI(字符串名称、字符串地址、列表评论、图标){
设置大小(600600);
setLayout(新的BorderLayout());
JLabel iconLabel=新的JLabel(图标);
JLabel namelab=新的JLabel(名称);
JLabel addressLabel=新的JLabel(地址);
JPanel southReviewPanel=新JPanel();
southReviewPanel.setLayout(新的BoxLayout(southReviewPanel,BoxLayout.Y_轴));
for(字符串审阅:审阅){
southReviewPanel.add(新JTextArea(审查));
}
添加(southReviewPanel);
添加(iconLabel,BorderLayout.WEST);
JPanel northPane=新的JPanel();
添加(名称标签);
添加(地址标签);
添加(northPane,BorderLayout.NORTH);
}
公共静态void main(字符串[]args){
ImageIcon ic=新的ImageIcon();
列表=新的ArrayList();
列表。添加(“审查1”);
列表。添加(“评审2”);
列表。添加(“审查3”);
列表。添加(“审查4”);
GUI测试=新GUI(“测试”、“测试”、列表、ic);
test.setVisible(真);
}
}
我想JPanel不可能是顶级容器。它必须放在JFrame或JWindow中才能显示
JFrame f=new JFrame();
f.add(test);
f.setVisible(true);
JPanel不是顶级容器。您需要将该JPanel放在JDialog或JFrame中。确保将其添加到该对话框或框架的内容窗格中:
JFrame f = new JFrame();
f.getContentPane().add(test);
面板不会在秋千上出现。它们必须添加到windows中。创建JFrame或JDialog并将面板添加到其中