首页 > 解决方案 > 单击按钮后 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++;
   }

我已经尝试寻找解决方案,但没有解决我的问题。我的代码有什么问题?

标签: javaswing

解决方案


改变:

while (input.hasNext()) 
  n++; 

..类似于..

while (input.hasNext()) { 
  input.getNext(); 
  n++; 
} 

否则条件将永远为真。

来源于评论。


推荐阅读