为什么 Method Override 在我的 java 代码中不起作用?
why Method Override is not working in my java code?
所以这是我在 Java 中的代码,我想知道为什么覆盖第三个 Class 的 actionPerformed 方法永远不起作用?始终运行 firstClass 的 actionPerformed 方法。我应该如何更改代码?
我将非常感谢你的帮助:)
这是我的第一个Class:
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);
}
}
}
这是我的第三个Class:
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);
}
}
}
这是我的主Class:
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);
}
}
我想你想在你的 thirdClass 中覆盖 "thehandler" class。
在这种情况下,创建一个创建 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();
}
...
}
使 类 本身实现 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);
}
}
所以这是我在 Java 中的代码,我想知道为什么覆盖第三个 Class 的 actionPerformed 方法永远不起作用?始终运行 firstClass 的 actionPerformed 方法。我应该如何更改代码? 我将非常感谢你的帮助:)
这是我的第一个Class:
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);
}
}
}
这是我的第三个Class:
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);
}
}
}
这是我的主Class:
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);
}
}
我想你想在你的 thirdClass 中覆盖 "thehandler" class。
在这种情况下,创建一个创建 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();
}
...
}
使 类 本身实现 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);
}
}