Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取在其自己的类中定义的鼠标按下事件内的帧中的组件值_Java_Swing - Fatal编程技术网

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

Java 获取在其自己的类中定义的鼠标按下事件内的帧中的组件值,java,swing,Java,Swing,我正在尝试创建一个tic-tac-toe GUI,需要按照以下方式进行设置: 1) 框架是一个由2个窗格组成的网格,左窗格有一个9个按钮的网格,用作板 2) 右窗格分为另外两个窗格1,位于另一个窗格下方。根据用户使用单选按钮选择的符号,点击游戏板必须显示“X”或“O” 由于我的鼠标侦听器代码位于一个单独的类中,我不确定如何获得用户单击的内容 这是我的密码。请给我一个正确的方向来克服这个问题 * To change this template, choose Tools | Templates

我正在尝试创建一个tic-tac-toe GUI,需要按照以下方式进行设置:

1) 框架是一个由2个窗格组成的网格,左窗格有一个9个按钮的网格,用作板 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("");
    }
}
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装样品;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入javax.swing.ButtonGroup;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JLayeredPane;
导入javax.swing.JPanel;
导入javax.swing.JRadioButton;
导入javax.swing.JSplitPane;
在关闭时导入静态javax.swing.WindowConstants.DISPOSE\u;
/**
*
*@作者saikrishnan.srivat
*/
公共类TicTacToe扩展JFrame{
/*声明窗口的组件*/
JPanel-iconPanel;
JPanel TictoeBoard;
JPanel-emptyPanel;
JLabel-chooseLabel;
JRadioButton xButton;
JRadioButton-obbutton;
按钮组bg;
JButton重置按钮;
JButton exitButton;
JButton按钮;
JSplitPane右窗格;
JSplitPane窗格玻璃;
MouseAdapterMod mam;
公共交通{
iconPanel=newjpanel();
tictactoeBoard=新的JPanel();
emptyPanel=newjpanel();
chooseLabel=newjlabel(“选择您的符号:”);
xButton=新的JRadioButton(“X”);
oButton=新JRadioButton(“O”);
bg=新按钮组();
bg.add(xButton);
bg.add(oButton);
/*将标签和单选按钮也添加到空面板中*/
emptyPanel.add(选择标签);
清空面板。添加(xButton);
清空面板。添加(对象按钮);
emptyPanel.setLayout(新的FlowLayout());
/*将退出、撤消和重置按钮添加到图标面板*/
setLayout(新的GridLayout(3,1,3,3));
resetButton=新按钮(“重置”);
exitButton=新JButton(“退出”);
undoButton=新的JButton(“撤消移动”);
iconPanel.add(重置按钮);
iconPanel.add(exitButton);
iconPanel.add(撤消按钮);
/*设置Tictaoe板的布局,并将游戏按钮添加到其中*/
tictactoeBoard.setLayout(新网格布局(3,3));
/*鼠标适配器对象,用于侦听tic-tac趾板中的按钮*/
mam=新的MouseAdapterMod();
对于(int i=0;i<9;i++){
JButton按钮=新JButton(“”);
按钮。添加鼠标侦听器(mam);
tictactoeBoard.add(按钮);
}
/*将图标面板和空面板添加到右侧窗格*/
右窗格=新的JSplitPane(JSplitPane.VERTICAL_SPLIT、iconPanel、emptyPanel);
窗格=新的JSplitPane(JSplitPane.HORIZONTAL_SPLIT,TictoeBoard,右窗格);
添加(窗格);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(400400);
//设置最大尺寸(新尺寸(500500));
设置最小尺寸(新尺寸(700500));
setLocationRelativeTo(空);
包装();
SETTILE(“Tic Tac Toe”);
setLocationRelativeTo(空);
setVisible(真);
}
}  
类MouseAdapterMod扩展了MouseAdapter{
/*根据所选单选按钮设置“X”或“O”-如何实现*/
公共无效鼠标按下(MouseEvent e){
//JButton按钮=(JButton)e.getSource();
//按钮。setText(“”);
}
}

您可以在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);
 }
}  
类MouseAdapterMod扩展了MouseAdapter{
/*根据所选单选按钮设置“X”或“O”-如何实现*/
JRadioButton xButton;
公共MouseAdapterMod(JRadioButton xButton)
{
this.xButton=xButton;
}
公共无效鼠标按下(MouseEvent e){
JButton按钮=(JButton)e.getSource();
if(xButton.isSelected())
按钮设置文本(“X”);
其他的
按钮。设置文本(“O”);
}
}
/**
*
*@作者saikrishnan.srivat
*/
公共类TicTacToe扩展JFrame{
/*声明窗口的组件*/
JPanel-iconPanel;
JPanel TictoeBoard;
JPanel-emptyPanel;
JLabel-chooseLabel;
JRadioButton xButton;
JRadioButton-obbutton;
按钮组bg;
JButton重置按钮;
JButton exitButton;
JButton按钮;
JSplitPane右窗格;
JSplitPane窗格玻璃;
MouseAdapterMod mam;
公共静态void main(字符串参数[])
{
新的TicTacToe();
}
公共交通{
iconPanel=newjpanel();
tictactoeBoard=新的JPanel();
emptyPanel=newjpanel();
chooseLabel=newjlabel(“选择您的符号:”);
xButton=新的JRadioButton(“X”);
oButton=新JRadioButton(“O”);
bg=新按钮组();
bg.add(xButton);
bg.add(oButton);
/*将标签和单选按钮也添加到空面板中*/
emptyPanel.add(选择标签);
清空面板。添加(xButton);
清空面板。添加(对象按钮);
emptyPanel.setLayout(新的FlowLayout());
/*将退出、撤消和重置按钮添加到图标面板*/
setLayout(新的GridLayout(3,1,3,3));
resetButton=新按钮(“重置”);
exitButton=新JButton(“退出”);
undoButton=新的JButton(“撤消移动”);
iconPanel.add(重置按钮);
iconPanel.add(exitButton);
iconPanel.add(撤消按钮);
/*设置Tictaoe板的布局,并将游戏按钮添加到其中*/
tictactoeBoard.setLayout(新网格布局(3,3));
/*鼠标适配器对象,用于侦听tic-tac趾板中的按钮*/
mam=新的鼠标指针(xButton);
用于(int i)
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);
   }
}