我有一个登录表单,它是JDialog
类的实例。但它没有出现在JFrame
内。我之前在Application
内实现了它作为一种方法并且它起作用了。但是在将它包装到Login
类中之后它不起作用也没有任何错误。有什么问题?
public class Application extends JFrame {
JDialog loginForm = null;
public Application() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setMinimumSize(new Dimension(800, 400));
setVisible(true);
loginForm = (JDialog) new Login();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.alee.laf.WebLookAndFeel");
WebLookAndFeel.setDecorateDialogs(true);
} catch (Exception e) {
}
Application app = new Application();
}
}
public class Login extends JDialog {
private JButton loginButton = null;
private JButton cancelButton = null;
private JTextField userNameField = null;
private JPasswordField userPassField = null;
public void Login() {
//...
//...
setSize(new Dimension(300, 200));
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("Login");
setVisible(true);
setAlwaysOnTop(true);
}
class EventHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = userNameField.getText();
String password = Security.getSha256(userPassField.getText());
if(User.login(username, password)) {
// Login Successful
} else {
// Login Failed. Alert error
}
} else if(e.getSource() == cancelButton) {
System.exit(0);
}
}
}
}
答案 0 :(得分:2)
只需更改
public void Login()
到
public Login()
Login
不是方法,它是构造函数。