[英]JPanel class is not adding to JFrame
我无法从主类获取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() { ... }
因此代码在对象初始化时执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.