0

我正在尝试创建一个需要按如下方式设置的井字游戏 GUI:

1) 框架是由 2 个窗格组成的网格,左窗格带有 9 个按钮 grif,用作板 2) 右窗格窗格分为两个窗格 1 在另一个下方。根据用户使用单选按钮选择的符号,在游戏板上单击必须显示“X”或“O”。

由于我的鼠标侦听器代码在一个单独的类中,我不确定如何获得用户点击的内容。

这是我的代码。请在正确的方向上轻推我以克服此问题。

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 package samples;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

/**
*
* @author saikrishnan.srivat
*/
public class TicTacToe extends JFrame {

/*declare the components of the window*/
JPanel iconPanel;
JPanel tictactoeBoard;
JPanel emptyPanel;
JLabel chooseLabel;
JRadioButton xButton;
JRadioButton oButton;
ButtonGroup bg;
JButton resetButton;
JButton exitButton;
JButton undoButton;
JSplitPane rightPane;
JSplitPane pane;
MouseAdapterMod mam;

public TicTacToe() {

    iconPanel = new JPanel();
    tictactoeBoard = new JPanel();
    emptyPanel = new JPanel();
    chooseLabel = new JLabel("Choose your symbol :");
    xButton = new JRadioButton("X");
    oButton = new JRadioButton("O");
    bg = new ButtonGroup();
    bg.add(xButton);
    bg.add(oButton);

    /*add the label and the radio buttons too the empty panel*/
    emptyPanel.add(chooseLabel);
    emptyPanel.add(xButton);
    emptyPanel.add(oButton);
    emptyPanel.setLayout(new FlowLayout());
    /*add the exit,undo and reset buttons to the icon panel*/
    iconPanel.setLayout(new GridLayout(3, 1, 3, 3));
    resetButton = new JButton("Reset");
    exitButton = new JButton("Exit");
    undoButton = new JButton("Undo Move");
    iconPanel.add(resetButton);
    iconPanel.add(exitButton);
    iconPanel.add(undoButton);

    /*Set layout of the tictactoe board and add the game buttons to it */
    tictactoeBoard.setLayout(new GridLayout(3, 3));
    /* Mouse adapter object that listens to the buttons in the tic tac toe board */
    mam = new MouseAdapterMod();
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton("");
        button.addMouseListener(mam);
        tictactoeBoard.add(button);     
    }
    /*add the icon panel and the empty panel to the right pane*/
    rightPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, iconPanel, emptyPanel);

    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tictactoeBoard, rightPane);
    add(pane);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    //setMaximumSize(new Dimension(500, 500));
    setMinimumSize(new Dimension(700, 500));
    setLocationRelativeTo(null);
    pack();
    setTitle("Tic Tac Toe");
    setLocationRelativeTo(null);
    setVisible(true);
 }
}  

class MouseAdapterMod extends MouseAdapter {
/*Set 'X' or 'O' based the selected radio button- how to achieve this? */
    public void mousePressed(MouseEvent e) {
    //JButton button = (JButton)e.getSource();
    //button.setText("");
    }
}
4

2 回答 2

1

您可以在 MouseAdapterMod 中创建一个变量,并通过构造函数将您需要的任何内容(例如 GUI)分配给它(或 JButton 的引用)。

然后,您可以使用它在运行时单击期间访问所需的任何信息。

例子:

注意:根据需要使用 getter 方法。

class MouseAdapterMod extends MouseAdapter {
/*Set 'X' or 'O' based the selected radio button- how to achieve this? */

    JRadioButton xButton;

    public MouseAdapterMod(JRadioButton xButton)
    {
        this.xButton = xButton;
    }

    public void mousePressed(MouseEvent e) {
     JButton button = (JButton)e.getSource();

     if(xButton.isSelected())
     button.setText("X");
     else
     button.setText("O");    
    }
}

/**
*
* @author saikrishnan.srivat
*/
public class TicTacToe extends JFrame {

/*declare the components of the window*/
JPanel iconPanel;
JPanel tictactoeBoard;
JPanel emptyPanel;
JLabel chooseLabel;
JRadioButton xButton;
JRadioButton oButton;
ButtonGroup bg;
JButton resetButton;
JButton exitButton;
JButton undoButton;
JSplitPane rightPane;
JSplitPane pane;
MouseAdapterMod mam;


public static void main(String args[])
{
    new TicTacToe();
}

public TicTacToe() {

    iconPanel = new JPanel();
    tictactoeBoard = new JPanel();
    emptyPanel = new JPanel();
    chooseLabel = new JLabel("Choose your symbol :");
    xButton = new JRadioButton("X");
    oButton = new JRadioButton("O");
    bg = new ButtonGroup();
    bg.add(xButton);
    bg.add(oButton);

    /*add the label and the radio buttons too the empty panel*/
    emptyPanel.add(chooseLabel);
    emptyPanel.add(xButton);
    emptyPanel.add(oButton);
    emptyPanel.setLayout(new FlowLayout());
    /*add the exit,undo and reset buttons to the icon panel*/
    iconPanel.setLayout(new GridLayout(3, 1, 3, 3));
    resetButton = new JButton("Reset");
    exitButton = new JButton("Exit");
    undoButton = new JButton("Undo Move");
    iconPanel.add(resetButton);
    iconPanel.add(exitButton);
    iconPanel.add(undoButton);

    /*Set layout of the tictactoe board and add the game buttons to it */
    tictactoeBoard.setLayout(new GridLayout(3, 3));
    /* Mouse adapter object that listens to the buttons in the tic tac toe board */
    mam = new MouseAdapterMod(xButton);
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton("");
        button.addMouseListener(mam);
        tictactoeBoard.add(button);     
    }
    /*add the icon panel and the empty panel to the right pane*/
    rightPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, iconPanel, emptyPanel);

    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tictactoeBoard, rightPane);
    add(pane);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    //setMaximumSize(new Dimension(500, 500));
    setMinimumSize(new Dimension(700, 500));
    setLocationRelativeTo(null);
    pack();
    setTitle("Tic Tac Toe");
    setLocationRelativeTo(null);
    setVisible(true);
 }
}  
于 2013-09-21T16:31:54.260 回答
1

我的建议是使用 JButtons 数组和 ActionListener。试试下面我刚刚写的快速SSCCE,看看它是否会给你一些好主意:(一旦你点击了9个按钮中的任何一个,控制台就会写下按钮编号)

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

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

class test extends JFrame implements ActionListener
{
JButton button[] = new JButton[9];
public test()
{
    init();
    initializeAndAddButtons();
}

private void init()
{
    this.setLayout(new GridLayout(3, 1, 3, 3));
    this.setSize(600,600);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setTitle("Test");
}

private void initializeAndAddButtons()
{
    for(int i = 0; i < 9;i++)
    {
        button[i] = new JButton(String.valueOf(i));
        button[i].addActionListener(this);
        add(button[i]);
    }
}

public void actionPerformed(ActionEvent e) 
{
    JButton tempButton = (JButton) e.getSource();
    System.out.println(tempButton.getText());
}
}

public class main
{
   public static void main(String args[])
   {
    new test().setVisible(true);
   }
}
于 2013-09-21T19:48:20.900 回答