为什么方法覆盖不能在我的java代码中工作?

时间:2015-01-17 09:04:22

标签: java methods override method-overriding

所以这是我在Java中的代码,我想知道为什么覆盖actionCerformed方法的thirdClass永远不会起作用?始终使用firstPlass的actionPerformed方法。我该如何更改代码? 我会非常感谢你的帮助:)。

这是我的第一课:

package Tehran;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class firstClass extends JFrame {

    public JTextField item1;
    public JTextField item2;
    public JTextField item3;

    public JPasswordField passwordField;

    public firstClass() {
        super("here is the Title of the Our Pages");

        setLayout(new FlowLayout());

        item1 = new JTextField(10);
        add(item1);

        item2 = new JTextField("unediteable", 20);
        item2.setEnabled(false);
        add(item2);

        item3 = new JTextField("Enter ur Text Here");
        add(item3);

        passwordField = new JPasswordField("mypass");
        add(passwordField);

        passwordField = new JPasswordField("mypass");
        add(passwordField);

        thehandler handler = new thehandler ();
        item1.addActionListener(handler);
        item2.addActionListener(handler);
        item3.addActionListener(handler);

        passwordField.addActionListener(handler);



    }

    public class thehandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            String string = "";

            if (event.getSource() == item1)
                string = String.format("field 1: %s", event.getActionCommand());
            else if (event.getSource() == item2)
                string = String.format("field 2: %s", event.getActionCommand());
            else if (event.getSource() == item3)
                string = String.format("field 3: %s", event.getActionCommand());
            else if (event.getSource() == passwordField)
                string = String.format("Password Field is : %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);


        }
    }


}

这是我的第三课:

package Tehran;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class thirdClass extends firstClass{

    public class thehandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {

            String string = "";

            if (event.getSource() == item1)
                string = String.format("field 1 inherited from first class babe : %s", event.getActionCommand());
            else if (event.getSource() == item2)
                string = String.format("field 2 inherited from first class babe : %s", event.getActionCommand());
            else if (event.getSource() == item3)
                string = String.format("field 3 inherited from first class babe : %s", event.getActionCommand());
            else if (event.getSource() == passwordField)
                string = String.format("Password Field inherited from first class babe  is : %s", event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);


        }
    }

}

这是我的主要班级:

package Tehran;

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class mansour {
    public static void main(String args[]) {


        thirdClass mansour[] = new thirdClass[2];
        thirdClass jaxi = new thirdClass();



        mansour[1] = new thirdClass();
        mansour[1].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mansour[1].setSize(350, 150);
        mansour[1].setVisible(true);


    }
}

2 个答案:

答案 0 :(得分:0)

我认为你想要覆盖" thehandler"你的第三课中的课程。

在这种情况下,创建一个创建theHandler实例并在thirdClass中覆盖此方法的新方法是一种好方法。

public class thirdClass extends firstClass {
    ...
    @Override
    public ActionListener createActionListener() {
        return new theHandler();
    }
}

和firstClass应如下所示:

public class firstClass exteds JFrame {
    ...
    public firstClass() {
        ...
        thehandler handler = createActionListener();
        item1.addActionListener(handler);
        ...
    }

    public ActionListener createActionListener() {
        return new theHandler();
    }
    ...
}

答案 1 :(得分:0)

让类本身实现ActinoListener:

public class firstClass extends JFrame implements ActionListener {

...

    item1.addActionListener(this);
    item2.addActionListener(this);
    item3.addActionListener(this);

    passwordField.addActionListener(this);


}

public void actionPerformed(ActionEvent event) {

    String string = "";

    if (event.getSource() == item1)
        string = String.format("field 1: %s", event.getActionCommand());
    else if (event.getSource() == item2)
        string = String.format("field 2: %s", event.getActionCommand());
    else if (event.getSource() == item3)
        string = String.format("field 3: %s", event.getActionCommand());
    else if (event.getSource() == passwordField)
        string = String.format("Password Field is : %s", event.getActionCommand());

    JOptionPane.showMessageDialog(null, string);


}

public class thirdClass extends firstClass {

@Override
public void actionPerformed(ActionEvent event) {

    String string = "";

    if (event.getSource() == item1)
        string = String.format("field 1 inherited from first class babe : %s", event.getActionCommand());
    else if (event.getSource() == item2)
        string = String.format("field 2 inherited from first class babe : %s", event.getActionCommand());
    else if (event.getSource() == item3)
        string = String.format("field 3 inherited from first class babe : %s", event.getActionCommand());
    else if (event.getSource() == passwordField)
        string = String.format("Password Field inherited from first class babe  is : %s", event.getActionCommand());

    JOptionPane.showMessageDialog(null, string);


}

}