2013-04-13 49 views
0

我正在嘗試使用網格佈局來製作GUI窗口。我添加了所有的組件並編譯,但運行時它不顯示任何內容。我試圖做一個簡單的佈局分組和堆疊像這樣。我的GUI窗口不顯示任何內容

{introduction message} 
{time label 
time input text} 
{gravity label 
gravity input text} 
{answer label 
answer text box} 
{calculate button clear button} 

這裏是我的代碼

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

public class TurnerRandyFallingGUI extends JFrame 
{ 
final int WINDOW_HEIGHT=500; 
final int WINDOW_WIDTH=500; 

public TurnerRandyFallingGUI() 
{ 
    setTitle("Falling Distance Calculator"); 

    setSize(WINDOW_WIDTH,WINDOW_HEIGHT); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    setLayout(new GridLayout(1, 5)); 

    //labels 
    JLabel introMessage = new JLabel("Welcome to the Falling distance"+ 
             "calculator"); 
    JLabel timeLabel = new JLabel("Please enter the amount of time "+ 
       "in seconds the object was falling."); 
    JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+ 
        "forced onto the object"); 
    JLabel answerLabel = new JLabel("Answer"); 

    //text fields 
    JTextField fTime = new JTextField(10); 
    JTextField gForce = new JTextField(10); 
    JTextField answerT = new JTextField(10); 

    //buttons 
    JButton calculate = new JButton("Calculate"); 
    JButton clr = new JButton("clear"); 

    //panels 
    JPanel introP = new JPanel(); 
    JPanel timeP = new JPanel(); 
    JPanel gravityP = new JPanel(); 
    JPanel answerP = new JPanel(); 
    JPanel buttonsP = new JPanel(); 

    //adding to the panels 
    //intro panel 
    introP.add(introMessage); 
    //time panel 
    timeP.add(timeLabel); 
    timeP.add(fTime); 
    //gravity panel 
    gravityP.add(gravityLabel); 
    gravityP.add(gForce); 
    //answer panel 
    answerP.add(answerLabel); 
    answerP.add(answerT); 
    //button panel 
    buttonsP.add(calculate); 
    buttonsP.add(clr); 




    setVisible(true); 
} 
    public static void main(String[] args) 
    { 
     new TurnerRandyFallingGUI(); 
    } 
} 

回答

2

你沒有到你上面的類擴展JFrame中添加。您需要將組件添加到其層次最終導致頂層窗口的容器,如果您願意,也可以將其添加到this。換句話說,在上面的代碼中沒有add(someComponent)或功能相似的this.add(someComponent)方法調用。

  • 考慮添加所有JPanels到一個單一的JPanel
  • 考慮添加的JPanel到JFrame實例,它是你的類調用add(thatJPanel)
  • 更好的辦法是不擴展JFrame,只是在需要時創建一個JFrame,但這可能會成爲另一次討論的主題。
2

之前setVisible (true)語句添加以下語句:

add (introP); 
add (timeP); 
add (gravityP); 
add (answerP); 
add (buttonsP); 
+0

這將不起作用,因爲它不考慮JFrame的默認BorderLayout。 –

+0

我認爲所有的面板都會根據GridLayout添加到JFrame中,一行五列 – olyanren

+0

當你看上面的代碼時,你可以看到使用setLayout(new GridLayout(1,5))語句。如果您不指定佈局,JFrame的默認佈局是BorderLayout – olyanren

2

有沒有在你的JFrame。這就是原因所在

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

public class TurnerRandyFallingGUI extends JFrame 
{ 
final int WINDOW_HEIGHT=500; 
final int WINDOW_WIDTH=500; 

public TurnerRandyFallingGUI() 
{ 


    //labels 
    JLabel introMessage = new JLabel("Welcome to the Falling distance"+ 
             "calculator"); 
    JLabel timeLabel = new JLabel("Please enter the amount of time "+ 
       "in seconds the object was falling."); 
    JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+ 
        "forced onto the object"); 
    JLabel answerLabel = new JLabel("Answer"); 

    //text fields 
    JTextField fTime = new JTextField(10); 
    JTextField gForce = new JTextField(10); 
    JTextField answerT = new JTextField(10); 

    //buttons 
    JButton calculate = new JButton("Calculate"); 
    JButton clr = new JButton("clear"); 

    //panels 
    JPanel introP = new JPanel(); 
    JPanel timeP = new JPanel(); 
    JPanel gravityP = new JPanel(); 
    JPanel answerP = new JPanel(); 
    JPanel buttonsP = new JPanel(); 

    //adding to the panels 
    //intro panel 
    introP.add(introMessage); 
    //time panel 
    timeP.add(timeLabel); 
    timeP.add(fTime); 
    //gravity panel 
    gravityP.add(gravityLabel); 
    gravityP.add(gForce); 
    //answer panel 
    answerP.add(answerLabel); 
    answerP.add(answerT); 
    //button panel 
    buttonsP.add(calculate); 
    buttonsP.add(clr); 

    setLayout(new GridLayout(5, 1)); 

    this.add(introP); 
    this.add(timeP); 
    this.add(gravityP); 
    this.add(answerP); 
    this.add(buttonsP); 


setTitle("Falling Distance Calculator"); 

    this.pack(); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



    setVisible(true); 
    this.validate(); 
} 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TurnerRandyFallingGUI(); 
      } 
     }); 

    } 
} 

考慮以下

  • 在網格佈局,第一個參數是,二是
  • 決不手動設置JFrame的大小。使用pack()方法決定 的大小
  • 使用SwingUtilities.InvokeLater()在另一個線程中運行GUI。