如何比较java swing按钮图标以检查是否相等?

如何比较java swing按钮图标以检查是否相等?,java,swing,button,comparison,equals,Java,Swing,Button,Comparison,Equals,我正在用爪哇做一个井字游戏来教我自己荡秋千课。不过,我不得不面对一些问题 首先,如何使用图标比较按钮 symX = new ImageIcon(this.getClass().getResource("symbolX.png")); symO = new ImageIcon(this.getClass().getResource("symbolO.png")); 我使用这两个变量来设置按钮图像 if (e.getSource() instanceof JButton) { JButto

我正在用爪哇做一个井字游戏来教我自己荡秋千课。不过,我不得不面对一些问题

首先,如何使用图标比较按钮

symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));
我使用这两个变量来设置按钮图像

if (e.getSource() instanceof JButton) {
    JButton source = (JButton) e.getSource();

    if (isX) {
        source.setIcon(symX);
        source.setEnabled(false);
        source.setDisabledIcon(symX);       
    } else {    
        source.setIcon(symO);
        source.setEnabled(false);
        source.setDisabledIcon(symO);   
    }
}           
第二个问题是,在哪里比较对象是动作事件?我试图在上面的代码中比较if语句的内部,但是Eclipse总是给我编译时错误

如果我将代码放在带有按钮的方法中,那么java似乎永远无法访问它们

根据要求,这里是我的整个Java文件

package ticTacToeGUI;

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

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class tttGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

    JPanel panel = new JPanel();
    JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;

    private ImageIcon symX, symO;
    private ImageIcon symIco = new ImageIcon(this.getClass().getResource("symbolX.png"));
    private boolean isX = true;     

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

    public tttGUI() {
        //Setup the window.
        super("Tic-Tac-Toe GUI 1.0");
        setSize(425,425);
        setIconImage(symIco.getImage());
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Create the content.
        symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
        symO = new ImageIcon(this.getClass().getResource("symbolO.png"));       

        panel.setLayout(new GridLayout(3,3));
        setVisible(true);

        JButton btn1 = new JButton();
        JButton btn2 = new JButton();
        JButton btn3 = new JButton();
        JButton btn4 = new JButton();
        JButton btn5 = new JButton();
        JButton btn6 = new JButton();
        JButton btn7 = new JButton();
        JButton btn8 = new JButton();
        JButton btn9 = new JButton();

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        panel.add(btn5);
        panel.add(btn6);
        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);

        add(panel);
        revalidate();
    }

    public void actionPerformed(ActionEvent e) {    
        if (e.getSource() instanceof JButton) {
            JButton source = (JButton) e.getSource();

            if (isX) {
                source.setIcon(symX);
                source.setEnabled(false);
                source.setDisabledIcon(symX);       
            } else {    
                source.setIcon(symO);
                source.setEnabled(false);
                source.setDisabledIcon(symO);   
            }
        }   
        isX ^= true;    
    }
}

我对如何做到这一点有一些想法,例如:

symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));

if (btn1.getIcon().equals(symX) && btn2.getIcon().equals(symX) {
    // Your logic here...
}
如果将局部变量提升为实例变量,则可以在
actionPerformed()
方法内部执行此操作。您似乎混淆了概念,因为您已经将
btn1…btn9
声明为实例变量,但也创建了新的实例变量作为局部变量

public class TttGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

    private JPanel panel = new JPanel();
    private JButton btn1 = new JButton();
    private JButton btn2 = new JButton();
    private JButton btn3 = new JButton();
    private JButton btn4 = new JButton();
    private JButton btn5 = new JButton();
    private JButton btn6 = new JButton();
    private JButton btn7 = new JButton();
    private JButton btn8 = new JButton();
    private JButton btn9 = new JButton();

    private ImageIcon symX, symO;
    private ImageIcon symIco = new ImageIcon(this.getClass().getResource("symbolX.png"));
    private boolean isX = true;     

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

    public TttGUI() {
        //Setup the window.
        super("Tic-Tac-Toe GUI 1.0");
        setSize(425,425);
        setIconImage(symIco.getImage());
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Create the content.
        symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
        symO = new ImageIcon(this.getClass().getResource("symbolO.png"));       

        panel.setLayout(new GridLayout(3,3));
        setVisible(true);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        panel.add(btn5);
        panel.add(btn6);
        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);

        add(panel);
        revalidate();
    }
}

我已经更改了您的类名,使其符合Java约定(类名必须始终以大写字母开头)。

我对如何做到这一点有一些想法,例如:

symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
symO = new ImageIcon(this.getClass().getResource("symbolO.png"));

if (btn1.getIcon().equals(symX) && btn2.getIcon().equals(symX) {
    // Your logic here...
}
如果将局部变量提升为实例变量,则可以在
actionPerformed()
方法内部执行此操作。您似乎混淆了概念,因为您已经将
btn1…btn9
声明为实例变量,但也创建了新的实例变量作为局部变量

public class TttGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

    private JPanel panel = new JPanel();
    private JButton btn1 = new JButton();
    private JButton btn2 = new JButton();
    private JButton btn3 = new JButton();
    private JButton btn4 = new JButton();
    private JButton btn5 = new JButton();
    private JButton btn6 = new JButton();
    private JButton btn7 = new JButton();
    private JButton btn8 = new JButton();
    private JButton btn9 = new JButton();

    private ImageIcon symX, symO;
    private ImageIcon symIco = new ImageIcon(this.getClass().getResource("symbolX.png"));
    private boolean isX = true;     

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

    public TttGUI() {
        //Setup the window.
        super("Tic-Tac-Toe GUI 1.0");
        setSize(425,425);
        setIconImage(symIco.getImage());
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Create the content.
        symX = new ImageIcon(this.getClass().getResource("symbolX.png"));
        symO = new ImageIcon(this.getClass().getResource("symbolO.png"));       

        panel.setLayout(new GridLayout(3,3));
        setVisible(true);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btn4);
        panel.add(btn5);
        panel.add(btn6);
        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);

        add(panel);
        revalidate();
    }
}

我已经更改了您的类名,使其符合Java约定(类名必须始终以大写字母开头)。

我知道这已经晚了两年,但将来其他人可能会发现这很有用

不管怎样,巧合的是,我正在为3D Tic Tac Toe编程。 为了比较
图像图标
,我用
字符串
说明初始化了图标,然后使用:

((ImageIcon) JLabel().getIcon()).getDescription().compareTo("someOtherDesc")

希望这能在将来帮助你或其他人…

我知道这已经晚了两年,但将来其他人可能会觉得这很有用

不管怎样,巧合的是,我正在为3D Tic Tac Toe编程。 为了比较
图像图标
,我用
字符串
说明初始化了图标,然后使用:

((ImageIcon) JLabel().getIcon()).getDescription().compareTo("someOtherDesc")

希望这对你或其他人将来有所帮助…

虽然为时已晚,但任何搜索的人都可以在比较图像图标方面获得帮助。我认为这很容易做到

    ImageIcon icon = new ImageIcon("images/myimage.jpg");

    JLabel a = new JLabel(icon);

    JLabel b = new JLabel(icon);



    if(a.getIcon().toString().equals(b.getIcon().toString()))
    {
      //do whatever
    }

虽然为时已晚,但任何搜索的人都可以在比较图像图标方面得到帮助。我认为这很容易做到

    ImageIcon icon = new ImageIcon("images/myimage.jpg");

    JLabel a = new JLabel(icon);

    JLabel b = new JLabel(icon);



    if(a.getIcon().toString().equals(b.getIcon().toString()))
    {
      //do whatever
    }

你会得到什么错误?什么是isX?您的代码需要更多的上下文。可能会发布整个JAVA文件。我更新了整个JAVA文件,而且isX是我用来确定它是哪个符号的布尔值。我试着这样比较基站:if(btn1.getIcon().equals(symX)和&btn2.getIcon().equals(symX)等等,但是如果我把它放在tttGUI的末尾,它什么也没做,如果我把它放在Action Listener中,它什么也没做。你会得到什么错误?什么是isX?你需要更多的代码上下文。可能会发布整个JAVA文件。我更新了整个JAVA文件,而且isX是我用来确定它是哪个符号的布尔值。我试着比较btn例如:if(btn1.getIcon().equals(symX)和&btn2.getIcon().equals(symX)等等,但是如果我把它放在tttGUI的末尾,它什么也没做,如果我把它放在Action Listener中,它什么也没做。我仍然不知道该把这段代码放在哪里。如果我把更多的代码放在Listener方法中,它就会出错。如果我把它放在tttGUI的末尾,它就达不到了。我不知道如何将代码的检查部分添加到lis中tener。比较按钮后,您想做什么?我仍然不知道将此代码放在何处。如果我在侦听器方法中添加更多代码,它就会出错。如果我将其放在tttGUI的末尾,它将无法到达。我不确定如何将代码的检查部分添加到侦听器中。比较按钮后,您想做什么?