Java Can';t将按钮图标与其他图标进行比较

Java Can';t将按钮图标与其他图标进行比较,java,swing,compare,icons,jbutton,Java,Swing,Compare,Icons,Jbutton,我正在覆盖actionListener。除“playbutton”之外的所有按钮都有一个图标图像(button1、button2、button3)。每次按下按钮时,它都应将其图标与准备好的ImageIcon“livePicture”进行比较,如果它们相同,则应使用“escape()”方法。否则,程序将运行“Died()”方法。 但是,至少在当前代码中,它只使用“Died()”。我想,这意味着ifs在比较图像时有问题,但这是我在互联网上找到的唯一比较方法。 另外,请记住,这是我的第一个项目,所以它

我正在覆盖actionListener。除“playbutton”之外的所有按钮都有一个图标图像(button1、button2、button3)。每次按下按钮时,它都应将其图标与准备好的ImageIcon“livePicture”进行比较,如果它们相同,则应使用“escape()”方法。否则,程序将运行“Died()”方法。 但是,至少在当前代码中,它只使用“Died()”。我想,这意味着ifs在比较图像时有问题,但这是我在互联网上找到的唯一比较方法。 另外,请记住,这是我的第一个项目,所以它可能看起来有点杂乱

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Vector;

public class Frame
{
private final int WIDTH = 1024;
private final int HEIGHT = 768;

private JFrame frame;
private JPanel panel;
private JLabel human;
private JTextArea text;
private JTextArea deathMessage;
private ImageIcon livePicture;
private JButton button1;
private JButton button2;
private JButton button3;
private GridBagConstraints gbc;
private ActionListener actionListener;
private JButton playButton;
private Border border = BorderFactory.createEmptyBorder();
private Font font = new Font(Font.MONOSPACED, Font.PLAIN, 20);

public Frame()
{
    Quest survival = new Quest();

    actionListener = e -> {
        if (e.getSource() == playButton) //playbutton works fine
        {
            if (!survival.IsEmpty())
            {
                AppendQuest(survival.GetQuest());
                survival.RemoveQuest();
            }
            else
            {
                Escaped();
            }
        }
        else if (e.getSource() == button1) //button1 action
        {
            if (button1.getIcon().toString() != livePicture.toString())
            {
                Died();
            }
            else
            {
                if (!survival.IsEmpty())
                {
                    AppendQuest(survival.GetQuest());
                    survival.RemoveQuest();
                }
                else
                {
                    Escaped();
                }
            }
        }
        else if (e.getSource() == button2) //button2 action
        {
            if (button2.getIcon().toString() != livePicture.toString())
            {
                Died();
            }
            else
            {
                if (!survival.IsEmpty())
                {
                    AppendQuest(survival.GetQuest());
                    survival.RemoveQuest();
                }
                else
                {
                    Escaped();
                }
            }
        }
        else if (e.getSource() == button3) //button3 action
        {
            if (button3.getIcon().toString() != livePicture.toString())
            {
                Died();
            }
            else
            {
                if (!survival.IsEmpty())
                {
                    AppendQuest(survival.GetQuest());
                    survival.RemoveQuest();
                }
                else
                {
                    Escaped();
                }
            }
        }
    };
    //I left the rest of the constructor for bonus info
    frame = new JFrame();
    panel = new JPanel();
    gbc = new GridBagConstraints();
    human = new JLabel(ImageSize(200, 200, "res/human.png"));
    text = new JTextArea("You have lost in the forest. Now you have to find " +
            "your way back.");
    FormatText(text);
    deathMessage = new JTextArea();
    frame.setTitle("Shady Path");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setResizable(false);

    playButton = new JButton();
    playButton.addActionListener(actionListener);
    playButton.setFont(font);
    playButton.setText("Play");
    playButton.setForeground(Color.WHITE);
    playButton.setBackground(Color.BLACK);
    playButton.setBorder(border);

    panel.setLayout(new GridBagLayout());
    panel.setOpaque(false);
    gbc.anchor = GridBagConstraints.PAGE_START;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(human, gbc);

    gbc.insets = new Insets(30, 0, 0, 0);
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(text, gbc);

    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.insets = new Insets(50, 0, 68, 0);
    panel.add(playButton, gbc);
    frame.add(panel);
    frame.setVisible(true);
}

public void AppendQuest(Vector<String> event)
{
    panel.removeAll();

    panel.add(human, gbc);
    gbc.insets = new Insets(0, 0, 30, 0);
    text.setText(event.remove(0));
    FormatText(text);
    panel.add(text, gbc);
    deathMessage.setText(event.remove(0));
    FormatText(deathMessage);
    livePicture = ImageSize(50, 50, event.remove(0));

    Collections.shuffle(event);
    ImageIcon picture1 = ImageSize(50, 50, event.get(0)); //setting button1
    button1 = new JButton();
    button1.addActionListener(actionListener);
    button1.setIcon(picture1);
    button1.setBorder(border);
    ImageIcon picture2 = ImageSize(50, 50, event.get(1)); //setting button2
    button2 = new JButton();
    button2.addActionListener(actionListener);
    button2.setIcon(picture2);
    button2.setBorder(border);
    ImageIcon picture3 = ImageSize(50, 50, event.get(2)); //setting button3
    button3 = new JButton();
    button3.addActionListener(actionListener);
    button3.setIcon(picture3);
    button3.setBorder(border);
    gbc.gridwidth = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(50, 360, 100, 0);
    panel.add(button1, gbc);
    gbc.insets = new Insets(50, 77, 100, 77);
    panel.add(button2, gbc);
    gbc.insets = new Insets(50, 0, 100, 360);
    panel.add(button3, gbc);

    panel.revalidate();
    panel.repaint();
}

private void Escaped()
{
    //Unnecessary info
}

private void Died()
{
    //Unnecessary info
}
//This just resizes the images
private ImageIcon ImageSize(int x, int y, String fileName)
{
    BufferedImage baseImg = null;
    try {
        baseImg = ImageIO.read(new File(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Image resizedImg = baseImg.getScaledInstance(x, y, Image.SCALE_SMOOTH);
    ImageIcon IconImg = new ImageIcon(resizedImg);
    return IconImg;
}

private void FormatText(JTextArea baseText)
{
    //Unnecessary info
}
}
导入javax.imageio.imageio;
导入javax.swing.*;
导入javax.swing.border.border;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.Collections;
导入java.util.Vector;
公共类框架
{
私有最终整数宽度=1024;
私人最终内部高度=768;
私有JFrame;
私人JPanel小组;
私家侦探;
私有区域文本;
私人区域死亡信息;
私人图片图标;
私人按钮1;
私人按钮2;
私人按钮3;
gbc的私有网格;
私有ActionListener ActionListener;
私人JButton播放按钮;
private Border Border=BorderFactory.createEmptyByOrder();
私有字体字体=新字体(Font.MONOSPACED,Font.PLAIN,20);
公共框架()
{
任务生存=新任务();
actionListener=e->{
如果(e.getSource()==playButton)//playButton工作正常
{
如果(!survival.IsEmpty())
{
AppendQuest(survival.GetQuest());
生存。RemoveQuest();
}
其他的
{
逃逸();
}
}
如果(e.getSource()==button1)//button1操作
{
如果(按钮1.getIcon().toString()!=livePicture.toString())
{
死亡();
}
其他的
{
如果(!survival.IsEmpty())
{
AppendQuest(survival.GetQuest());
生存。RemoveQuest();
}
其他的
{
逃逸();
}
}
}
如果(e.getSource()==button2)//button2操作
{
如果(按钮2.getIcon().toString()!=livePicture.toString())
{
死亡();
}
其他的
{
如果(!survival.IsEmpty())
{
AppendQuest(survival.GetQuest());
生存。RemoveQuest();
}
其他的
{
逃逸();
}
}
}
如果(e.getSource()==button3)//button3操作
{
如果(按钮3.getIcon().toString()!=livePicture.toString())
{
死亡();
}
其他的
{
如果(!survival.IsEmpty())
{
AppendQuest(survival.GetQuest());
生存。RemoveQuest();
}
其他的
{
逃逸();
}
}
}
};
//我离开了构造器的其余部分以获取奖金信息
frame=新的JFrame();
panel=新的JPanel();
gbc=新的GridBagConstraints();
human=新的JLabel(ImageSize(200200,“res/human.png”);
text=new JTextArea(“你在森林中迷路了,现在你必须找到”+
“你回来的路。”);
格式文本(文本);
deathMessage=newjtextarea();
frame.setTitle(“阴影路径”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架尺寸(宽度、高度);
frame.setLocationRelativeTo(空);
frame.getContentPane().setBackground(颜色:黑色);
frame.setresizeable(false);
playButton=新JButton();
playButton.addActionListener(actionListener);
playButton.setFont(字体);
playButton.setText(“播放”);
播放按钮。设置前景(颜色。白色);
playButton.setBackground(颜色:黑色);
playButton.setboorder(边框);
panel.setLayout(新的GridBagLayout());
面板。设置不透明(假);
gbc.anchor=gridbagsconstraints.PAGE_START;
gbc.gridwidth=GridBagConstraints.rements;
新增专家组(人类,gbc);
gbc.插图=新插图(30,0,0,0);
gbc.weightx=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
面板。添加(文本,gbc);
gbc.fill=gridbagsconstraints.VERTICAL;
gbc.插图=新插图(50,0,68,0);
面板。添加(播放按钮,gbc);
框架。添加(面板);
frame.setVisible(true);
}
公共任务(向量事件)
{
panel.removeAll();
新增专家组(人类,gbc);
gbc.插图=新插图(0,0,30,0);
text.setText(event.remove(0));
格式文本(文本);
面板。添加(文本,gbc);
deathMessage.setText(event.remove(0));
格式化文本(死亡信息);
livePicture=ImageSize(50,50,event.remove(0));
收藏。洗牌(事件);
ImageIcon picture1=ImageSize(50,50,event.get(0));//设置按钮1
button1=新的JButton();
按钮1.addActionListener(actionListener);
按钮1.设置图标(图1);
按钮1.订单(边框);
ImageIcon picture2=ImageSize(50,50,event.get(1));//设置按钮2
button2=新的JButton();
按钮2.addActionListener(actionListener);
按钮2.设置图标(图2);
按钮2.订单(边框);
ImageIcon picture3=ImageSize(50,50,event.get(2));//设置按钮3
button3=新的JButton();
按钮3.addActionListener(actionListener);
按钮3.设置图标(图3);
按钮3.订单(边框);
gbc.gridwidth=GridBagConstraints.HORIZONTAL;
gbc.插图=新插图(50,360,100,0);
面板。添加(按钮1,
Vector<String> items2 = new Vector<>();
items2.add("You are kind of disoriented. What will you use to find the right way?" +
            " moss, sun or tree barks");
items2.add("Unfortunately you didn't orient yourself well enough. Now, you " +
            "will roam through the forest forever.");
items2.add("res/orientation_sun.png");
items2.add("res/orientation_moss.png");
items2.add("res/orientation_sun.png");
items2.add("res/orientation_tree_bark.png");
if(!button.getIcon().equals(livePicture))
{
   Died();
}
else
{...}
int nr1 = 1;
int nr2  = 1;
if(nr1 == nr2) {...} //true -> int is a native type

String str1 = "test";
String str2 = "test";
if(str1 == str2) {...} //false -> Same content but not same objects
if(str1.equals(str2)) {...} //true -> Same content, different objects
livePicture = ImageSize(50, 50, event.remove(0));