我一直在尝试构建一个简单的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行之后,它不会获得任何输入或停止运行。
请帮忙。谢谢
最佳答案
我认为这与AWT线程以及关机的发生方式有关(它似乎至少需要一个活动窗口)。我通过创建一个不可见的JFrame
修复了该问题,该问题以某种方式不会使AWT线程结束。只需将其作为main
中的第一条语句即可:
JFrame f = new JFrame();
f.setVisible(true);
SwingUtilities.invokeLater(() -> f.setVisible(false));
并且您的
JOptionPane
将起作用。关于java - JOptionPane不显示且代码无法继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51551235/