2013-08-16 61 views
1

我正在學習使用JFrame,以便我可以開始構建一些應用程序,但是,我的面板顯示組件時遇到了很多麻煩。到目前爲止我創建的代碼只是練習並感受實際的JFrame庫。如何讓JFrame顯示我的面板?

這裏是我的代碼:

import java.awt.Dimension; 
    import java.awt.Toolkit; 
    import javax.swing.*; 

    public class GUI extends JFrame { 

    public static void main(String[] args){ 
     new GUI();   
    } 

    /* 
    * This is the constructor of the GUI. It sets the dimension, 
    * the visibility, and the positioning of the frame relative 
    * to the users dimension of their screen size. 
    */ 
    public GUI(){ 
     //'this' is in reference to the frame being created 
     this.setSize(500, 400); 
     //this.setLocationRelativeTo(null); 
     this.setVisible(true); 

     Toolkit tk = Toolkit.getDefaultToolkit(); 
     Dimension dim = tk.getScreenSize();  //will hold height and width 

     //Grabs the height and width positioning 
     //of the screen and centers the window 
     int xPos = (dim.width /2) - (this.getWidth()/2); 
     int yPos = (dim.height /2) - (this.getHeight()/2); 

     this.setLocation(xPos, yPos); 

     //This prevents someone from resizing the frame 
     //By default this method is true. 
     this.setResizable(false); 

     //Sets how the frame will close 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Sets the title of the frame 
     this.setTitle("Woot I created a frame!"); 

     /* 
     * The panel is used to hold all of the different 
     * labels and components within the frame. 
     */ 
     JPanel thePanel = new JPanel(); 

     JLabel label1 = new JLabel("Tell me something"); 
     label1.setToolTipText("Click if you need help"); 

     thePanel.add(label1); 

     //Creates a button 
     JButton button1 = new JButton("OK"); 
     button1.setBorderPainted(true); 
     button1.setContentAreaFilled(true); 
     button1.setToolTipText("This is my button"); 
     thePanel.add(button1); 

     //Creates a text field 
     JTextField txtField = new JTextField("Type here", 15); 

     txtField.setToolTipText("It's a field"); 
     //Adds txtField to the Frame 
     thePanel.add(txtField); 

     //Adds thePanel to the Frame originally created 
     this.add(thePanel); 
    }  
} 

當我運行它,會出現一個顯示窗口,但我沒有看到文本字段。當我註釋掉文本字段時,按鈕和標籤就會像他們應該顯示的那樣出現。

回答

4

你的問題是,你調用調用setVisible(真)已將您的組件添加到GUI ,所以GUI沒有顯示出後來添加的組件。

建議:

  • 避免空佈局和setBounds(...)
  • 不要叫setVisible(true)直到加入到JFrame所有組件。
  • 避免setSize()。讓GUI及其組件大小自己。

因此,這本身並解決您的問題:

public GUI() { 
    this.setSize(500, 400); 
    // this.setVisible(true); 

    // ......... 

    txtField.setToolTipText("It's a field"); 
    thePanel.add(txtField); 
    this.add(thePanel); 

    setVisible(true); 

} 

例如,

import java.awt.Dimension; 
import javax.swing.*; 

public class Gui2 extends JPanel { 
    private static final int PREF_W = 500; 
    private static final int PREF_H = 400; 

    public Gui2() { 
     JLabel label1 = new JLabel("Tell me something"); 
     label1.setToolTipText("Click if you need help"); 

     add(label1); 
     JButton button1 = new JButton("OK"); 
     button1.setBorderPainted(true); 
     button1.setContentAreaFilled(true); 
     button1.setToolTipText("This is my button"); 
     add(button1); 
     JTextField txtField = new JTextField("Type here", 15); 

     txtField.setToolTipText("It's a field"); 
     add(txtField); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private static void createAndShowGui() { 
     Gui2 mainPanel = new Gui2(); 

     JFrame frame = new JFrame("Woot I created a frame!"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.setResizable(false); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

謝謝!這解決了我的問題! – JSCOTT12

+1

@ JSCOTT12:你很受歡迎! –