2013-05-07 78 views
1

我對JFrame相當陌生,我想知道爲什麼我的項目沒有顯示在窗口上。我知道我沒有ActionHandler,但我只想讓我的文本框顯示在我的窗口中。這裏是我的代碼:爲什麼我的項目沒有顯示在JFrame中?

import java.awt.Font; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

public class FirstGUI extends JFrame{ 
    public void GUI(){ 
     setTitle("Welcome"); 
     setResizable(false); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     setSize(600,600); 

     JLabel title = new JLabel(); 
     title.setText("Apple Inc. Member Login Port"); 
     title.setFont(new Font("Arial", Font.PLAIN, 24)); 

     JTextField login = new JTextField("Login",10); 

     JPasswordField pass = new JPasswordField("Password"); 

     add(title); 
     add(login); 
     add(pass); 

    } 

    public static void main(String[] args){ 
     FirstGUI a = new FirstGUI(); 
     a.GUI(); 
    } 
} 

但是當我運行它,我得到這個:

enter image description here

+0

我相信你需要添加你的組件到'context pane'而不是直接添加到框架。請參閱http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html – 2013-05-07 00:58:50

+0

@ PM77-1我相信自Java 5以來,不再需要將「JFrame#add」委託給您的內容窗格 – MadProgrammer 2013-05-07 01:20:32

回答

6

但是當我運行它,我得到這個:

,因爲你添加組件到幀後的幀是可見的你會得到一個空白屏幕。

  1. 正如已經建議您需要使用適當的佈局管理器。 FlowLayout是最簡單的開始。
  2. 調用setVisible(true) AFTER將組件添加到框架後。

因此,代碼應該更像:

panel.add(...); 
panel.add(...); 
add(panel); 
pack(); 
setVisible(true); 
+0

「setVisible」+1 +1 – MadProgrammer 2013-05-07 01:19:47

+0

感謝上帝,有些聰明的人提醒你看看你的代碼行的順序。謝謝你,先生。 – IchHabsDrauf 2017-08-17 18:17:13

0

不要將組件直接添加到您的框架。請將其添加到內容窗格,這是JFrame存儲它繪製的所有組件的位置。通常這是一個JPanel。

下面是一個例子:

public class GUI 
{ 

    private JPanel content; 

    public void GUI 
    { 
     /*Other code*/ 

     content = new JPanel(); 
     add(content); //make content the content pane 

     content.add(title); 
     content.add(login); 
     content.add(pass); 
    } 

如果失敗,呼籲所有的組件setVisible(true)setEnabled(true)

在旁註中,您可能希望使您的GUI函數成爲構造函數。

1

我同意MadProgrammer的建議(+1)

好吧,讓我們來看看在你的程序雖然

你居然已經創建了一個包含組件的JFrame。其優良的工作爲好,但你的「爲什麼我的項目沒有顯示在JFrame中」問題是不是因爲你做錯了什麼,而是因爲錯過了一些東西,即重新驗證()

嘗試:

public static void main(String[] args){ 
     FirstGUI a = new FirstGUI(); 
     a.GUI(); 
     a.revalidate(); 
    } 

我不是說這會給你完美的用戶界面。我想說的是這會幫助你更好地理解Swing。瞭解Swing佈局管理器,然後在您的UI上工作以獲得更好的結果

revalidate():此組件及其上方的所有父級都標記爲需要佈局。這意味着佈局管理器將嘗試重新對齊組件。刪除組件後經常使用。一些真正敏銳的搖擺人可能會錯過這一點。如果你真的在使用Swing,我會認爲你只會知道這一點。

1

的原因只有一個:

setVisible(True); method for the frame should be put on the end of the code. 

如果你給的,當你創建一個框架,該框架的代碼的頂部這一行。這會導致這個問題。

-1
import javax.swing.*; 
import java.awt.*; 
class Myframec extends JFrame 
{ 

    Myframec() 
    { 
     Container c = this.getContentPane(); 
     c.setLayout(null); 

     this.setBounds(10,10,700,500); 
     this.setTitle("Welcome"); 

     this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(); 
     panel.setBounds(0,0,700,500); 
     panel.setBackground(Color.gray); 
     panel.setLayout(null); 
     c.add(panel); 
     Font f = new Font("Arial",Font.BOLD,25); 
     Font f1 = new Font("Arial",Font.BOLD,20); 
     JLabel lable = new JLabel(); 
     lable.setBounds(130,10,400,100); 
     lable.setText("Apple Inc. Member Login Port"); 
     lable.setFont(f); 
     panel.add(lable); 
     JTextField login = new JTextField("Login",10); 
     login.setBounds(120,150,400,30); 
     login.setFont(f1); 
     panel.add(login); 
     JPasswordField pass =new JPasswordField("Password"); 
     pass.setBounds(120,200,400,30); 
     pass.setFont(f1); 


     lable.setFont(f); 
     panel.add(pass); 
     c.setVisible(true); 
     this.setVisible(true); 
    } 
    public static void main(String[] argm) 
    { 
     Myframec frame = new Myframec(); 
     frame.setVisible(true); 
    } 
} 
+3

請添加關於此代碼如何工作的解釋(通過編輯答案)。 – MikaS 2018-01-27 17:20:11

相關問題