2013-05-09 102 views
0

我一直有一些JPanel沒有顯示出來的問題(可能是因爲所有的東西都是在迴路中的線程?)。我試過重新排列它的位置,添加SwingUtilities.invokeLater(...);,但仍然沒有奏效。在谷歌上搜索,他們大多數只是說使用invokeLater函數,這對我不起作用。我假設它不需要佈局管理器,因爲大小和位置是由.SetBounds()完成的?這是我在此刻JPanel沒有顯示,嘗試像invokeLater這樣的事情,仍然沒有顯示

public class MenuLogin extends JPanel{ 

private JTextField txtUsername; 
private JPasswordField txtPassword; 

public MenuLogin(){ 

    setBounds(0, 0, 380, 205); 
    setBackground(Color.WHITE); 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      JLabel lblLogin = new JLabel("Login"); 
      lblLogin.setBounds(155, 5, 85, 42); 
      lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36)); 
      lblLogin.setHorizontalAlignment(SwingConstants.CENTER); 


      JPanel form = new JPanel(); // The panel that won't show 
      form.setBorder(null); 
      form.setBounds(10, 74, 390, 195); 
      add(form); 
      form.setLayout(null); 
      form.setVisible(true); 

      txtUsername = new JTextField(); 
      txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17)); 
      txtUsername.setToolTipText("Username"); 
      txtUsername.setBounds(10, 30, 370, 30); 
      txtUsername.setColumns(16); 
      form.add(txtUsername); 

      txtPassword = new JPasswordField(); 
      txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17)); 
      txtPassword.setToolTipText("Password"); 
      txtPassword.setColumns(16); 
      txtPassword.setBounds(10, 78, 370, 30); 
      form.add(txtPassword); 

      JButton btnProceed = new JButton("Proceed"); 
      btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15)); 
      btnProceed.setBounds(150, 119, 94, 30); 
      form.add(btnProceed); 

      JButton btnPlayAsGuest = new JButton("Play As Guest"); 
      btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9)); 
      btnPlayAsGuest.setBounds(291, 161, 89, 23); 
      form.add(btnPlayAsGuest); 

      JButton btnSignUp = new JButton("Sign Up"); 
      btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13)); 
      btnSignUp.setBounds(155, 160, 83, 23); 
      form.add(btnSignUp); 
      add(lblLogin); 
     } 
     }); 
} 
} 

唯一的一點是,第一個JLabel顯示了代碼... Only the jlabel shows...

任何建議,爲什麼它仍然沒有顯示將是很好。謝謝。

+2

'這是我目前得到的代碼 - 這並沒有什麼幫助,因爲我們不知道你在哪裏/如何實際使用這個組件。 「我認爲它不需要佈局管理器」 - 通常是一個糟糕的假設。大多數問題是由於您不使用佈局管理器而導致的。 Swing旨在與佈局經理一起使用。 – camickr 2013-05-09 16:07:58

+1

不要在'invokeLater'周圍撒上試圖解決問題的方法。像@camickr說的,問題是你沒有佈局管理器,而你的添加(標籤)調用正在有效地破壞較早的添加(JPanel)。 @ brano88的解決方案使用一個'BorderLayout'來解決問題。 – wolfcastle 2013-05-09 16:43:53

回答

4

這應該工作:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Font; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class MenuLogin extends JPanel { 

    private JTextField txtUsername; 
    private JPasswordField txtPassword; 

    public MenuLogin() { 
     setLayout(new BorderLayout()); 
     setBackground(Color.WHITE); 

     JPanel form = new JPanel(); // The panel that won't show 
     form.setVisible(true); 

     JLabel lblLogin = new JLabel("Login"); 
     lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36)); 
     form.add(lblLogin); 

     txtUsername = new JTextField(); 
     txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17)); 
     txtUsername.setToolTipText("Username"); 
     txtUsername.setColumns(16); 
     form.add(txtUsername); 

     txtPassword = new JPasswordField(); 
     txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17)); 
     txtPassword.setToolTipText("Password"); 
     txtPassword.setColumns(16); 
     form.add(txtPassword); 

     JButton btnProceed = new JButton("Proceed"); 
     btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15)); 
     form.add(btnProceed); 

     JButton btnPlayAsGuest = new JButton("Play As Guest"); 
     btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9)); 
     form.add(btnPlayAsGuest); 

     JButton btnSignUp = new JButton("Sign Up"); 
     btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13)); 
     form.add(btnSignUp); 

     add(form, BorderLayout.CENTER); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       frame.add(new MenuLogin()); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

我甚至沒有試圖找出其中的setBounds方法之一引起的問題(因爲我從來沒有使用它們,你也不應該)。我剛剛刪除了setBounds方法並實施了合適的佈局管理器(FlowLayout - 它非常原始)。

當談到一些高級佈局時,我個人更喜歡MigLayout,因爲簡單。 另外,看看GridBagLayout

問候。

+0

感謝您的答案,它工作正常,但它看起來水平? – YepNepDep 2013-05-09 17:08:51

+0

正如我所說的,你必須使用適當的佈局,而不是空的佈局和絕對定位。我將更新我的答案,並提供一些高級版面管理器的鏈接。 – 2013-05-09 17:10:50

0

您需要添加您的面板框架

JFrame frame = new JFrame(); 
frame.add(form); 
+0

'add(form);'包含在創建時......類擴展了一個JPanel ... – YepNepDep 2013-05-09 17:05:12