[英]How do I compare java swing button icons to check equality?
我正在用Java制作井字游戲,以自學Swing類。 不過,我必須解決問題。
首先,如何使用圖標比較按鈕?
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;
}
}
我知道這已經太遲了兩年,但是將來其他人可能會覺得這很有用...
無論如何,巧合的是,我正在編程3D Tic Tac Toe。 為了比較ImageIcons
,我使用String
描述初始化了圖標,然后使用:
((ImageIcon) JLabel().getIcon()).getDescription().compareTo("someOtherDesc")
希望將來對您或其他人有幫助...
我對如何做到這一點有一些想法,例如:
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...
}
如果將局部變量btn1...btn9
為實例變量,則可以在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約定(類名必須始終以大寫字母開頭)。
雖然為時已晚,但是任何人都可以在比較圖像圖標時獲得幫助。我認為這很容易
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.