2016-04-26 32 views
1

在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

你需要你的AccountApplet包含您的賬戶類的實例可以調用它的方法。 – bdavies6086

回答

1

你需要的Account對象的實例來訪問它的領域。

您可以使用setText(String text)方法更新JTextField對象的值。

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() 
    { 
    } 
+0

我得到以下錯誤AccountApplet.java:104:錯誤:構造函數Account類中的Account無法應用於給定的類型; – HoodCoolege

+0

@HoodCoolege我已更新答案 –

+0

我必須有這些參數 – HoodCoolege

相關問題