Swing 应用程序未出现

Swing Application not appearing

您好,我正在处理我的 Java 项目,我必须创建一个登录页面供用户输入,当登录成功时,应该会有一个警报告诉他们登录成功。但是,我遇到了一些麻烦,因为我的应用程序根本没有出现,我认为这与我编写的代码有关。下面是代码:

public class UserLoginPage implements ActionListener {
    //Put all JLabels,Frames and buttons here etc
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    JLabel userLabel = new JLabel("Username");
    JLabel passwordLabel = new JLabel("Password");
    JTextField userText = new JTextField();
    JTextField passwordText = new JTextField();
    JButton loginButton = new JButton("Login");

    //Label for successful login
    JLabel success = new JLabel("Login Successful");

    //Default Constructor to add the frames and panels etc
    public UserLoginPage(){
        panel.setLayout(null);
        userLabel.setBounds(10,20,80,25);
        panel.add(userLabel);
        passwordLabel.setBounds(10,50,80,25);
        panel.add(passwordLabel);

        userText.setBounds(100,20,165,25);
        panel.add(userText);
        passwordText.setBounds(100,50,165,25);
        panel.add(passwordText);

        loginButton.setBounds(10,80,80,25);
        loginButton.addActionListener(new UserLoginPage());
        panel.add(loginButton);

        success.setBounds(10,110,300,25);
        panel.add(success);
        //success.setText();

        frame.setSize(500,500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(panel);
    }


    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UserLoginPage();
            }
        });
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button Clicked");
    }

我认为问题出在loginButton.addActionListener(new UserLoginPage());,但我可能错了,请告诉我如何解决这个问题,谢谢。

你是对的。 loginButton.addActionListener(new UserLoginPage()) 导致 java.lang.WhosebugError。构造函数总是在没有默认基本情况的情况下调用自身。您应该将 UserLoginPage 的那个实例作为参数传递,而不是新实例。

改用此代码:

loginButton.addActionListener(this);

您的程序中存在多个问题:

  1. panel.setLayout(null);.setBounds(...)。您不应该使用 null-layouts,因为 Swing 必须处理多个 PLAF、OS、屏幕尺寸和分辨率。您最终可能会遇到 . Pixel-perfect layouts might seem like the easiest way to create complex UIs but it's not, it'll just lead you to endless issues. Instead use layout managers 或它们的组合这样的问题。

  2. loginButton.addActionListener(new UserLoginPage()); 你每次都在创建程序的新实例,并且在它的每个实例上你都在创建一个新对象,因为你的所有代码都在构造函数中。只是,不要!这是一个递归调用,最终创建了一个java.lang.WhosebugError,解决这个使用this而不是new UserLoginPage()

  3. frame.setVisible(true); 此行应始终是程序中的最后一行,在您将所有内容添加到 JFrame 之后,而不是之前。

根据上述建议,这里是您的代码的更新版本:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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 UserLoginPage implements ActionListener {
    // Put all JLabels,Frames and buttons here etc
    private JPanel panel;
    private JFrame frame;
    private JLabel userLabel;
    private JLabel passwordLabel;
    private JTextField userText;
    private JTextField passwordText;
    private JButton loginButton;

    // Label for successful login
    private JLabel success;

    // Default Constructor to add the frames and panels etc
    public UserLoginPage() {
        
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        panel  = new JPanel();
        
        panel.setLayout(new GridLayout(0, 2));
        
        userLabel = new JLabel("Username");
        passwordLabel = new JLabel("Password");
        
        userText = new JTextField(10);
        passwordText = new JPasswordField(10);
        
        loginButton = new JButton("Login");
        success = new JLabel("Login Successful");
        
        loginButton.addActionListener(this);
        
        panel.add(userLabel);
        panel.add(userText);
        panel.add(passwordLabel);
        panel.add(passwordText);
        panel.add(loginButton);
        
        frame.add(panel);
        frame.add(success, BorderLayout.SOUTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UserLoginPage().createAndShowGUI();;
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button Clicked");
    }
}