在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
你需要你的AccountApplet包含您的賬戶類的實例可以調用它的方法。 – bdavies6086