带有jbutton的actionlistener问题

ccgok5k5  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(222)

我正在为我的第一个java类编写一个游戏。我必须创建两个不同的按钮与两个不同的行动听众。按钮从emptybutton扩展而来,每个按钮都有一个不同的侦听器。
我遇到的问题是,当我尝试在空按钮侦听器中设置测试时,它会覆盖空按钮侦听器。例如,宝藏按钮文本设置为“$”。如果我将空按钮文本设置为“x”,那么所有按钮都显示为“x”,即使它是一个宝藏。我还必须创建最后一个移动标签和文本字段。当我在空按钮监听器下运行该方法以“半身像”的形式执行最后一步时,即使我发现了宝藏,它也只会说“半身像”。我在我的treasurebuttonlistener类中尝试了@override,但它没有改变任何东西。有什么指导吗?

  1. import javax.swing.*;
  2. public class EmptyButton extends JButton
  3. {
  4. //has-a TreasureGame
  5. private TreasureGame game;
  6. //has-a TreasureGameView
  7. private TreasureGameView gameView;
  8. public EmptyButton(TreasureGame game, TreasureGameView gameView)
  9. {
  10. super();
  11. setText("X");
  12. this.game = game;
  13. this.gameView = gameView;
  14. addActionListener(new EmptyButtonListener(this, game, gameView));
  15. }
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. public class TreasureButton extends EmptyButton
  19. {
  20. public TreasureButton(TreasureGame game, TreasureGameView gameView)
  21. {
  22. super(game, gameView);
  23. //setText("$");
  24. addActionListener(new TreasureButtonListener(this, game, gameView));
  25. }
  26. }
  27. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. import java.awt.*;
  29. import java.awt.event.*;
  30. import javax.swing.*;
  31. public class EmptyButtonListener implements ActionListener
  32. {
  33. private EmptyButton button;
  34. //has-a TreasureGame
  35. private TreasureGame game;
  36. //has-a TreasureGameView
  37. private TreasureGameView gameView;
  38. public EmptyButtonListener(EmptyButton button, TreasureGame game, TreasureGameView gameView)
  39. {
  40. this.game = game;
  41. this.button = button;
  42. this.gameView = gameView;
  43. }
  44. public void actionPerformed(ActionEvent e)
  45. {
  46. //button.setText("X");
  47. button.setEnabled(false);
  48. game.reduceTriesLeft();
  49. //game.treasureNotFound();
  50. game.isGameOver();
  51. gameView.updateUI();
  52. }
  53. }
  54. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. import java.awt.*;
  56. import java.awt.event.*;
  57. import javax.swing.*;
  58. public class TreasureButtonListener implements ActionListener
  59. {
  60. // Has-a TreasureButton
  61. private TreasureButton button;
  62. private TreasureGame game;
  63. private TreasureGameView gameView;
  64. public TreasureButtonListener(TreasureButton button, TreasureGame game, TreasureGameView gameView)
  65. {
  66. this.button = button;
  67. this.game = game;
  68. this.gameView = gameView;
  69. }
  70. public void actionPerformed(ActionEvent e)
  71. {
  72. button.setText("$");
  73. game.reduceNumberStillHidden();
  74. game.treasureFound();
  75. game.isGameOver();
  76. gameView.updateUI();
  77. }
  78. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题