2013-10-31 56 views
0

因此,我一直在讓這個BankPanel類與我的老師給我們的Java課程的另外兩個類一起去。 GUI啓動,但它不顯示除名稱「Unknown」以外的任何值,其餘文本字段爲0。我應該改變BankPanel類,而不是其他類。我無法讓我的GUI正常顯示

更新:我只需要在BankPanel中獲取setAcctNumber,setBalance和setName,以便將值更改爲他們需要的值。

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() // this is where i messed up 
    { 
     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 

主類:

import javax.swing.JFrame; 

public class BankGUI 
{ 
//----------------------------------------------------------------- 
// Creates and displays the main program frame. 
//----------------------------------------------------------------- 
public static void main (String[] args) 
{ 
    JFrame frame = new JFrame ("My Bank Account"); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

    BankPanel panel = new BankPanel(); 
    frame.getContentPane().add(panel); 

    frame.pack(); 
    frame.setVisible(true); 
} 
} 
+0

顯示的值是您的BankAccount對象的初始值!?所以一切都好? – PrR3

+0

@ PrR3它應該顯示名稱爲「Bart Simpson」,然後賬號爲「128895」 –

+0

我認爲我們不是在這裏解決你的功課,而是作爲一個提示:使用set-method來改變你的對象! – PrR3

回答

1

爲什麼Bart Simpson不顯示這樣

設置的值,你不設置任何值,多數民衆贊成
BankAccount b=new BankAccount(); 
b.setName("Bart Simpson"); 
b.setBalance(0.00); 
b.setAcctNumber(128895); 
+0

當我將它們更改爲「set________」時,它只是表示它沒有字段/無法解析 –

+0

@BlakeWinters發佈堆棧跟蹤 – SpringLearner

+0

線程「main」中的異常java.lang.Error:未解決的編譯問題: \t setAcctNumber不能成爲解決或不是一個場 \t的setBalance不能得到解決或不是一個場 \t的setName不能得到解決或不是一個場 \t在bankAccountGUI.BankPanel。 (BankPanel.java:40) \t at bankAccountGUI.BankGUI.main(BankGUI.java:22) –