首页 > 解决方案 > GUI 卡住了。(Java netbeans),

问题描述

我在我的 GUI 中添加了一个函数,它永远存在。它以前工作正常。这里的图片显示了我卡住的 GUI

public void setBalance(String username,double balance){

        Node current = headNode;

        while(current!=null)
        {
            if(current.username.equalsIgnoreCase(username))
            {
                current.setBalance(balance);

            }else{
                current=current.getNext();
             
            }
        }
    }

这是我写的函数,它只是卡在那里。

private void D_EnterActionPerformed(java.awt.event.ActionEvent evt) { 
                                       
        // TODO add your handling code here:
        String username = accountUsername.getText();

        String password = accountPassword.getText();

        boolean flag = false;
       S_Balance=l.findBalance(username, password);
        Deposit_text.setVisible(true);
        num1 = S_Balance + Double.parseDouble(D_deposit_text.getText()) ;
        Deposit_text.setEditable(false);
        Deposit_text.setText("Previous Balance:"+Double.toString(S_Balance)+"\nNew Balance:"+Double.toString(num1));
        S_Balance = S_Balance + Double.parseDouble(D_deposit_text.getText());
        l.setBalance(username, S_Balance);
        this.N_Deposit++;
        D_deposit_text.setText("");
        if(S_Balance < 25)
        {
            Status.setVisible(true);
            Active_Status.setVisible(false);
            S_TextField.setText("Your Balance is less than $25");
            SmileyFace.setVisible(false);
            Smiley1.setVisible(true);
        }else{
            Status.setVisible(false);
            Active_Status.setVisible(true);
            S_WithDraw.setEnabled(true);
            S_TextField.setText("You are good to go!!!");
            SmileyFace.setVisible(true);
            Smiley1.setVisible(false);
            W_Enter.setEnabled(true);
        }
    }                   

这就是它的名称。

标签: javaswingjframe

解决方案


你被卡住了,因为当你找到用户名时,你并没有打破 while 循环(所以它会永远运行)。您应该执行以下操作:

public void setBalance(String username,double balance){

    Node current = headNode;

    while(current!=null)
    {
        if(current.username.equalsIgnoreCase(username))
        {
            current.setBalance(balance);
            break; // this will break the loop

        }else{
            current=current.getNext();
        }
    }
}

推荐阅读