如何暂停actionPerformed方法的程序



我正在尝试编写一个允许用户输入用户名和密码的类。我正在使用Swing类创建一个JFrame,其中包括一个JButton,用户输入信息后将按下它。我想返回用户输入的信息,我使用了两个全局变量,它们在actionPerformed方法中设置。不幸的是,由于这个方法是void(我继承它(,我还需要包括2个访问器方法。这是这个类的代码:

import javax.swing.*;                                      
import java.awt.*;   
import javax.swing.border.Border;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;  

public class LoginPage extends JFrame
implements ActionListener{

private JButton button;
private JTextField usrEntry;
private JPasswordField pwordEntry;
private String _usrName;
private String _password;

public LoginPage(){
// set fields 
_usrName = "";
_password = "";

this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE); 
this.setSize(700, 700);
this.setBackground(Color.blue);

// make background panel
JPanel background = new JPanel
(new GridBagLayout());  
background.setPreferredSize(this.getSize());
background.setBackground(Color.blue);

// make panel to hold contents
JPanel panel = new JPanel(); 
panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(300, 200));

// add labels to create some space
JLabel labelHolder1 = new JLabel
("                                          ");
JLabel labelHolder2 = new JLabel
("                                          ");
JLabel labelHolder3 = new JLabel("              ");
labelHolder1.setBounds(10, 20, 80, 25);
labelHolder2.setBounds(10, 20, 80, 25);
labelHolder3.setBounds(10, 20, 80, 25);
panel.add(labelHolder1);     
panel.add(labelHolder2);     
panel.add(labelHolder3);     

// add 'user' tag to the panel
JLabel usrLabel = new JLabel("UserName:");
usrLabel.setBounds(10, 20, 80, 25);
panel.add(usrLabel);         

// add space for user to enter his username
usrEntry = new JTextField(20); 
usrEntry.setBounds(100, 20, 165, 25);
panel.add(usrEntry);         

// add 'password' tag to the panel
JLabel pwordLabel = new JLabel("Password:");
pwordLabel.setBounds(10, 50, 80, 25);
panel.add(pwordLabel);       

// add space for user to enter his password
pwordEntry = new JPasswordField(20);
pwordEntry.setBounds(100, 50, 165, 25);
panel.add(pwordEntry); 

// add button for login
button = new JButton("Log-In");
button.setBounds(10, 80, 80, 25);
button.addActionListener(this);
panel.add(button);  

// make the frame   
this.setVisible(true);
background.add(panel);
this.add(background);
}                    

@ Override           
public void actionPerformed(ActionEvent press){
if(press.getSource() == button){
_usrName = usrEntry.getText();
_password = pwordEntry.getText();}
System.out.print(_usrName + "n" +
_password);
}                    

// returns user input for userName
public String getUserName(){
return _usrName; 
}                    

// returns the user input for password
public String getPassword(){
return _password;}
}

问题是,如果我实例化这个类并运行它,主方法程序将在调用actionPerformed方法之前完成。这是有问题的,因为如前所述,我正在尝试获取输入的值并返回它们。以下是一个示例主程序及其输出:

public class LoginTester{     
public static void main(String[] args){
LoginPage login = new LoginPage();
int i = 1 + 2;                                    
System.out.println(i);
System.out.println(login.getUserName() + "n" +
login.getPassword()); } }

这里是如果我输入"0"的输出;用户名";是第一个文本框;密码";在第二个:

3

用户名密码

因此,正如您所看到的,它从主方法中打印"3",然后为空String,因为它们是在构造函数中初始化的。然后从actionPerformed方法打印"用户名"one_answers"密码"。成功的条目将打印"用户名"one_answers"密码",而不是空字符串。

希望这是足够清楚的,如果不是,我会在评论中澄清。谢谢你花时间阅读我的问题。

另一个可能对你有用的想法:如果你想执行一些代码,你可以添加一个windowListener来在关闭时调用,比如:

public class LoginTester{     
public static void main(String[] args){
LoginPage login = new LoginPage();
login.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
int i = 1 + 2;                                    
System.out.println(i);
System.out.println(login.getUserName() + "n" +
login.getPassword());
}
});
}
}

这将使代码在关闭窗口时执行,假设这是您想要的

我不确定我是否理解你的问题
因此执行actionPerformed并打印一些输出,但主方法中的println调用不会。

这是因为主方法将同时执行。如果你想在以后做一些事情,你必须使用其他事件,类似于actionPerformed

您可能想要使用一个可以设置为模态的JDialog。这意味着程序在对话框关闭后进行处理。https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

最新更新