gpt4 book ai didi

Java : Wait for user input on swing window

转载 作者:行者123 更新时间:2023-12-01 21:10:38 26 4
gpt4 key购买 nike

所以,我大约一周前开始学习 Java,我正在构建一个小程序,用于一般性地使用 swing 和 oop/java 进行训练,但遇到了一些问题。

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

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

问题是我仍然以程序的方式思考:MainClass 打印 null,因为它不等待窗口的 istance 来获取用户输入。

如何修复它,从而让 main 在打印之前等待窗口的距离接受用户输入?

铌。 Jframe 的东西工作了,窗口出现了,只是 MainClass 没有等待它做它应该做的事情。我可以(我认为?)使用一些 sleep 命令来等待,但这似乎完全错误。

这是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

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 视为“一旦窗口从输入中保存了这些字段,我就需要访问和处理这些字段”。

最佳答案

您可以使用模式对话框来获取用户输入,该对话框将在其可见时阻止代码执行,并在其不可见时继续执行(这很神奇),看看 How to Make Dialogs了解更多详情

已更新

模式对话框只会阻止事件调度线程(从技术上讲,它不会阻止它,它只是绕过它),请参阅Initial Threads了解更多详情

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 : Wait for user input on swing window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58902825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com