2013-05-09 101 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