如何将ActionEvent添加到JButtons?井字游戏

时间:2018-12-13 14:53:59

标签: java arrays swing jbutton actionlistener

我是编码的新手,尝试为班级创建一个简单的井字游戏,因此我想起了这一点,因为我需要使用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); 
       }
   }  
}

2 个答案:

答案 0 :(得分:0)

每个JButton需要一个ActionListener(可能是相同的)以及一个自定义的actionPerformed方法。

例如,对于一个名为button1的JButton,您可以编写

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        /* do some stuff when the button is clicked */
    }
});

另请参见Java教程中的How to Write an Action Listener,如果需要,请参阅How to add action listener that listens to multiple buttons

这里是一个例子:

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

答案 1 :(得分:0)

这是一个简单的Java GUI应用程序示例-并非完美,但仅用于演示您需要的内容。

您需要实现ActionListener并编写实现ActionListener时所需的actionPerformed()。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class HandleActionEventsForJButton extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    public HandleActionEventsForJButton() {

        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());

        JButton button1 = new JButton("Yes");
        JButton button2 = new JButton("No");

        //set action listeners for buttons
        button1.addActionListener(this);
        button2.addActionListener(this);

        //add buttons to the frame
        add(button1);
        add(button2);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();
        if (action.equals("Yes")) {
            System.out.println("Yes Button pressed!");
        }
        else if (action.equals("No")) {
            System.out.println("No Button pressed!");
        }
    }

    private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new HandleActionEventsForJButton();

  //Display the window.

  frame.pack();

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}