在refreshFields方法下,我试图在AccountApplet类中显示帐户ID和余额,getId()和getBalance()都在Account类中,我该怎么做?
这是帐户类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Account extends Exception
{
int id = 1234;
double balance = 1000.00;
Account (int id, double balace)
{
}
public int getId()
{
return id; // placeholder
}
public double getBalace()
{
return balance; // placeholder
}
public void setBalance(double balance) throws NegativeAmountException
{
}
public void deposit(double amount) throws NegativeAmountException
{
}
public void withdraw(double amount) throws NegativeAmountException,
InsufficientFundsException
{
}
}
这是accountApplet类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class AccountApplet extends JApplet implements ActionListener
{
// For West
public JLabel ai = new JLabel("Account ID ");
public JTextField aitf = new JTextField();
public JLabel ab = new JLabel("Account Balance ");
public JTextField abtf = new JTextField();
// For East
public JButton dp = new JButton ("Deposit");
public JTextField dptf = new JTextField();
public JButton wt = new JButton ("Withdraw");
public JTextField wttf = new JTextField();
// For South
public JLabel status = new JLabel("placeholder");
public void init()
{
this.setSize(400, 90);
//----------------------
// Set up the Structure
//----------------------
Container c = getContentPane();
JPanel b = new JPanel(new BorderLayout());
JPanel west = new JPanel(new GridLayout(2,2));
JPanel east = new JPanel(new BorderLayout());
JPanel depo_with = new JPanel(new GridLayout(2,2));
// Add BorderLayout to the container
c.add(b);
// Add everything to West
b.add(west, BorderLayout.WEST);
west.setBorder(new TitledBorder("Display Account Information"));
west.add(ai);
west.add(aitf);
aitf.setEditable(false);
west.add(ab);
west.add(abtf);
abtf.setEditable(false);
// Add everything to EAST
b.add(east, BorderLayout.EAST);
east.setBorder(new TitledBorder("Deposit or Withdrawl Funds"));
east.add(depo_with, BorderLayout.EAST);
depo_with.add(dptf);
depo_with.add(dp);
depo_with.add(wttf);
depo_with.add(wt);
dp.addActionListener(this);
wt.addActionListener(this);
// Add everything to EAST
b.add(status, BorderLayout.SOUTH);
} // End intit
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == dp) // Executes if deposit was clicked
{
}
if (e.getSource() == wt) // Executes if withdraw was clicked
{
}
} // End actionPerformed
public void refreshFields()
{
getId();
// diplays accound id and balance in left text fields
//should be called when the applet is first displayed and after each valid transaction
}
public double getAmount(JTextField tf) throws EmptyFieldException,
NumberFormatException,
NegativeAmountException
{
return 5.0;
} // End getAmount
} // End Class
答案 0 :(得分:1)
您需要Account
对象的实例来访问它的字段。
您可以使用JTextField
方法更新setText(String text)
个对象的值。
public void refreshFields()
{
Account theAccount = new Account();
aitf.setText(theAccount.getId());
abtf.setText(theAccount.getBalace());
// diplays accound id and balance in left text fields
//should be called when the applet is first displayed and after each valid transaction
}
您可能希望将getBalace()
方法更改为getBalance()
。
另外,为Account
对象添加新的构造函数:
Account ()
{
}