Java:等待用户在旋转窗口上输入 [英] Java : Wait for user input on swing window

查看:55
本文介绍了Java:等待用户在旋转窗口上输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,大约一周前,我开始学习Java,我正在编写的一个小程序遇到一些问题,这些程序是我通常会使用swing和oop/java进行训练的.

So, I started studying java about a week ago, I'm running into a few issues with a little program I'm building to train with swing and oop/java in general.

程序(到目前为止)具有MainClass和Window类. MainClass创建Window类的实例,该实例创建JFrame并将用户输入保存在字段中.

The program (so far) has a MainClass and a Window class. The MainClass creates an instance of the Window class, which creates a JFrame and saves the user input in a field .

这时,MainClass打印输出,这是我通过getters方法获得的.

At this point, MainClass prints the output, which I get through getters methods.

问题在于,我仍然以一种过程性的方式思考:MainClass打印null,因为它不等待窗口的位置来获取用户输入.

The problem is that I still think in a procedural way: MainClass prints null, because it doesn't wait for the istance of window to get user input.

我该如何解决它,从而使主体在打印之前等待窗口的状态接受用户输入?

How can I fix it, thus getting main to wait for the istance of window to accept user input, before printing?

Nb. Jframe的东西起作用,出现窗口,只是MainClass不等待它完成应有的工作.我可以(我认为?)使用一些睡眠命令来等待,但这似乎是完全错误的.

Nb. The Jframe stuff works, the window appears, it's just that MainClass doesn't wait for it to do what it's supposed to. I could (I think?) use some sleep command to wait but it seems utterly wrong.

这是MainClass.java的代码

here's the code of MainClass.java

import java.util.Arrays;

public class MainClass {

    private char[] password;
    private String pin;

    public static void main(String[] args) {

        Window w = new Window();    
        System.out.println(w.getPin() + Arrays.toString(w.getPassword()) + '1');
    }


}

和Window.java

and Window.java

import java.awt.*;
import javax.swing.*;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import net.miginfocom.swing.MigLayout;


public class Window extends JFrame{

    private JTextField textField_1;
    private JButton btnNewButton;
    private JPanel panel;
    private JPasswordField passwordField;
    private char[] password = new char[10];
    private String pin;


    public Window() {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(370, 150);
        this.setForeground(new Color(192, 192, 192));
        this.setTitle("Access Password Manager");
        this.setResizable(false);

        panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new MigLayout("", "[70.00][132.00,grow][44.00][67.00,grow][61.00][]", "[19.00][34.00][]"));

        JLabel lblNewLabel = new JLabel("Password");
        panel.add(lblNewLabel, "cell 0 1,alignx trailing,aligny center");

        passwordField = new JPasswordField();
        passwordField.setColumns(13);
        panel.add(passwordField, "cell 1 1,alignx center");

        JLabel lblNewLabel_1 = new JLabel("Key");
        panel.add(lblNewLabel_1, "cell 2 1,alignx center,aligny center");

        textField_1 = new JTextField();
        panel.add(textField_1, "cell 3 1,alignx left,aligny center");
        textField_1.setColumns(4);

        btnNewButton = new JButton("Log In");
        ListenForButton listener = new ListenForButton();

        btnNewButton.addActionListener(listener);
        panel.add(btnNewButton, "cell 4 1");

        this.setVisible(true);

    }

        private class ListenForButton implements ActionListener{


            @Override
            public void actionPerformed(ActionEvent e) {

                if(e.getSource() == btnNewButton){

                    if (passwordField.getPassword().length < 10){

                        password = passwordField.getPassword().clone();   
                    }

                    pin = textField_1.getText();

                }
            }       
        }


        public char[] getPassword(){            
            return password;            
        }

        public String getPin(){
            return pin;     
        }


}

这不仅仅是打印,我知道我可以直接在Window.class中进行打印. 如果我自己解释得不好,我很抱歉.请考虑println为一旦窗口将其保存为输入后,我就需要访问和处理这些字段".

It's not just about printing, which I know I could do directly into Window.class. I'm sorry if I explained myself poorly. Please consider the println as a "I need to access and work on those fields once window has saved them form the input".

推荐答案

您可以使用模式对话框获取用户输入,该对话框将在可见的位置阻止代码执行,并在不可见的情况下继续执行(魔术),请参见如何制作对话框以了解更多信息详细信息

You could use a modal dialog to get user input, the dialog will block the code execution at the point it is made visible and continue when it's made invisible (it's magic), have a look at How to Make Dialogs for more details

已更新

模式对话框将仅阻止事件调度线程(从技术上讲,它不会阻止它,只是绕过它),请参见

The modal dialog will only block the Event Dispatching Thread (technically it doesn't block it, it simply circumvents it), see Initial Threads for more details

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.miginfocom.swing.MigLayout;

public class MainClass {

    private char[] password;
    private String pin;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                System.out.println("Before Window");
                Window w = new Window();
                System.out.println("After Window");
                System.out.println(w.getPin() + Arrays.toString(w.getPassword()) + '1');
            }
        });
    }

    public static class Window extends JDialog {

        private JTextField textField_1;
        private JButton btnNewButton;
        private JPanel panel;
        private JPasswordField passwordField;
        private char[] password = new char[10];
        private String pin;

        public Window() {

            this.setModal(true);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.setSize(370, 150);
            this.setForeground(new Color(192, 192, 192));
            this.setTitle("Access Password Manager");
            this.setResizable(false);

            panel = new JPanel();
            getContentPane().add(panel, BorderLayout.CENTER);
            panel.setLayout(new MigLayout("", "[70.00][132.00,grow][44.00][67.00,grow][61.00][]", "[19.00][34.00][]"));

            JLabel lblNewLabel = new JLabel("Password");
            panel.add(lblNewLabel, "cell 0 1,alignx trailing,aligny center");

            passwordField = new JPasswordField();
            passwordField.setColumns(13);
            panel.add(passwordField, "cell 1 1,alignx center");

            JLabel lblNewLabel_1 = new JLabel("Key");
            panel.add(lblNewLabel_1, "cell 2 1,alignx center,aligny center");

            textField_1 = new JTextField();
            panel.add(textField_1, "cell 3 1,alignx left,aligny center");
            textField_1.setColumns(4);

            btnNewButton = new JButton("Log In");
            ListenForButton listener = new ListenForButton();

            btnNewButton.addActionListener(listener);
            panel.add(btnNewButton, "cell 4 1");

            this.setVisible(true);

        }

        private class ListenForButton implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {

                if (e.getSource() == btnNewButton) {

                    if (passwordField.getPassword().length < 10) {

                        password = passwordField.getPassword().clone();
                    }

                    pin = textField_1.getText();

                }
            }
        }

        public char[] getPassword() {
            return password;
        }

        public String getPin() {
            return pin;
        }

    }
}

这篇关于Java:等待用户在旋转窗口上输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆