gpt4 book ai didi

java - JPanel 类未添加到 JFrame

转载 作者:行者123 更新时间:2023-12-02 01:19:33 25 4
gpt4 key购买 nike

我无法从主类获取 JFrame 以显示另一个类的 JPanel。一切编译都没有错误这是我扩展 JFrame 的主类代码:

public OnlineCarSalesSystem(){
setTitle("Online Car Sales System");
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(new Login());
}

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

在上面的代码中,我添加了 add(new Login()); 但它没有在我的 JFrame 上显示该面板。在下面的代码中,我使用 JPanel 扩展了我的类。这是 JPanel 类代码:

import java.awt.Color;
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;

public class Login extends JPanel{
JLabel loginLabel = new JLabel("Login ID");
JLabel passwordLabel = new JLabel("Password");
JTextField loginTextField = new JTextField();
JPasswordField passwordTextField = new JPasswordField();
JButton submitButton = new JButton("Submit");
JButton registration = new JButton("new Registration");
JLabel noaccountLabel = new JLabel("No Account yet!!!");
public void Login(){
setBounds(0,0,500,500);
setBackground(Color.red);
setVisible(true);
loginLabel.setBackground(Color.cyan);
passwordLabel.setBackground(Color.cyan);
loginTextField.setBounds(680, 103,90,20);
add(loginTextField);
loginLabel.setBounds(600, 100,90,30);
add(loginLabel);
passwordTextField.setBounds(680, 153,90,20);
passwordTextField.setEchoChar('*');
add(passwordTextField);
passwordLabel.setBounds(600, 150,90,30);
add(passwordLabel);
add(submitButton);
submitButton.setBounds(640,200,90,30);
submitButton.addActionListener(new ActionListener() { //////Submit Button
public void actionPerformed(ActionEvent e) {

}
});
add(registration);
registration.setBounds(638,270,96,30);
add(noaccountLabel);
noaccountLabel.setBackground(Color.cyan);
noaccountLabel.setBounds(640,250,90,30);
registration.addActionListener(new ActionListener() { //////registration Button
public void actionPerformed(ActionEvent e) {

}
});
}
}

最佳答案

问题是 Login() 函数在代码中的任何位置都不会执行。您可能想要更改

public void Login() { ... }

public Login() { ... }

因此代码在对象初始化时执行

关于java - JPanel 类未添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com