过去3天我一直试图解决这个问题:/
我有一个类Login(),它创建一个JFrame对象
另一个名为LoginTab()的类,它有一个带有JFrame-object作为参数的构造函数。
我想在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();
}
});
}
}
答案 0 :(得分:2)
要在另一个对象上调用方法,您需要对该另一个对象的引用。我看到你的代码遇到的一个问题是无处(我无论如何都能看到)是否有对你当前可视化的Login对象的引用。要解决此问题,您需要获取此引用,可能通过构造函数参数或setter方法,然后关闭JFrame。
顺便说一下,不得不以这种方式关闭一个JFrame中间程序。您确定不应该使用模态对话框(如JDialog)来代替JFrame吗?
此外,您的代码显示了许多GUI组件,但它们在哪里添加到顶级窗口并显示?
修改强>
另请注意,您可以通过SwingUtilities方法获取封闭的顶级窗口:
SwingUtilities.getWindowAncestor(...);
答案 1 :(得分:2)
从Closing an Application查看ExitAction
。 ExitAction类将找到活动窗口,然后向该窗口发送关闭请求。所以这个类可以用来关闭任何窗口。
答案 2 :(得分:2)
您需要将window
引用为final
才能在匿名类ActionListener
内访问它:
public void panelMethod(final JFrame window) {
// obmitted code
}
然后:
loginBtn.addActionListener(new ActionListener() {
// obmitted coce
if (count == 1) {
JOptionPane.showMessageDialog(null, "User Found, Access Granded!"); //TODO String
window.setVisible(false);
window.dispose();
new ButtonPanel();
}
// ...
}
我建议您阅读Core Java Volumn I,以了解开发Java应用程序以及使用Swing组件的好方法。