在超类的构造函数中,我有通向其他子类的按钮。然而,这些子类也继承了这些按钮,即使它们不在它们的构造函数中。如何从我的子类中删除这些按钮?我正在使用 javax.swing。
好的,这是一个代码示例。我很确定我在做 OOP 完全错误......
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Big extends JFrame implements ActionListener
{
static private JPanel p1;
static private JButton b1;
public Big(){
setSize(700,400);
p1 = new JPanel();
b1 = new JButton("Go to subclass");
b1.addActionListener(this);
p1.add(b1);
add(p1, BorderLayout.NORTH);
setVisible(true);
}
public void actionPerformed (ActionEvent e){
if(e.getSource() == b1){
setVisible(false);
Small subclass = new Small();
}
}
}
第二类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Small extends Big implements ActionListener
{
static private JButton b2;
static private JPanel p2;
public Small(){
setSize(700,400);
p2 = new JPanel();
b2 = new JButton("Hello!");
p2.add(b2);
add(p2, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed (ActionEvent e){}
}