首页 > 解决方案 > 如何将 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); 
       }
   }  
}

标签: javaarraysswingjbuttonactionlistener

解决方案


每个 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());
        }
    }
}

推荐阅读