Java 在JPanel中显示和更新余额

Java 在JPanel中显示和更新余额,java,swing,jbutton,actionlistener,jtextfield,Java,Swing,Jbutton,Actionlistener,Jtextfield,我正在编程一个GUI(银行账户)。一切正常;但面对在JPanel中显示当前余额的问题,它应该在我点击提交按钮时显示余额。我尝试过许多不同的方法,但仍然不成功 我的信息在JTextArea中正确显示 但我不能让它在第三个JPanel中显示金额(“当前金额”)。 我在JTextField中输入金额,然后单击submit,它应该显示在JPanel中(“当前金额”) 每当我输入一个人的相同id并单击提交按钮时,金额应该得到更新。 我在建立逻辑方面确实有问题。 我已经发布了整个代码。 请参见下面我的编码:

我正在编程一个GUI(银行账户)。一切正常;但面对在JPanel中显示当前余额的问题,它应该在我点击提交按钮时显示余额。我尝试过许多不同的方法,但仍然不成功

我的信息在JTextArea中正确显示

但我不能让它在第三个JPanel中显示金额(“当前金额”)。 我在JTextField中输入金额,然后单击submit,它应该显示在JPanel中(“当前金额”)

每当我输入一个人的相同id并单击提交按钮时,金额应该得到更新。 我在建立逻辑方面确实有问题。 我已经发布了整个代码。 请参见下面我的编码:

//帐户类别

public class Account 
{
    private String id;
    private double balance;
    private String name;
    private double withdraw;
    private double deposit;

    public Account(String id, double balance, String name, double withdraw, double deposit)
    {
        this.id = id;
        this.balance = balance;
        this.name = name;
        this.withdraw = withdraw;
        this.deposit = deposit;     
    }
    public Account()
    {           
    }

    public void setId(String acID)
    {
        this.id = acID; 
    }

    public void deposit(double sum)
    {
        this.balance = this.balance + sum;
    }

    public void withdraw(double sum)
    {
        this.balance = this.balance - sum;
    }

    public String getId()
    {
        return this.id;
    }

    public void setBalance(double blnc)
    {
        this.balance = blnc;
    }

    public double getBalance()
    {
        return this.balance;
    }

    public String getName()
    {
        return this.name;
    }

        public void setName(String a)
    {
        this.name = a;
    }

    public double getWithdraw()
    {
        return this.withdraw;
    }

    public double getDeposit()
    {
        return this.deposit;
    }

    public String toString()
    {
        return " " + getId()
             + " - " + getName()
             + " - " + getBalance();
    }   

}   
//银行类

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;

public class Bank implements ActionListener,ItemListener
{
    private List<Account> store;

        DefaultListModel listModel = new DefaultListModel();
        JList list = new JList(listModel);
        FlowLayout flow = new FlowLayout();
        ButtonGroup group = new ButtonGroup();
        JFrame frame = new JFrame("Lexus Bank");
        JPanel p = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();

        JRadioButton a = new JRadioButton("Savings");
        JRadioButton b = new JRadioButton("Current");
        JRadioButton c = new JRadioButton("Deposit");
        JRadioButton d = new JRadioButton("Withdraw");

        JLabel l1 = new JLabel("A/C No:");
        JLabel l2 = new JLabel("A/C Name:");
        JTextField accID = new JTextField(10);
        JTextField accName = new JTextField(10);

        JLabel l3 = new JLabel();
        JLabel l4 = new JLabel();
        JLabel l5 = new JLabel("Amount: " );
        JLabel l6 = new JLabel("Current \n Amount: " );
        JLabel currentBal = new JLabel();
        JTextField amount = new JTextField(10);
        JButton button = new JButton("Submit");

        JTextArea area = new JTextArea(10,30);    

    public Bank() 
    {
        store = new ArrayList<Account>();           

        //Setting values for JFrame
        frame.setSize(800,600);
        frame.add(p);
        frame.add(p2);
        frame.add(p3);
        frame.add(p4);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Adding the buttons in group
        group.add(a);
        group.add(b);
        group.add(c);
        group.add(d);

        //Setting value for panel 1
        frame.getContentPane().setLayout(flow);
        p.setPreferredSize(new Dimension(100,100));     
        p.setLayout(new GridLayout(2,1));
        p.add(a);
        p.add(b);
        p.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(),"A/C Type"));

        //Setting value for panel 2
        p2.setPreferredSize(new Dimension(300,100));
        p2.setLayout(new GridLayout(4,3));
        p2.add(l1);
        p2.add(accID);
        p2.add(l2);
        p2.add(accName);
        p2.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(),"Account Details")); 
        p2.setVisible(false);

        //Setting value for panel 3
        p3.setPreferredSize(new Dimension(300,150));
        p3.setLayout(new FlowLayout());
        p3.add(l3);
        p3.add(c);
        p3.add(l4);
        p3.add(d);
        p3.add(l5);

        p3.add(amount);
        p3.add(button);
        p3.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(),"Transaction"));
        p3.add(l6);
        p3.setVisible(false);

        //Setting value for panel 4
        p4.setLayout(new GridLayout(1,2));
        p4.add(area);
        p4.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(),"Transaction History"));
        p4.setVisible(false);

        //Creating Actions
         a.addItemListener(this);
         b.addItemListener(this);
         c.addActionListener(this);
         d.addActionListener(this);

         button.addActionListener(this);            
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == button)
        {
            Account amnt = new Account();               
            amnt.setBalance(Integer.parseInt(amount.getText()));
            //currentBal.append(amnt.getBalance());
            store.add(amnt);                
        }

        if(e.getSource() == button)
        {               
            if(c.isSelected())
            {
                Account acnt = new Account();
                acnt.setId(accID.getText());
                acnt.setName(accName.getText());
                acnt.setBalance(Integer.parseInt(amount.getText()));
                //store.add(ad);
                area.append("\nDP-"+ acnt.toString());
                store.add(acnt);
            }

            if(d.isSelected())
            {
                Account acnt = new Account();
                acnt.setId(accID.getText());
                acnt.setName(accName.getText());
                acnt.setBalance(Integer.parseInt(amount.getText()));
                area.append("\nWD-"+acnt.toString());
                store.add(acnt);
            }  
        }       
    }


    public void itemStateChanged(ItemEvent e)
    {
        Object source = e.getSource();
        if(source == a)
        {
            p2.setVisible(true);
            p3.setVisible(true);
            p4.setVisible(true);
        }
        if(source == b)
        {
            p2.setVisible(true);
            p3.setVisible(true);
            p4.setVisible(true);
        }
    }
}    

你有一些工作要做

我可以给你一些建议:

  • actionPerformed
    中,您不应该每次按下按钮时都创建一个新的
    帐户acnt
    。相反,您应该有一个
    hashmap
    (或类似)包括
    Account
    对象作为值和
    Account Number
    作为查询键
  • 一旦有了可以通过hashmap查找的
    Account
    的全局实例,就需要实际使用
    acnt.setBalance
    方法进行加法和减法运算
下面是一个代码示例,它基本上将您的代码更改为仅适用于一个帐户,但它正确地执行减法和加法:

Account acnt = new Account();
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == button)
        {

        }

        if(e.getSource() == button)
        {

            if(c.isSelected())
            {

                acnt.setId(accID.getText());
                acnt.setName(accName.getText());
                acnt.setBalance(acnt.getBalance()+Integer.parseInt(amount.getText()));
                //store.add(ad);
                area.append("\nDP-"+ acnt.toString());
                store.add(acnt);
            }

            if(d.isSelected())
            {
                acnt.setId(accID.getText());
                acnt.setName(accName.getText());
                acnt.setBalance(acnt.getBalance()-Integer.parseInt(amount.getText()));
                area.append("\nWD-"+acnt.toString());
                store.add(acnt);
            }


        }       
    }
要实际显示帐户的当前余额,您可以使用:

 area.append("\nBalance="+acnt.getBalance());

这个问题更适合于代码审查。还有,代码中有很多问题。我的英语有点弱。让我附上屏幕截图,然后这个问题将使更多sense@user2448774您需要在actionListner中添加类似于
l6.setText(amount.getText())的内容天哪,我真是太蠢了,谢谢@Smit,它起作用了。但是如何更新balance@user2448774我不确定你是否需要翻阅你的代码。创建反映您的问题的新帖子。
 area.append("\nBalance="+acnt.getBalance());