Java JFrame在单击按钮后冻结
我有一个JFrame,它有一个JTextField和一个JButton。我试图从用户在JTextField中的输入中获取元素的计数。但是,按下按钮后,JFrame将冻结。这是我的密码:Java JFrame在单击按钮后冻结,java,swing,Java,Swing,我有一个JFrame,它有一个JTextField和一个JButton。我试图从用户在JTextField中的输入中获取元素的计数。但是,按下按钮后,JFrame将冻结。这是我的密码: private void bubbleSortButtonActionPerformed(java.awt.event.ActionEvent evt) { // read the user'
private void bubbleSortButtonActionPerformed(java.awt.event.ActionEvent evt) {
// read the user's input from text field and store it in to the elements string
String elements = inputField.getText();
// initialize a scanner for the elements
Scanner input = new Scanner(elements);
// initialize a counter variable for counting the number of elements input by the user
int n = 0;
// increment the counter variable as long as it could read a next token
while (input.hasNext())
n++;
}
我已经试着寻找解决办法,但没有任何办法解决我的问题。我的代码怎么了?更改:
while (input.hasNext())
n++;
…到…之类的程度
while (input.hasNext()) {
input.getNext();
n++;
}
否则,条件将永远为真
来源于评论。更改:
while (input.hasNext())
n++;
…到…之类的程度
while (input.hasNext()) {
input.getNext();
n++;
}
否则,条件将永远为真
来源于注释。为什么要使用带有文本字段文本的扫描仪。如果您想知道字符串中的字符数,只需使用
String.length()
方法即可。为什么要使用扫描仪扫描文本字段中的文本。如果您想知道字符串中的字符数,只需使用String.length()
方法即可。