过去 3 天我一直在尝试解决这个问题:/
我有一个类 Login() 创建一个 JFrame 对象
另一个名为 LoginTab() 的类,它有一个以 JFrame 对象作为参数的构造函数。
我想在 LoginTab() 类中使用 Button 处理 JFrame。但是 addActionListener 不接受 JFrame 对象,我不知道为什么:(
LoginTab() 的代码:
package tabs;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import abstractClasses.JTextFieldImmo;
import abstractClasses.ValidateInput;
import programs.MySQL;
import windows.ButtonPanel;
import windows.UserDetails;
public class LoginTab extends ValidateInput {
/**
*
*/
private static final long serialVersionUID = 1L;
static Locale locale = new Locale("de");
static ResourceBundle r = ResourceBundle.getBundle("Strings", locale);
// static Connection con = null;
static Statement stnt = null;
static ResultSet rs = null;
public LoginTab(JFrame window) {
panelMethod(window);
}
// LOGIN ITEMS
JLabel usernameLbl = new JLabel(r.getString("username"));
JLabel passwordLbl = new JLabel(r.getString("password"));
JTextFieldImmo usernameFld = new JTextFieldImmo(10);
JPasswordField passwordFld = new JPasswordField(10);
JButton loginBtn = new JButton(r.getString("login"));
JButton registerUserBtn = new JButton("Neuer Benutzer"); // TODO String
// einfügen
public void panelMethod(JFrame window) {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Insets
c.insets = new Insets(4, 5, 0, 0);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
this.add(usernameLbl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
this.add(usernameFld, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
this.add(passwordLbl, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
this.add(passwordFld, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
this.add(loginBtn, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
this.add(registerUserBtn, c);
// Actions Listener
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
MySQL.connect();
String username = usernameFld.getText().trim();
String password = String.valueOf(passwordFld.getPassword())
.trim();
String sql = "SELECT username,password from per_user where username = '"
+ username + "'and password = '" + password + "'";
stnt = MySQL.getCon().createStatement();
rs = stnt.executeQuery(sql);
int count = 0;
while (rs.next()) {
count = count + 1;
}
if (count == 1) {
JOptionPane.showMessageDialog(null, "User Found, Access Granded!"); //TODO String
//window.setVisible(false);
//window.dispose();
new ButtonPanel();
} else if (count > 1) {
JOptionPane.showMessageDialog(null,
"Duplicate User, Access Denied!"); // TODO String einfügen
} else {
JOptionPane.showMessageDialog(null, "User not Found"); // TODO String einfügen
}
} catch (Exception e1) {
// JOptionPane.showMessageDialog(null,
// "Es konnte keine Verbindung zum MySQL Server hergestellt werden.");
e1.printStackTrace();
}
}
});
registerUserBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new UserDetails();
}
});
passwordFld.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
loginBtn.doClick();
}
});
}
}