2012-08-13 58 views
0

沒有錯誤,但是當我運行它時,我添加到JPanel中的內容不會出現,只有不在JPanel內部的內容出現。JPanel中的內容不會出現

import javax.swing.*; 

import java.awt.*; 

public class SimpleGUI extends JFrame 
{ 

     public static void main(String arg[]) 
     { 
       SimpleGUI f = new SimpleGUI("GUI components"); 
       f.setSize(600,200); 
       f.setVisible(true); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     } 
     SimpleGUI(String s) 
     { 


       setTitle(s); 
       setLayout(new GridLayout(3,2)); 

       JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!"); 
       JButton bt = new JButton("OK"); 

       JLabel lb = new JLabel ("Enter your name:"); 
       JTextField tf = new JTextField("<type name here>"); 

       JLabel lb2 = new JLabel ("Enter age:"); 
       JTextField tf2= new JTextField(10); 
       tf2.setHorizontalAlignment(JTextField.RIGHT); 

       JCheckBox cb = new JCheckBox("Bold",true); 
       JRadioButton rb1 = new JRadioButton("Red"); 

       JTextArea ta = new JTextArea(5,20); 
       JList list = new JList(new Object[] {"Block A", "Block B"}); 
       JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"}); 

       ImageIcon ic = new ImageIcon("music.gif"); 
       JButton newbt = new JButton("Play",ic); 
       newbt.setVerticalTextPosition(JButton.TOP); 
       newbt.setHorizontalTextPosition(JButton.CENTER); 

       JPanel p1 = new JPanel(); 
       p1.setLayout(new BorderLayout()); 
       p1.add(lb, BorderLayout.WEST); 
       p1.add(tf, BorderLayout.CENTER); 
       p1.add(cb, BorderLayout.EAST); 

       JPanel p2 = new JPanel(); 
       p2.setLayout(new BorderLayout()); 
       p2.add(lb2, BorderLayout.WEST); 
       p2.add(tf2, BorderLayout.CENTER);     
       p2.add(rb1, BorderLayout.EAST); 

       JPanel p3 = new JPanel(); 
       p3.setLayout(new BorderLayout()); 
       p3.add(jcb); 
       add(ta); 
       add(list); 
       p3.add(newbt, BorderLayout.NORTH); 
       add(msg); 
       p3.add(bt, BorderLayout.SOUTH); 
     } 
} 
+1

你是否嘗試添加'p1'到框架? 'add(p1);' – MadProgrammer 2012-08-13 04:39:13

+1

您需要調用'pack()'而不是'setSize()'。 – trashgod 2012-08-13 04:47:07

回答

0

你在哪裏添加面板到框架?另外,忘記了我的java「規則和條例」:你需要調用「super()」嗎?

3

我已更新您的代碼。看看這個版本:

import javax.swing.*; 

import java.awt.*; 

public class SimpleGUI extends JFrame { 

    public static void main(String arg[]) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SimpleGUI f = new SimpleGUI("GUI components"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      } 

     }); 

    } 

    public SimpleGUI(String s) { 


     setTitle(s); 
     setLayout(new GridLayout(3, 2)); 

     JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!"); 
     JButton bt = new JButton("OK"); 

     JLabel lb = new JLabel("Enter your name:"); 
     JTextField tf = new JTextField("<type name here>"); 

     JLabel lb2 = new JLabel("Enter age:"); 
     JTextField tf2 = new JTextField(10); 
     tf2.setHorizontalAlignment(JTextField.RIGHT); 

     JCheckBox cb = new JCheckBox("Bold", true); 
     JRadioButton rb1 = new JRadioButton("Red"); 

     JTextArea ta = new JTextArea(5, 20); 
     JList list = new JList(new Object[]{"Block A", "Block B"}); 
     JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"}); 

     ImageIcon ic = new ImageIcon("music.gif"); 
     JButton newbt = new JButton("Play", ic); 
     newbt.setVerticalTextPosition(JButton.TOP); 
     newbt.setHorizontalTextPosition(JButton.CENTER); 

     JPanel p1 = new JPanel(); 
     p1.setLayout(new BorderLayout()); 
     p1.add(lb, BorderLayout.WEST); 
     p1.add(tf, BorderLayout.CENTER); 
     p1.add(cb, BorderLayout.EAST); 

     JPanel p2 = new JPanel(); 
     p2.setLayout(new BorderLayout()); 
     p2.add(lb2, BorderLayout.WEST); 
     p2.add(tf2, BorderLayout.CENTER); 
     p2.add(rb1, BorderLayout.EAST); 

     JPanel p3 = new JPanel(); 
     p3.setLayout(new BorderLayout()); 
     p3.add(jcb); 
     add(ta); 
     add(list); 
     p3.add(newbt, BorderLayout.NORTH); 
     add(msg); 
     p3.add(bt, BorderLayout.SOUTH); 

     /** 
     * Need to add the following lines 
     */ 
     this.add(p1); 
     this.add(p2); 
     this.add(p3); 

     this.pack(); 
     this.setVisible(true); 
    } 
} 

幾個指針:

  • 您需要addcomponentsJFrame爲他們實際顯示。

  • 對用戶界面的任何更新都必須發生在event dispatch thread上。因此,您會注意到我已將一個SwingUtilites.invokeLater()添加到main。看看這篇文章,瞭解"Threading with Swing"

+0

考慮省略'setSize()'並且只調用一次'setVisible()'。用於'invokeLater()'的+1。 – trashgod 2012-08-13 04:59:50

+1

woops,我沒有在'main()'中看到'setVisible()'。感謝您指出了這一點! – Sujay 2012-08-13 05:02:24

+1

不應該加入到ContentPane嗎?即this.getContentPane()。添加(...) – 2012-08-13 06:17:27