从另一个类访问 JTextfield 中的值
有人可以检查我的代码是否有问题,因为当我调用 getter 方法时它什么也不显示。不知何故,它不存储我尝试使用的文本字段的输入。
以下是一些代码:
A 类的登录按钮
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("1"+getuser());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/devweb", "root", "123456");
PreparedStatement statement = con.prepareStatement("select User,pwd from account");
ResultSet rs = statement.executeQuery();
name = user.getText();
String pwd = jTextField2.getText();
while (rs.next()) {
if (rs.getString("User").equals(name) && rs.getString("pwd").equals(pwd)) {
result now = new result();
now.res();
dispose();
System.out.println("2"+getuser());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
然后是 B 类的视图按钮:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
log qwe=new log();
jTextField1.setText(qwe.getuser());
System.out.println(qwe.getuser());//when I print this it just says null
}
注意:此时我只想看看它是否能够从登录页面获取输入,这就是我的代码的原因所做的只是打印输入的用户名。
Can someone check my code if there is something wrong because when I call the getter method it displays nothing. Somehow it does not store the inputs from the text field that I am trying to use.
here are some of codes:
Login Button from class A
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("1"+getuser());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/devweb", "root", "123456");
PreparedStatement statement = con.prepareStatement("select User,pwd from account");
ResultSet rs = statement.executeQuery();
name = user.getText();
String pwd = jTextField2.getText();
while (rs.next()) {
if (rs.getString("User").equals(name) && rs.getString("pwd").equals(pwd)) {
result now = new result();
now.res();
dispose();
System.out.println("2"+getuser());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Then this the view button from class B:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
log qwe=new log();
jTextField1.setText(qwe.getuser());
System.out.println(qwe.getuser());//when I print this it just says null
}
note: at this point I just want to see if it will be able to get the input from the log in page that is why what my code does is just print the inputted user name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有问题:您使用了静态。 Getter 方法应该是实例方法,因此不应该涉及静态。
我经常在此类代码中看到的另一个问题是您何时尝试提取文本?它是事件驱动的并且仅在将文本添加到 JTextField 之后才调用吗?或者,当用户还没有机会输入文本时,您是否尝试在程序启动时调用此方法?很难说这是否会影响您,因为我们对您的代码和问题的了解有限。
另一个并不罕见的问题是尝试从阴影对象而不是当前显示的实际对象中提取信息。您的第二个代码窗口表明这可能是一个问题,因为您在尝试从中提取信息之前立即创建了一个日志对象。这可能是第二个日志对象,并且根本无法显示。
要了解这些问题是否影响您,或者您是否需要更多或更深入的建议,您可能希望显示更多代码。
编辑2
关于您最近的编辑,如果日志是模式对话框,您的代码可以工作,但仍然很难说,因为您添加的代码为我们提供了一些信息,但仍然不够。不要灰心,因为有一门艺术可以决定什么是没有足够的信息/代码在此处发布,什么是足够的信息,什么是太多的信息。通过练习你会在这方面做得更好。
Yes, there's something wrong: your use of static. Getter methods should be instance methods and so there should be no statics involved.
The other issue I often see with this sort of code is when are you trying to extract the text? Is it event driven and only called after text has been added to the JTextField? Or do you try to call this method on program start-up when the user hasn't had a chance to enter text yet? It's hard to say if this affects you though as we only have a limited view of your code and problem.
Another issue that is not uncommon is trying to extract information from a shadow object and not the actual object that is currently being displayed. Your second code window suggests that this may be an issue since you create a log object immediately before trying to extract information from it. This could be a second log object and could be not displayed at all.
To see if any of these issues effect you or if you need more or deeper advice, you may wish to show more code.
Edit 2
Regarding your recent edit, your code could work if log is a modal dialog, but still tough to say as your added code gives us some information but still not enough. Don't lose heart as there is an art to deciding what is not enough information/code to post here vs enough information vs. too much information. You will get better at this with practice.