代码之家  ›  专栏  ›  技术社区  ›  aherlambang

JPanel没有出现

  •  0
  • aherlambang  · 技术社区  · 15 年前

    为什么用户界面没有出现在我的下面的代码中:

    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 回复  |  直到 9 年前
        1
  •  6
  •   vallentin Remi    9 年前

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

    JFrame f=new JFrame();
    f.add(test);
    f.setVisible(true);
    
        2
  •  0
  •   Eugene Ryzhikov    15 年前

    面板在摇摆时不会出现。它们必须添加到Windows中。创建jframe或jdialog并将面板添加到其中。

        3
  •  0
  •   Dimitar    9 年前

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

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