java - 单击按钮后 JFrame 冻结
问题描述
我有一个 JFrame,它有一个 JTextField 和一个 JButton。我试图从 JTextField 中的用户输入中获取元素的计数。但是,按下按钮后,JFrame 会冻结。这是我的代码:
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++;
}
否则条件将永远为真。
来源于评论。
推荐阅读
- ios - AVFoundation - raw 冻结相机预览后的深度数据捕获 (captureSessionIsMissing)
- android - Sugar orm 更新错误
- linux - 创建锁定文件时防止竞争条件
- angular - 如何使用 loopback-sdk-builder 实现嵌套查询?
- sql-server-2017 - 选择到#TempTable
- dart - Flutter:使用 Backbutton 功能设置 tabindex,就像在带有底部导航栏的 twitter 中一样?
- android - 无法在后台线程上调用 observeForever
- php - 执行命令的 shell 脚本时,主管标准输出日志为空
- java - 如何在异步任务中使用矩阵?
- javascript - 如何正确循环