简体   繁体   English

在Java弹出窗口中重复使用JTextField

[英]Re-using JTextField in Java pop-ups

I'm building a large Java application that uses multiple pop-up windows. 我正在构建一个使用多个弹出窗口的大型Java应用程序。 Some of these windows must be able to be displayed at the same time (for instance, separate pop-ups for Google Earth and a webcam feed), but some of them can only be displayed one at a time (for instance, error messages). 这些窗口中的某些必须能够同时显示(例如,Google Earth和网络摄像头摘要的单独弹出窗口),但是其中某些窗口一次只能显示一个(例如,错误消息) 。 The first kind of pop-ups, the webcam kind, are working fine. 第一种弹出窗口(网络摄像头)运行良好。 But the error message pop-ups act like the webcam type of pop-ups (that is, they create new .class files and there can be multiple error pop-ups). 但是错误消息弹出窗口的作用类似于弹出式摄像头的网络摄像头(也就是说,它们会创建新的.class文件,并且可能有多个错误弹出窗口)。 How do I fix this? 我该如何解决? Should I create a new error class? 我应该创建一个新的错误类吗?

Also, in one of the webcam-type pop-ups, I have JTextFields which read a username and password. 另外,在网络摄像头类型的弹出窗口之一中,我具有读取用户名和密码的JTextFields。 This log-in popup works fine, but if I use it once, close it, and use it again, doing a getText() on the JTextFields returns an empty string. 此登录弹出窗口工作正常,但是如果我使用一次,请关闭它,然后再次使用它,对JTextFields执行getText()会返回一个空字符串。 I think this problem might be related to the one above, but I am not sure. 我认为这个问题可能与上述问题有关,但我不确定。

passwordAction.addActionListener(new ActionListener() {
            JFrame pwPop=new JFrame("Log in");
            JTextField unameField;
            JTextField pwField;
            public void actionPerformed(ActionEvent arg0) {
                pwPop.setBounds(250,200,300,150);
                JPanel pwPopPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
                pwPop.add(pwPopPanel);
                unameField=new JTextField();
                pwField=new JTextField();
                JButton logInButton=new JButton("Log in");
                JButton cancelButton=new JButton("Cancel");
                JLabel logInText=new JLabel("Username:");
                JLabel passwordText=new JLabel("Password:");
                JPanel buttonPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
                pwPopPanel.add(logInText);
                pwPopPanel.add(unameField);
                pwPopPanel.add(passwordText);
                pwPopPanel.add(pwField);
                buttonPanel.add(logInButton);
                buttonPanel.add(cancelButton);
                pwPopPanel.add(buttonPanel);
                logInButton.addActionListener(new ActionListener() {
                    JFrame logErrorFrame;
                    public void actionPerformed(ActionEvent arg0) {
                        if (doLogIn(unameField.getText(), pwField.getText(), "")) {
                            unameField.setText(null);
                            pwField.setText(null);
                            pwPop.setVisible(false);
                        }
                    }
                });
                pwPop.setVisible(true);
            }
        });

As Andrew Thompson showed me, JOptionPane is the solution I was looking for. 正如Andrew Thompson向我展示的那样,JOptionPane是我一直在寻找的解决方案。 It is capable of creating almost any type of popup you may need. 它能够创建您可能需要的几乎任何类型的弹出窗口。

JOptionPane Javadoc JOptionPane Javadoc

Simple Dialog with JOptionPane 使用JOptionPane的简单对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM