为什么继承的方法只适用于一个类?

时间:2017-11-20 22:51:26

标签: java inheritance jframe

我是Java和编程的新手,也是Stack溢出的新手。我参加面向对象的课程,我被困在一项任务中。希望我能在这里得到一些提示或反馈。 我写了一个虚构的银行业务程序,现在我应该写一个用户界面。

SubMenuWindow类有图形组件来绘制窗口,我想显示所有客户名称和ID号。

Classes CustomerSubWindow和CreditAccountSubWindow继承自SubMenuWindow,因为我希望它们都显示客户名称和ID号。

当我创建客户对象时,everting在从CustomerSubWindow创建的框架中工作,但是从CreditAccountWindow创建的框架不会显示名称和ID号。两者都继承自同一个父类,为什么它们没有相同的行为呢?

我试图以不同方式重写代码,但错误仍然存​​在。有人可以看到可能出错的地方吗?

我知道有一堆正式的约定错误(缩进,varabel名称等)

以下是三个类: 首先是父类:

public abstract class SubMenuWindow extends JFrame {
protected BankLogic bank = new BankLogic(); 
private JPanel customersDisplayPanel; 
private JLabel customersDisplayLabel; 
JTextArea customersDisplayText; 
private GridBagLayout gridBag;
private GridBagConstraints customGrid; 
private FlowLayout layout; 
private JScrollPane scrollBarDisplayText;  
private static final int TEXT_LENGTH = 30;  
private static final int ROWS = 20;  
private static final int COLUMNS = 50;
private static final int FRAME_HEIGHT = 900;
private static final int FRAME_WIDTH = 1600;

public SubMenuWindow(String title) { 
    super(title);
    createComponents();
    FlowLayout layout = new FlowLayout();
    this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    this.setVisible(false); 
    this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setLayout(layout);//
    this.add(customersDisplayPanel);
    }   

private void createComponents(){
    gridBag = new GridBagLayout();
    customGrid = new GridBagConstraints(); 
    customersDisplayPanel = new JPanel(gridBag); 
    customersDisplayLabel = new JLabel("Within this frame you can find all 
    the Customers in the bank");    
    setCustomersDisplayText(new JTextArea(ROWS,COLUMNS));
    scrollBarDisplayText = new JScrollPane(getCustomersDisplayText());
    customGrid.gridx = 0;
    customGrid.gridy = 1;
    customersDisplayPanel.add(customersDisplayLabel, customGrid); 
    customGrid.gridx = 0;
    customGrid.gridy = 2;
    customersDisplayPanel.add(scrollBarDisplayText,customGrid); 
}

public void createBankLogicCustomer(String firstNameString, String 
surNameString, String pnumString ){
bank.createCustomer(firstNameString, surNameString, pnumString); 
updateCustomersTextArea();
}

public void updateCustomersTextArea(){

    customersDisplayText.setText("");
    for(String customer : bank.getAllCustomers()){
        customersDisplayText.append(customer + "\n");
    }
}
    public BankLogic getBank(){
    return bank; 
}

public static int getTextLength() {
    return TEXT_LENGTH;
}

public JTextArea getCustomersDisplayText() {
    return customersDisplayText;
}

public void setCustomersDisplayText(JTextArea customersDisplayText) {
    this.customersDisplayText = customersDisplayText;
}
}

第二课

public class CustomerSubWindow extends SubMenuWindow {

private JPanel nameInputPanel;  
private JTextField firstNameTextField;
private JTextField surNameTextField; 
private JTextField pNumTextField;
private JLabel firstNameLabel;
private JLabel surNameLabel;
private JLabel pNumLabel;
private JButton finishButton; 
private ActionListener createCustomerButtonPressed;


public CustomerSubWindow(String title) {

    super(title);
    createComponents(); 
    this.add(nameInputPanel);

}
    public void createComponents(){
    nameInputPanel = new JPanel();
    firstNameTextField = new JTextField(super.getTextLength()); 
    surNameTextField= new JTextField(super.getTextLength());
    pNumTextField= new JTextField(super.getTextLength());
    firstNameLabel= new JLabel("First name: ");
    surNameLabel= new JLabel("Surname: ");
    pNumLabel= new JLabel("Person number: ");
    finishButton = new JButton("Create customer");
    createCustomerButtonPressed = new CustomerButtonListener();
    finishButton.addActionListener(createCustomerButtonPressed);
    nameInputPanel.add(firstNameLabel);
    nameInputPanel.add(firstNameTextField);
    nameInputPanel.add(surNameLabel);
    nameInputPanel.add(surNameTextField);
    nameInputPanel.add(pNumLabel);
    nameInputPanel.add(pNumTextField);
    nameInputPanel.add(finishButton);
}

private class CustomerButtonListener implements ActionListener {
    @Override
              public void actionPerformed(ActionEvent custButtonPressed){
            if (custButtonPressed.getSource()==finishButton){
            String firstNameString = firstNameTextField.getText();  
            String surNameString = surNameTextField.getText(); 
            String pnumString = pNumTextField.getText(); 
            createBankLogicCustomer(firstNameString, surNameString, 
            pnumString);
            updateCustomersTextArea();

        }
    }
}

}

最后是第三类

public class CreditAccountSubWindow extends SubMenuWindow {
private JPanel pNoInputPanel;  
private JTextField pNoTextField;
private JLabel pNoLabel;
private JButton pNoButton;
private ActionListener createCreditAccountButtonPressed;

public CreditAccountSubWindow(String title)
    super(title); 
    createComponents(); 
    this.add(pNoInputPanel); 
    }

public void createComponents(){


    pNoInputPanel = new JPanel(); 
    pNoTextField = new JTextField(super.getTextLength());
    pNoLabel = new JLabel("Input client ID number");
    pNoButton = new JButton("Create Account"); 
    createCreditAccountButtonPressed = new CreditAccButtonListener();
    pNoButton.addActionListener(createCreditAccountButtonPressed);
    pNoInputPanel.add(pNoLabel);
    pNoInputPanel.add(pNoTextField);
    pNoInputPanel.add(pNoButton);
}
private class CreditAccButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent creditAccButtonPressed){
        if (creditAccButtonPressed.getSource()==pNoButton){
            String pNo = pNoTextField.getText();
            getBank().createCreditAccount(pNo);  

            System.out.println(getBank().createCreditAccount(pNo));
            }
        }
    }

}

0 个答案:

没有答案