代码:
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/