我正在尝试制作一个将Swing用于GUI的程序,但是运行以下代码时打开的表单为空。但是,我仍然可以获得有关未见对象的信息(已通过btnLogin.getText()测试)。我是否缺少某些东西,或者我做错了什么?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class loginForm extends JFrame implements ActionListener{

public static void main(String[] args){
    new loginForm();
}

public loginForm() {
    System.out.println("Starting...");
    pack();
    initComponents();
    System.out.println("Opened");
    this.setVisible(true);
}

private void initComponents() {


    loginForm = new JFrame();
    lblUserID = new JLabel();
    existingUserIDTextField = new JTextField();
    lblPassword = new JLabel();
    existingUserPasswordField = new JPasswordField();
    btnLogin = new JButton();
    btnLogin.addActionListener(this);
    lblWelcome = new JLabel();

    //======== loginForm ========
    {
        loginForm.setResizable(false);
        loginForm.setTitle("Library Reservation System");
        Container loginFormContentPane = loginForm.getContentPane();

        //---- lblUserID ----
        lblUserID.setText("User ID:");
        lblUserID.setFont(new Font("Tahoma", Font.PLAIN, 16));

        //---- lblPassword ----
        lblPassword.setText("Password:");
        lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));

        //---- btnLogin ----
        btnLogin.setText("Login");

        //---- lblWelcome ----
        lblWelcome.setText("Welcome to our Library Reservation System");
        lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 16));

        GroupLayout loginFormContentPaneLayout = new GroupLayout(loginFormContentPane);
        loginFormContentPane.setLayout(loginFormContentPaneLayout);
        loginFormContentPaneLayout.setHorizontalGroup(
            loginFormContentPaneLayout.createParallelGroup()
                .addGroup(loginFormContentPaneLayout.createSequentialGroup()
                    .addGroup(loginFormContentPaneLayout.createParallelGroup()
                        .addGroup(loginFormContentPaneLayout.createSequentialGroup()
                            .addGap(199, 199, 199)
                            .addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE))
                        .addGroup(loginFormContentPaneLayout.createSequentialGroup()
                            .addGap(72, 72, 72)
                            .addGroup(loginFormContentPaneLayout.createParallelGroup()
                                .addComponent(lblUserID, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblPassword))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(existingUserIDTextField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                                .addComponent(existingUserPasswordField, GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)))
                        .addGroup(GroupLayout.Alignment.TRAILING, loginFormContentPaneLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 318, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(118, Short.MAX_VALUE))
        );
        loginFormContentPaneLayout.setVerticalGroup(
            loginFormContentPaneLayout.createParallelGroup()
                .addGroup(loginFormContentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblWelcome, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
                    .addGap(11, 11, 11)
                    .addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblUserID)
                        .addComponent(existingUserIDTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(loginFormContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(lblPassword)
                        .addComponent(existingUserPasswordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(btnLogin)
                    .addContainerGap(12, Short.MAX_VALUE))
        );
        loginForm.pack();
        loginForm.setLocationRelativeTo(loginForm.getOwner());
    }

}

private JFrame loginForm;
private JLabel lblUserID;
private JTextField existingUserIDTextField;
private JLabel lblPassword;
private JPasswordField existingUserPasswordField;
private JButton btnLogin;
private JLabel lblWelcome;

}

最佳答案

包含控件的框架从不显示。

loginForm.setVisible(true);


不用外部冗余框架类,您可以做

public class LoginFormApp extends JFrame  {

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

            @Override
            public void run() {
                LoginFormApp loginFormApp = new LoginFormApp();
                loginFormApp.initComponents();
            }
        });

       .....
    }


如前所述,其中initComponents使loginForm可见。

关于java - JFrames加载空白,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29570802/

10-11 02:21