如何比较Java摆动按钮图标以检查是否相等? [英] How do I compare java swing button icons to check equality?

查看:257
本文介绍了如何比较Java摆动按钮图标以检查是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java制作井字游戏,以自学Swing类。

I am making a Tic-Tac-Toe game in Java to teach myself the Swing class. I am having to problems, though.

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

First, how do you compare buttons using icons?

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

我使用这两个变量来设置按钮图像。

I use those 2 variables to set the button images.

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总是给我这样做的编译时错误。

The second question is where do you compare objects are an Action Event? I tried to compare inside of the if statements in the above code, but Eclipse always gives me compile time errors doing that.

如果我将代码与

根据请求,这是我的Java文件的全部。

As requested, here is the entirety of my Java file.

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;    
    }
}


推荐答案

I知道这已经太迟了两年,但是将来其他人可能会觉得这很有用...

I know this is two years too late, but other people in the future might find this useful...

无论如何,巧合的是,我正在编写3D Tic Tac Toe。
为了比较 ImageIcons ,我用 String 描述初始化了图标,然后使用:

Anyway, coincidentally I'm programming up 3D Tic Tac Toe. To compare ImageIcons, I initialized the Icons with a String description, and then used:

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

希望对您或其他人有帮助...

Hope that helps you or someone else in future...

这篇关于如何比较Java摆动按钮图标以检查是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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