我正在为我的第一个java类编写一个游戏。我必须创建两个不同的按钮与两个不同的行动听众。按钮从emptybutton扩展而来,每个按钮都有一个不同的侦听器。
我遇到的问题是,当我尝试在空按钮侦听器中设置测试时,它会覆盖空按钮侦听器。例如,宝藏按钮文本设置为“$”。如果我将空按钮文本设置为“x”,那么所有按钮都显示为“x”,即使它是一个宝藏。我还必须创建最后一个移动标签和文本字段。当我在空按钮监听器下运行该方法以“半身像”的形式执行最后一步时,即使我发现了宝藏,它也只会说“半身像”。我在我的treasurebuttonlistener类中尝试了@override,但它没有改变任何东西。有什么指导吗?
import javax.swing.*;
public class EmptyButton extends JButton
{
//has-a TreasureGame
private TreasureGame game;
//has-a TreasureGameView
private TreasureGameView gameView;
public EmptyButton(TreasureGame game, TreasureGameView gameView)
{
super();
setText("X");
this.game = game;
this.gameView = gameView;
addActionListener(new EmptyButtonListener(this, game, gameView));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
public class TreasureButton extends EmptyButton
{
public TreasureButton(TreasureGame game, TreasureGameView gameView)
{
super(game, gameView);
//setText("$");
addActionListener(new TreasureButtonListener(this, game, gameView));
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EmptyButtonListener implements ActionListener
{
private EmptyButton button;
//has-a TreasureGame
private TreasureGame game;
//has-a TreasureGameView
private TreasureGameView gameView;
public EmptyButtonListener(EmptyButton button, TreasureGame game, TreasureGameView gameView)
{
this.game = game;
this.button = button;
this.gameView = gameView;
}
public void actionPerformed(ActionEvent e)
{
//button.setText("X");
button.setEnabled(false);
game.reduceTriesLeft();
//game.treasureNotFound();
game.isGameOver();
gameView.updateUI();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TreasureButtonListener implements ActionListener
{
// Has-a TreasureButton
private TreasureButton button;
private TreasureGame game;
private TreasureGameView gameView;
public TreasureButtonListener(TreasureButton button, TreasureGame game, TreasureGameView gameView)
{
this.button = button;
this.game = game;
this.gameView = gameView;
}
public void actionPerformed(ActionEvent e)
{
button.setText("$");
game.reduceNumberStillHidden();
game.treasureFound();
game.isGameOver();
gameView.updateUI();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!