JPanel没有出现 [英] JPanel not showing up

查看:128
本文介绍了JPanel没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么用户界面没有显示在我的代码中:

Why is the UI not showing up in my code below:

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中才能显示

I guess JPanel cannot be a toplevel container. It has to be put inside a JFrame or JWindow to be shown

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

这篇关于JPanel没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆