JPanel没有出现

时间:2010-11-18 04:25:05

标签: java swing jpanel

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

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);

    }

}

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:0)

JPanel不是顶级容器。您需要将JPanel放在JDialog或JFrame中。确保将其添加到该对话框或框架的内容窗格中:

JFrame f = new JFrame();
f.getContentPane().add(test);

答案 2 :(得分:0)

小组不会出现在Swing中。必须将它们添加到窗口中。创建JFrame或JDialog并将面板添加到其中。