2010-11-18 95 views
0

爲什麼UI沒有顯示在下面我的代碼:的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); 

    } 

} 

回答

6

我猜的JPanel不能是頂層容器。它必須放在JFrame或JWindow中才能顯示

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

JPanel不是頂級容器。您需要將該JPanel放置在JDialog或JFrame中。確保將其添加到該對話框或框架的內容窗格中:

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

面板不會顯示在Swing中。他們必須被添加到窗口。創建JFrame或JDialog並將其添加到它。