JOptionPane不显示并且代码无法继续

时间:2018-07-27 05:24:15

标签: java joptionpane

我一直在尝试构建一个模拟银行存款和取款的简单Java应用程序。 如果用户尝试提款超过银行帐户余额,则尝试使用JOptionPane显示错误。但是JOptionPane不会显示该消息,并且代码不会继续到下一行。

包含主要应用程序的“我的银行”类

import java.util.Scanner;
public class Bank {

public static void main(String[] args)
{
        //Define an object of type Scanner to get the input
        Scanner inp = new Scanner(System.in); 
        String name; //Local variable to get the name
        double bal; //Local variable to get the balance


        //Getting the inputs from the user
        System.out.println("Enter the name: ");
        name = inp.nextLine();
        System.out.println("Enter the balance: ");
        bal = inp.nextDouble();
        Account a1 = new Account(name, bal); //Creating Account object a1


        inp.nextLine();
        /**The above command is to remove the newline character after 
           inp.nextDouble() since it is not consumed by it and this affects 
           the string input in the following lines*/

        System.out.println("Enter the name: ");
        name = inp.nextLine();
        System.out.println("Enter the balance: ");
        bal = inp.nextDouble();
        Account a2 = new Account(name, bal); //Creating Account object a2

        //Displaying the input details
        a1.dispDetails();
        a2.dispDetails();

        /**For this app we just withdraw money from a1 and deposit in a2 */

        System.out.println("Enter the amount to be withdrawn from a1");
        double w_d = inp.nextDouble();
        a1.withdraw(w_d);
        a1.dispDetails();


        System.out.println("Enter the amount to be deposited in a2: ");
        double deposit = inp.nextDouble();
        a2.credit(deposit);
        a2.dispDetails();

        System.out.println( "Number of accounts created is "+Account.getCount());
        }

}

JOptionPane无法使用的我的帐户类

import javax.swing.*;
public class Account {
/** Declaration of the class variables*/
private final String name; //store the name of the account holder
private double balance;//store the balance
private final int acc_num; //since account number is not changed, set as final
static int count = 0; //to keep track of the number of accounts
JFrame f = new JFrame();

public Account(String n,double bal) 
{
    count++; 
    this.name = n;
    //generating a random account number based on count
    this.acc_num = count*599*254715; 
    this.balance = bal;

}
/** static function to access the static variable*/
static int getCount()
{
    return count;
}

/** To deduct amount on withdrawal Raises a warning if there is 
    insufficient balance*/           
public void withdraw (double amt)
{
    if(amt < balance)
        balance -= amt;
    else
    {    
        JOptionPane.showMessageDialog(f, "Insufficient Balance");//not working


    }    
}


//credit an amount to the account
public void credit (double amt)
{
    balance += amt;
}

//display the details of the account
public void dispDetails()
{
System.out.println("Name: "+name+"\nAccount number: "+acc_num);
System.out.println("Balance= "+balance+"\n");
}



}

在JOptionPane行之后,它没有获得任何输入或停止运行。

请帮助。谢谢

1 个答案:

答案 0 :(得分:0)

我认为这与AWT线程以及关闭的发生方式有关(它似乎至少需要一个活动窗口)。我通过创建一个不可见的JFrame来解决此问题,该问题以某种方式不会使AWT线程结束。只需将其作为main中的第一条语句即可:

JFrame f = new JFrame();
f.setVisible(true);
SwingUtilities.invokeLater(() -> f.setVisible(false));

,您的JOptionPane即可使用。