java - 我希望先删除按钮,然后执行函数 cam() 中的内容

标签 java eclipse user-interface

代码:

p.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        System.out.println("Welcome to Guess the number Game");
        System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");

        ne.remove(p);

        // ne.removeAll();
        ne.revalidate();
        ne.repaint();
    }

    public void cam() {

        gamer2 game = new gamer2();
        game.generatenum();
    }
});

p 是一个 JButton。

  • 我没有在 actionPerformed() 中调用 cam() 的原因是,如果我这样做,按钮只会在 generatenum() 已执行。我希望先删除该按钮。

最佳答案

cam() 方法属于匿名类(ActionListener 的子类),在这个匿名类中没有方法调用 cam()。这就是为什么你收到该警告

在我看来,你应该这样做

p.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        System.out.println("Welcome to Guess the number Game");
        System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");

        ne.remove(p);

        // ne.removeAll();
        ne.revalidate();
        ne.repaint();
        cam();
    }

});

public void cam() {

    gamer2 game = new gamer2();
    game.generatenum();
}

或者你可以这样做

p.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        System.out.println("Welcome to Guess the number Game");
        System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");

        ne.remove(p);

        // ne.removeAll();
        ne.revalidate();
        ne.repaint();
        cam();
    }

    public void cam() {

        gamer2 game = new gamer2();
        game.generatenum();
    }
});

关于java - 我希望先删除按钮,然后执行函数 cam() 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525386/

相关文章:

java - 将 .ser 文件从以前的项目复制粘贴到新项目时出现 ClassnotfoundException

ruby - Ruby 的默认 GUI 框架是什么?

Java-选项卡上的按键监听器不起作用

java - 使用 java 在 JavaScript 中创建文件

html - 带有 HTML/JS/CSS 等语法高亮的 Eclipse Indigo

java - Eclipse SWT : How to disable scrolledcomposite scrolling on mousewheel

java - 在 swing 中居中组件并调整 JFrame 大小

java - 从 Protractor 运行 Java 程序

java - 在 Liferay 中搜索组织下的用户

java - Jpa 通过字段子项获取父项