Java应用程序:运行前获取输入

发布于 2024-11-06 10:12:57 字数 1275 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺孤伤于心 2024-11-13 10:12:58

invokeLater 稍后将调用该代码。它保证 textfield1.toString() 在 checkUserNames() 之后执行

Well invokeLater will invoke the code later. It dosent gurantee textfield1.toString() to execute after checkUserNames()

年华零落成诗 2024-11-13 10:12:58

您的私有静态成员似乎根本没有被初始化。在使用之前初始化创建的每个对象之前,您不会遇到 NullPointerException。

我不知道你在说什么。您必须对所有这些对象执行类似的操作:

private static JTextArea textArea1 = new JTextArea();

您不是输入用户名或密码;而是输入用户名或密码。您正在创建一个文本区域 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:

private static JTextArea textArea1 = new JTextArea();

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.

日裸衫吸 2024-11-13 10:12:58

为什么不使用 SwingUtilities.invokeAndWait() 而是?这似乎可以立即解决您的问题。

这是一个简单的例子:

import javax.swing.SwingUtilities;

public class InvokeAndWaitExample {
    public static void main (String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable () {
                public void run () {
                    System.out.println("Hello World on " + Thread.currentThread());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Finished on " + Thread.currentThread());
    }
}

Why dont you use SwingUtilities.invokeAndWait() instead? This seems to solve your problem immediately.

Here is the simple example:

import javax.swing.SwingUtilities;

public class InvokeAndWaitExample {
    public static void main (String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable () {
                public void run () {
                    System.out.println("Hello World on " + Thread.currentThread());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Finished on " + Thread.currentThread());
    }
}
蓝眸 2024-11-13 10:12:57

如果您希望代码执行在等待输入时停止,请将 JFrame 设为模态 JDialogJOptionPane

If you want the code execution to stop while waiting for input, make the JFrame a modal JDialog or JOptionPane instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文