作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我一直在制作这个 BankPanel 类,以配合我的老师为我的 Java 类(class)提供的另外两个类。我应该创建一个银行帐户对象,并且需要从该对象获取 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
最佳答案
我应该创建一个银行帐户对象意味着创建一个银行帐户类的对象,例如
BankAccount b=new BankAccount();
我需要从对象获取 accountNameTF、accountnumberTf 和 accountBalanceTF 的变量值。
accountNameTF.setText(b.name);
注意我假设name
是BankAccount类中的变量假设如果你在bankaccount类中有getter和setter,那么你使用这种方式
accountNameTF.setText(b.getName());
关于java - 我如何在 GUI 中将此对象设置为 setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700883/
我是一名优秀的程序员,十分优秀!