【问题标题】:how do i get this object to setText in GUI我如何让这个对象在 GUI 中设置文本
【发布时间】:2013-10-31 07:48:41
【问题描述】:

所以,我一直在制作这个 BankPanel 课程,以配合我的老师为我的 Java 课程提供的另外两门课程。我应该创建一个银行账户对象,我需要从对象获取变量值到 accountNameTF、accountnumberTf 和 accountBalanceTF。请帮忙??

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class BankPanel extends JPanel
{
    private int amount;
    private JLabel accountName;
    private JLabel accountNumber;
    private JLabel accountBalance;
    private JLabel accountStatus;
    private JLabel depwitAmount;
    private JTextField accountNameTF;
    private JTextField accountNumberTF;
    private JTextField accountBalanceTF;
    private JTextField accountStatusTF;
    private JTextField depwitAmountTF;
    private JButton depositButton;
    private JButton withdrawButton;
    private int acctNumber;
    private double balance;
    private String name;

    Object myAcct()  // Create bank Account object   
    {
        acctNumber = 128895;
        balance = 0.00;
        name = "Bart Simpson";


    }

    public BankPanel()
   {
      amount = 0;

        accountName = new JLabel ("Account name: ");
        accountNumber = new JLabel ("Account number: ");
        accountBalance = new JLabel ("Account balance: ");
        accountStatus = new JLabel ("Account status: ");
        depwitAmount = new JLabel ("Deposit/Withdraw amount: ");
        accountNameTF = new JTextField (15);
        accountNumberTF = new JTextField (10);
        accountBalanceTF = new JTextField (10);
        accountStatusTF = new JTextField (10);
        depwitAmountTF = new JTextField (10);
        depositButton = new JButton ("Deposit");
        withdrawButton = new JButton ("Withdraw");

        depositButton.addActionListener (new ButtonListener());
        withdrawButton.addActionListener (new ButtonListener());
        BankPanel obj = new BankPanel(); 

        add (accountName);
        add (accountNameTF);
        add (accountNumber);
        add (accountNumberTF);
        add (accountBalance);
        add (accountBalanceTF);
        add (accountStatus);
        add (accountStatusTF);
        add (depositButton);
        add (withdrawButton);
        add (depwitAmount);
        add (depwitAmountTF);


      setBackground(Color.cyan);
      setPreferredSize(new Dimension(300, 200));
      accountNameTF.setText(Integer.toString(amount));
      accountNumberTF.setText(Integer.toString(amount));
      accountBalanceTF.setText(Integer.toString(amount));
      accountStatusTF.setText(Integer.toString(amount));
      depwitAmountTF.setText(Integer.toString(amount));
      accountNameTF.setText(myAcct.getName());
      accountNumberTF.setText(Integer.toString ( myAcct.getAcctNumber() ) );
      accountBalanceTF.setText(Double.toString( myAcct.getAcctNumber() ) );

   }

这是 BankAccount 类:

class BankAccount 
{

 private int acctNumber;
 private double balance;
 private String name;

 private static int acctCount= 0;  //not an instance variable, but a class variable (static)

/** constructs a bank account with zero balance, zero account number
 and name set to Unknown

*/

public BankAccount() {
     acctNumber = 0;
     balance = 0.0;
     name = "Unknown";

     acctCount++;
}

/*
  constructs a bank account with an account number, an  initial balance, and
  an owner!
 */

public BankAccount(int acctNo, double initBalance, String owner) {
    acctNumber = acctNo;
    balance = initBalance;
    name = owner;

    acctCount++;
}


 //all of the mutator methods - set

 public void setAcctNumber(int acct)
 {
        acctNumber = acct;
 }

public void setBalance(double amount)
 {
    balance = amount;
 }

 public void setName(String someName)
 {
    name = someName;
 }

//all of the accessor methods - get

public int getAcctNumber()
{
    return acctNumber;
 }

public double getBalance()
 {
 return balance;
}

public String getName()
 {
    return name;
}


public void deposit(double amount)
{
 balance = balance + amount;
}

public void withdraw(double amount) {
 balance = balance - amount;
}

 //overloaded method.  charges a fee!
 public void withdraw(double amount, double fee)
 {
        balance = balance - amount - fee;
 }

public String toString()
{
        return ("BankAccount : acctNumber "  + acctNumber +  " balance : "     + balance 
           + " name : " + name  );
}

//Class method to display our private static variable
public static int getAcctCount()
{
    return ( acctCount );
}

}// end of class definition

【问题讨论】:

    标签: java swing user-interface jtextfield settext


    【解决方案1】:

    我应该创建一个银行帐户对象 表示创建一个银行账户类的对象,如

    BankAccount b=new BankAccount();
    

    我需要从对象中获取 accountNameTF、accountnumberTf 和 accountBalanceTF 的变量值。

    accountNameTF.setText(b.name);
    

    注意 我假设 name 是 BankAccount 类中的一个变量 假设如果您在 bankaccount 类中有 getter 和 setter,那么您使用这种方式

    accountNameTF.setText(b.getName());
    

    【讨论】:

    • @BlakeWinters 接受答案是 stackoverflow 表达谢谢的方式
    • 请不要使用 b.name!改用吸气剂!
    • @PrR3 我不知道getter方法名,所以我只是举个例子
    • @PrR3 我使用了 myAcct.name(我的 BankAccount 类中已经写了 getter)
    • @javaBeginner 好吧。只是想为一个java初学者提一下好的编程风格......
    【解决方案2】:

    当您正确设置了构造函数后,您只需调用BankAccount bankAccount = new BankAccount(bankPanel);

    public BankAccount(BankPanel bankPanel)
    {
        this.whateverYouNeed = bankPanel.whateverYouNeed;
        // repeat for other variables
    }
    

    【讨论】:

      【解决方案3】:

      你试过了吗:

      BankAccount ba = new BankAccount();
      accountNameTF.setText(ba.getText());
      accountnumberTf.setText(ba.getText());
      accountBalanceTF.setText(ba.getText());
      

      ?

      【讨论】:

      • @PrR3 ba.getText() 是一个例子,如果你看到了 - 没有显示 BankAccount 类的代码!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2018-01-05
      • 2022-10-16
      • 1970-01-01
      相关资源
      最近更新 更多