关于ImageIcons的方法不起作用 [英] The method about ImageIcons does not work

查看:81
本文介绍了关于ImageIcons的方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Cards extends JFrame {

private GridLayout grid1;

    JButton []bt=new JButton[52];

    ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));

    ImageIcon ori;

    public Cards(){
        grid1=new GridLayout(7,9,2,2);
        setLayout(grid1);
        for(int i=0;i<bt.length;i++){
            ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));
            bt[i]=new JButton(c);
            bt[i].addActionListener(new RatingMouseListener(i));
            add( bt[i]);
        }
    }

    public static void main(String[] args){
        Cards frame=new Cards();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1400,700);
        frame.setVisible(true);
    }

    private class RatingMouseListener implements ActionListener {
        private  int index=0;


        public RatingMouseListener(int index) {
            this.index = index;

        }




        public void actionPerformed(ActionEvent e) {
              System.out.println("Mouse entered for rating " + index);
            ori=new ImageIcon(getClass().getResource(index+1+".png"));

            if (bt[index].getIcon()==ori)
                bt[index].setIcon(tail);
             else
                bt[index].setIcon(ori);



        }

    }



}

运行此命令时,我希望ori和尾巴可以互换。但是他们没有。有人可以帮我吗?

When I run this, I expect that the ori and the tail should exchange. But they don't. Can someone help me?

推荐答案

最好通过说明标签。

基本上,将描述设置为如下所示的图像,如果描述相同,则将其交换。

Basically, set the description to the images like below, then swap them if they have the same description.

ori.setDescription("ori");
tail.setDescription("tail");

if ("ori".equals((ImageIcon)bt[index].getIcon()).getDescription())
// The rest is how you had it.

这篇关于关于ImageIcons的方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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