【问题标题】:How to fix getSource() method to edit JButton Text in actionPerformed class?如何修复 getSource() 方法以在 actionPerformed 类中编辑 JButton 文本?
【发布时间】:2019-01-15 22:34:11
【问题描述】:

我必须在 Applet 中为 Ready to Program Java 1.42 中的学校项目创建一个井字游戏。在 actionPerformed 类中,我想简单地将左上角第一个正方形的文本设置/编辑为“X”。我在 JButton 上使用了 setActionCommand() 并在 actionPerformed 类中调用了 getActionCommand()。但是在我的 if 语句中,我使用 getSource() 以便我可以引用对象本身并设置/编辑文本。我不能一起使用 getActionCommand() 和 getSource() 吗?谢谢。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.JButton;

public class TicTacToe extends Applet implements ActionListener 
{
public void InitializeGame()
{

 setLayout(null);
 JButton Square1 = new JButton();
 Square1.setBackground(Color.white);
 Square1.setBounds(90,20,75,75);
 Square1.addActionListener(this);
 Square1.setActionCommand("s1");

 JButton Square2 = new JButton();
 Square2.setBackground(Color.white);
 Square2.setBounds(165,20,75,75);
 Square2.addActionListener(this);
 Square2.setActionCommand("s2");

 JButton Square3 = new JButton();
 Square3.setBackground(Color.white);
 Square3.setBounds(240,20,75,75);
 Square3.addActionListener(this);
 Square3.setActionCommand("s3");

 JButton Square4 = new JButton();
 Square4.setBackground(Color.white);
 Square4.setBounds(90,95,75,75);
 Square4.addActionListener(this);
 Square4.setActionCommand("s4");

 JButton Square5 = new JButton();
 Square5.setBackground(Color.white);
 Square5.setBounds(165,95,75,75);
 Square5.addActionListener(this);
 Square5.setActionCommand("s5");

 JButton Square6 = new JButton();
 Square6.setBackground(Color.white);
 Square6.setBounds(240,95,75,75);
 Square6.addActionListener(this);
 Square6.setActionCommand("s6");

 JButton Square7 = new JButton();
 Square7.setBackground(Color.white);
 Square7.setBounds(90,170,75,75);
 Square7.addActionListener(this);
 Square7.setActionCommand("s7");

 JButton Square8 = new JButton();
 Square8.setBackground(Color.white);
 Square8.setBounds(165,170,75,75);
 Square8.addActionListener(this);
 Square8.setActionCommand("s8");

 JButton Square9 = new JButton();
 Square9.setBackground(Color.white);
 Square9.setBounds(240,170,75,75);
 Square9.addActionListener(this);
 Square9.setActionCommand("s9");

 add(Square1);
 add(Square2);
 add(Square3);
 add(Square4);
 add(Square5);
 add(Square6);
 add(Square7);
 add(Square8);
 add(Square9);

}

public void init()
{
resize (400,300);
setBackground(Color.orange);

JButton Play = new JButton("Click to Play");                                 
Play.setBackground(Color.white);
Play.setForeground(Color.black);
Play.addActionListener(this);
add(Play);
}

public void actionPerformed(ActionEvent e)                         
{
removeAll();                                                       
setBackground(Color.pink);
InitializeGame();                                                   


if ( e.getActionCommand().equals( "s1" ) )
   {
   ((JButton)e.getSource()).setText("X");
   }

repaint(); 
} 

}

我希望在左上角的 JButton 上看到“X”。但是运行时文本不会出现。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 ..
  • 3) 学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。 4) 使用逻辑一致的形式缩进代码行和块。缩进是为了让代码流更容易理解!大多数 IDE 都有专门用于格式化代码的键盘快捷键。 5) 使用CardLayout,如this answer 所示。 ..
  • 6) 不要混合 Swing 和 AWT 组件。基于 Swing 的小程序有一个 javax.swing.JApplet

标签: java swing applet jbutton actionevent


【解决方案1】:

actionPerformed() 方法中,您需要在if 块中调用removeAll()setBackground()InitializeGame(),如下所示:

否则,每次单击任何按钮时,整个 UI 都会重置。 (因为您已将相同的ActionListener 添加到所有按钮中。)

public void actionPerformed(ActionEvent e)
{
  if (((JButton) e.getSource()).getText().equals("Click to Play")) {
    removeAll();
    setBackground(Color.pink);
    InitializeGame();
  }

  if ( e.getActionCommand().equals( "s1" ) )
  {
    ((JButton)e.getSource()).setText("X");
  }

  repaint();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2013-03-26
    • 2017-05-05
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多