Java应用程序:运行前获取输入
public class checkUsernames extends JFrame {
private static JTextArea textArea1;
private static JButton button1;
private static JScrollPane scrollPane1;
private static JTextField textField1;
private static JPasswordField passwordField1;
private static JLabel label3;
private static JButton button2;
private static JLabel label1;
private static JLabel label2;
public checkUsernames() {
initComponents();
}
public static void main(String[] args) throws IOException {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Nimbus isn't available");
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
checkUsernames GUI = new checkUsernames();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
String username = textField1.toString();
String password = passwordField1.toString();
线程“main”中的异常 java.lang.NullPointerException
checkUsernames
是唯一的类。当我尝试运行应用程序时,程序将进一步执行(无需字符串用户名和字符串密码即可继续),而无需等待输入。我该如何解决这个问题?
public class checkUsernames extends JFrame {
private static JTextArea textArea1;
private static JButton button1;
private static JScrollPane scrollPane1;
private static JTextField textField1;
private static JPasswordField passwordField1;
private static JLabel label3;
private static JButton button2;
private static JLabel label1;
private static JLabel label2;
public checkUsernames() {
initComponents();
}
public static void main(String[] args) throws IOException {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Nimbus isn't available");
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
checkUsernames GUI = new checkUsernames();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
String username = textField1.toString();
String password = passwordField1.toString();
Exception in thread "main" java.lang.NullPointerException
checkUsernames
is the only class. When I try to run the application the program executes further (proceeding without String username & String password) without waiting for input. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
invokeLater 稍后将调用该代码。它保证 textfield1.toString() 在 checkUserNames() 之后执行
Well invokeLater will invoke the code later. It dosent gurantee textfield1.toString() to execute after checkUserNames()
您的私有静态成员似乎根本没有被初始化。在使用之前初始化创建的每个对象之前,您不会遇到 NullPointerException。
我不知道你在说什么。您必须对所有这些对象执行类似的操作:
您不是输入用户名或密码;而是输入用户名或密码。您正在创建一个文本区域 UI 元素,当您输入它们时,该元素可以接受它们。
None of your private static members appear to be initialized at all. You won't get past the NullPointerException until you initialize each and every object you create before you use it.
I have no idea what you're talking about. You have to do something like this for all those objects:
You aren't entering a username or password; you're creating a text area UI element that can accept them when you do enter them.
为什么不使用 SwingUtilities.invokeAndWait() 而是?这似乎可以立即解决您的问题。
这是一个简单的例子:
Why dont you use SwingUtilities.invokeAndWait() instead? This seems to solve your problem immediately.
Here is the simple example:
如果您希望代码执行在等待输入时停止,请将
JFrame
设为模态JDialog
或JOptionPane
。If you want the code execution to stop while waiting for input, make the
JFrame
a modalJDialog
orJOptionPane
instead.