0

我一直有一些 JPanel 没有出现的问题(可能是因为所有东西都是在摆动中穿线的?)。我已经尝试在添加位置重新排序,添加SwingUtilities.invokeLater(...);但仍然没有工作。在谷歌上搜索,他们中的大多数人只是说使用 invokeLater 函数,这对我不起作用。我假设它不需要布局管理器,因为大小和位置是由 .SetBounds() 完成的?这是我目前得到的代码

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 显示... 只有 jlabel 显示...

关于为什么它仍然没有显示的任何建议都会很好。谢谢。

4

2 回答 2

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

问候。

于 2013-05-09T16:15:54.457 回答
0

您需要将面板添加到框架中

JFrame frame = new JFrame();
 frame.add(form);
于 2013-05-09T16:08:25.463 回答