简体   繁体   English

获取在其自己的类中定义的鼠标按下事件内的框架中的组件值

[英]Getting values of components in a frame inside a mouse press event defined in its own class

I am trying to create a tic tac toe GUI that needs to be set up as follows: 我正在尝试创建一个需要设置如下的tic tac toe GUI:

1) The frame is a grid made of 2 panes, left pane with a 9 button grif that acts as the board 2) The right pane pane is divided into two more panes 1 below the other. 1)框架是由2个窗格构成的网格,左窗格具有9按钮格栅,其充当板2)右窗格窗格被分成另外两个窗格1。 Based on the symbol the user chooses using the radio buttons, a click on the gameboard must display either "X" or "O". 根据用户使用单选按钮选择的符号,游戏板上的单击必须显示“X”或“O”。

Since my mouse listener code is in a separate class I am not sure how I can get what the user clicked. 由于我的鼠标监听器代码在一个单独的类中,我不知道如何获得用户单击的内容。

Here is my code. 这是我的代码。 Please give me a nudge in the right direction to overcome this issue. 请给我一个正确的方向来克服这个问题。

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

You can create a variable within your MouseAdapterMod, and assign whatever you need (eg the GUI) to it (or a reference of a JButton) through the constructor. 您可以在MouseAdapterMod中创建一个变量,并通过构造函数为其分配所需的任何内容(例如GUI)(或JButton的引用)。

You can then use that to access any information you need during runtime clicking. 然后,您可以使用它来访问运行时单击期间所需的任何信息。

Example: 例:

Note: Use getter methods as you wish. 注意:根据需要使用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);
 }
}  

My suggestion is to use an array of JButtons combined with ActionListener. 我的建议是使用JButton数组和ActionListener。 Try out the following quick SSCCE I just wrote and see if it will give you some good ideas: (Once you click any of the 9 buttons, the console shall write the button number) 试试我刚刚写的以下快速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);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM