簡體   English   中英

單擊按鈕后 JFrame 凍結

[英]JFrame freezes after clicking a button

我有一個 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++; 
} 

否則條件將永遠為真。

來自評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM