在ActionListener中引用单击JButton [英] Reference clicked JButton inside ActionListener

查看:71
本文介绍了在ActionListener中引用单击JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swing编写Tic Tac Toe程序,但似乎有些麻烦.在我的匿名内部类中,我尝试为每个按钮设置actionListener,但是在查找类型或变量时遇到了麻烦,该类型或变量将允许我引用按钮并将它们设置为X或Y.我的匿名类中的.getSource().setText(),但返回错误.有什么想法吗? 谢谢! 亚历克斯

I'm trying to write a Tic Tac Toe program using swing, but I seem to having some trouble. In my anonymous inner classes I attempt to set up the actionListener for each of my buttons, but I'm having trouble finding the type or the variable which will allow me to reference the buttons and set them to either X or Y. I tried e.getSource().setText() in my anonymous classes, but that came back with errors. Any thoughts? Thanks! Alex

import javax.swing.*;

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

public class TicTacToe  {

public JFrame frame;
public JLabel label;
public JPanel panel;

public static int counter;



public void go()
{ 
    frame = new JFrame("TicTacToe");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    panel.setLayout(new GridLayout(3,3,10,10));
    frame.add(BorderLayout.CENTER, panel);
    label= new JLabel("TIC TAC TOE");
    frame.add(BorderLayout.NORTH, label);

    ; 


    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 1");
    JButton button3 = new JButton("Button 1");
    JButton button4 = new JButton("Button 1");
    JButton button5 = new JButton("Button 1");
    JButton button6 = new JButton("Button 1");
    JButton button7 = new JButton("Button 1");
    JButton button8 = new JButton("Button 1");
    JButton button9 = new JButton("Button 1");





    button1.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e)
        {


        }
    });

    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    button9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {

        }
    });

    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(button7);
    panel.add(button8);
    panel.add(button9);
    frame.setVisible(true);
    panel.setVisible(true);

}


public static void main(String[] args)
{
    TicTacToe gui = new TicTacToe();
    gui.go();

}


}

推荐答案

请记住,ActionListener可以用于许多不同类型的组件,因此对源代码进行了概括.您需要将其投射回期望的值

Remember, ActionListener can be used on a number of different types of components, so the source reference is generalized. You need to cast to back to the expected value

button9.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source instanceof JButton) {
            JButton btn = (JButton)source;
            // Go ahead and do what you like
        }
    }
});

虽然我知道您目前的ActionListener可以保证Object的源类型将是JButton,但我从不喜欢盲目地投射对象,但这就是我

While I know your ActionListener as it stands can pretty much guarantee that the source type of the Object will be a JButton, I never like blindly casting objects, but that's me

这篇关于在ActionListener中引用单击JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆