【发布时间】:2014-12-14 11:33:16
【问题描述】:
我目前正在测试各种获取用户输入的方法。今天我正在使用 OnClosing() 进行测试,这似乎有效。我的问题是,如果我没有在 while() - 循环中放入任何东西,它就不起作用。用代码更容易展示:
public MyClass() {
String myPassword= new Password().getpw();
}
好的。这就是我获取字符串的方式。
public class Password {
private boolean goon = true;
private JPasswordField jpassword = new JPasswordField ();
public Password() {
JFrame f = new JFrame("Type your password here, get it by closing the frame");
f.getContentPane().add(jpassword);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
goon = false;
}
}
);
f.setSize(400,65);
f.show();
}
public String getpw() {
while (goon);
return new String(jpassword.getPassword());
}
}
除非我在 while() 循环中随机放置一些东西,否则这不起作用。让我告诉你。这样工作
public String getpw() {
while (goon) {
System.out.println("");
}
return new String(jpassword.getPassword());
}
为什么它不能使用空循环?
【问题讨论】:
标签: java oop while-loop passwords