java - 如何将 ActionEvent 添加到 JButton?井字游戏
问题描述
我是编码新手,并试图为课堂创建一个简单的井字游戏,我想因为我需要使用 GUI,所以我可以让板子成为按钮网格。不幸的是,我在让按钮做任何事情时遇到问题。我想当有人点击它时它变成一个 X,然后下一个点击它的人变成一个 O。任何帮助都会很棒。
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import java.awt.font.*;
import javafx.animation.TranslateTransition;
import javafx.scene.layout.GridPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ShadowT extends JFrame {
//creates 9 buttons
private static JButton buttons[] = new JButton[9];
//sets counter for amount of times a button has been clicked on
public static int counter = 0;
public static String letter;
public static void main(String[] args){
//creates grid of 3x3
int rows = 3;
int cols = 3;
ShadowT grid = new ShadowT(rows,cols);
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid.pack();
grid.setVisible(true);
}
public ShadowT(int rows, int cols){
//creates pane with grid and adds in the buttons
Container pane = getContentPane();
pane.setLayout(new GridLayout(rows,cols));
for(int i = 0; i < 9; i++){
JButton button = new JButton(Integer.toString((i+1)));
pane.add(button);
}
}
}
解决方案
每个 JButton 都需要一个带有自定义方法的ActionListener(可能相同) 。actionPerformed
例如,对于名为 button1 的 JButton,您可以编写
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* do some stuff when the button is clicked */
}
});
另请参阅Java 教程中的如何编写动作侦听器,如果需要,请参阅如何在 SO 上添加侦听多个按钮的动作侦听器。
这是一个例子:
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Grid extends JFrame implements ActionListener {
private static JButton buttons[][];
public static int counter = 0;
public static void main(String[] args){
Grid grid = new Grid(3, 3);
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid.pack();
grid.setVisible(true);
}
public Grid(int rows, int cols) {
Container pane = getContentPane();
pane.setLayout(new GridLayout(rows, cols));
buttons = new JButton[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JButton button = new JButton(i + "," + j);
button.addActionListener(this);
pane.add(button);
buttons[i][j] = button;
}
}
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (o instanceof JButton) {
JOptionPane.showMessageDialog(null, ((JButton)o).getText());
}
}
}
推荐阅读
- android - Flutter 应用程序无法跳过老用户的介绍屏幕
- amazon-web-services - 如何在 CloudFormation 中使用 OR 来设置多个条件?
- nopcommerce-3.90 - nopCommerce 3.90 HtmlHelper 不在管理视图中定义
- ios - WebRTC的add track在iOS swift Application中产生乱码
- list - 如何从元组列表中删除给定元素并在 Haskell 中更新它?
- python - 文件中列出的 FFmpeg 导出帧(包括重复项)
- python-3.x - 获取访问页面时加载的 xhr 文档
- json - 在警报对话框中删除从 json 解析的 null 或空值
- python - 如何通过在数据框中的两列上应用条件来选择行
- c# - 在 TinkerGraph 中使用字符串 ID