【问题标题】:Adding items to JtextArea and updating current balance向 JtextArea 添加项目并更新当前余额
【发布时间】:2013-06-03 17:05:40
【问题描述】:

我正在编写一个 GUI(银行账户)。我已经完成了必要的框架,但在 JTextArea 中添加信息时遇到了问题。我尝试了许多不同的方法,但仍然不成功。

当我选择一个单选按钮时,例如“储蓄或当前”,其他面板将可见。在 a/c TextField 中,我将在两个 TextField 中输入 ID 和名称。之后,我必须选择另一个 RadioButton 存款或取款,然后需要输入存款和取款的余额。

输入所有信息后,我需要单击提交按钮。当我点击提交时,所有信息都应该显示在 JTextArea 中,并且无论我在哪里存款或取款,它都应该更新信息。这就是我面临的问题,我已经厌倦了很多不同的方式,但失败了.....请帮助

请看我下面的代码:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class BankAccount implements ActionListener,ItemListener
    {
        private String id;
        private double balance;
        private String name;
        private double withdraw;
        private double deposit;

        public BankAccount(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 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 double getBalance()
        {
            return this.balance;
        }

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

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


            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 l7 = new JLabel();
            JTextField amount = new JTextField(10);
            JButton button = new JButton("Submit");

            JTextArea area = new JTextArea(10,30);              


        public BankAccount() 
        {
            //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)
            {
                if(c.isSelected())
                {
                    String item = area.getText();

                    listModel.addElement(item);
                }

            }       
        }


        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);
            }


        }


    } 

//Driver Class to run the program
public class BankAccount_Test {

    public static void main(String args[]) 
    {
        BankAccount Test = new BankAccount();
    }


}

【问题讨论】:

  • 您在代码中的哪个位置设置了 JTextArea 的文本?
  • 使用 JTable 代替 JText 区域会更合适。

标签: java jbutton jtextfield jtextarea jradiobutton


【解决方案1】:

如果您接受我的建议,请通过将类分为两个类来使用 OOP 的概念

  1. 帐户
  2. 银行(主类)

通过这种方式,您的程序中有很多帐户,并且每个帐户都将其信息保存在对象中。

class Accounts{
private String id;
    private double balance;
    private String name;
    private double withdraw;
    private double deposit;
    //parametric Constructor
    public Accounts(String id,String name, double balance,double withdraw,double deposit){
        setId(id);
        setName(name);
        setBalance(balance);
        setWithdraw(withdraw);
        setDeposit(deposit);
    }
   //Default Construcor:
   //Highly recommended having it because you have a parametric constructor
   public Accounts(){
    //...
   }

/*
Don't forget setters and Getter
........ 
*/

    @Override
    public String toString(){
        return "ID: "+getId()+"\n"
                +"Name: "+getName()+"\n"
                +"Balance: "+getBalance()+"\n"
                +"Withdraw: "+getWithdraw()+"\n"
                +"Deposit: "+getDeposit()+"\n";
    }
} 

这将是你的 Main 类:

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

public class Bank {
    private List<Accounts> list ;
    private JButton submit;
    //default Constructor
    public Bank(){
        list = new ArrayList<Accounts>();
        submit = new JButton("Submit");
        submit .addActionListener(this);
        /*
        will be alot of code if I create the whole frame 
        but suppose you have created your frame */
    }
    public void actionPerformed(ActionEvent ev){
      if(ev.getSource()==submit){
         Accounts ac = new Accounts(); 
         ac.setId("id.getText()");
         ac.setName("name.getText()");
        //...
        textArea.setText(ac.toString());//see it's easy 
        list.add(ac);
      }
    }
}

【讨论】:

  • 非常感谢兄弟,我正在尝试你的,希望一切顺利:-D
  • 兄弟,它给出了一个“错误:类 Account 中的构造函数 Account 不能应用于给定类型;”帐户不能应用于给定类型;帐户acnt = new Account();必需:String,double,String,double,double 找到:无参数原因:实际参数列表和正式参数列表长度不同错误:找不到符号acnt.setId("id.getText()");符号:方法 setId(String) 位置:Acnt 类型的变量帐户 C:\Users\Sajid\Documents\Bank.java:122: 错误:找不到符号 acnt.setName("name.getText()"); 3 个错误
  • @user2448774 :是的,我的错误我忘记在Account 类中创建一个默认构造函数。请查看编辑后的答案。
【解决方案2】:

公开 JTextArea 并在您想要更改文本时调用 .setText()

public JTextArea textArea = new JTextArea();

以后,您可以添加一个方法,例如:

public void setText() {

    textArea.setText("Type something here...");
}

【讨论】:

  • 是不是需要先设置文本,然后调用getText()方法来显示
  • @user2448774 getText() 将返回文本字段中的内容,setText() 将设置它。您不需要调用getText() 来显示它。
  • 噢噢噢!...让我试试看
猜你喜欢
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
相关资源
最近更新 更多