【问题标题】:How does ButtonListener() work when it is both a parent and a child class?当 ButtonListener() 既是父类又是子类时,它是如何工作的?
【发布时间】:2019-05-25 01:15:43
【问题描述】:

我有一个学校的终极项目,我必须包含我本学期学到的尽可能多的元素,其中 2 个是方法和 gui。在我正在尝试制作的程序中,父类和子类中都有一个 ButtonListener,但父类中存在错误:nameConfirm.addActionListener(new ButtonListener()) 表示 ButtonListener 无法解析为一种类型。我知道这与层次结构有关,但我不知道应该如何解决。

我有一个模糊的想法,有些人使用“this”或覆盖,但我真的不明白它们是如何工作的。

这是父类:

public void startGame() {
    JPanel namePanel = new JPanel();
    JButton nameConfirm = new JButton("ask");
    nameConfirm.addActionListener(new ButtonListener());
    namePanel.add(nameConfirm);
    setContentPane(namePanel);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(800, 175);
    setResizable(false);
    setLocationRelativeTo(null);
    class ButtonListener implements ActionListener {
    }
}

这是子类:

public cpt2() {
    JPanel startingPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    newGame = new JButton("New Game");
    newGame.addActionListener(new ButtonListener());
    buttonPanel.add(newGame);
    checkStats = new JButton("Statistics");
    checkStats.addActionListener(new ButtonListener());
    buttonPanel.add(checkStats);
    exit = new JButton("Exit");
    exit.addActionListener(new ButtonListener());
    buttonPanel.add(exit);
    startingPanel.add(buttonPanel);
    setContentPane(startingPanel);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(1110, 375);
    setResizable(false);
    setLocationRelativeTo(null);
public class ButtonListener extends JFrame implements ActionListener{
    public void actionPerformed(ActionEvent a) {
        if (a.getSource() == newGame){
            cptRun.startGame(); 

        }
        else if (a.getSource() == checkStats){

        }
        else if (a.getSource() == exit){
            System.exit(0);
        }
    }
}
}

我希望摆脱错误并为父类和子类中的不同 JPanel 提供工作按钮。

【问题讨论】:

  • 哪行代码导致错误?
  • nameConfirm.addActionListener(new ButtonListener());
  • 这是How to Write an Action Listener 上 Oracle 的 Java 教程主题的链接。您可能还想用类似的查询搜索网络 (Google) 以找到更多示例。

标签: java swing user-interface awt actionlistener


【解决方案1】:

这是父类

这是一个方法,您在方法中内嵌了class

更常见的方法是

nameConfirm.addActionListener(new ActionListener() {
    // Implement ActionListener methods here, within anonymous class
});

或者将匿名类移动到变量中

final ActionListener buttonListener = new ActionListener() {
    // Implement ActionListener methods here, within anonymous class
};
nameConfirm.addActionListener(buttonListener);

这是子类

不清楚这里是哪个类。你有一个构造函数cpt2public class ButtonListener extends JFrame implements ActionListener

我想你想要的是这个,例如

public class Cpt2Frame extends JFrame implements ActionListener {
    public Cpt2Frame() {
        // some button
        someButton.addActionListener(Cpt2Frame.this); // 'this' class 'is-a' ActionListener
    }

    // Implement ActionListener methods within defined class
}

或者你可以创建一个单独的文件ButtonListener.java,它不应该是一个JFrame,只是一个监听器接口

public class ButtonListener implements ActionListener {

}

那么,你可以addActionListener(new ButtonListener())

【讨论】:

  • 那么,您所指的是,无论我想对按钮执行什么操作,我都会在父类中初始化操作侦听器时将其放入{}?另外,我意识到当我试图将代码精简到必要的程度时,我混淆了其中一个括号。是否可以再次查看子类?
  • 我认为您的问题不在于专利/子类。只有一种方法可以将动作侦听器添加到按钮。您不应该在方法中以class 开头一行,每个Java 文件只能有一个顶级public class
猜你喜欢
  • 2019-02-25
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多