在jbutton上获取imageIcon的描述

时间:2012-11-04 16:09:42

标签: java swing jbutton imageicon

我有一个JButton可以根据具体情况选择2个不同的imageIcons。

image1Url = getClass().getResource(filename1);
image2Url = getClass().getResource(filename2);
ImageIcon image1 = new ImageIcon( image1Url , "image1")
ImageIcon image2 = new ImageIcon( image2Url , "image2")

JButton button = new JButton;

//Either image1 or image2 is set to this button depending 
//on whether the user clicks this button or whether the 
//computer sets an image there. The point is that the programme 
//then needs to respond depending upon what the image on the button is.
//I tried using...

if(button.getIcon() ==image1)
{
    //respond appropriately.
}

然而这不起作用。有人可以向我解释为什么这不起作用,如果有另一种方法可以解决这个问题?我以为我可以使用图像描述,但不知道如何实现它。 谢谢

2 个答案:

答案 0 :(得分:2)

我不建议您使用==,因为您正在比较参考文献。当然,除非你正在努力做到这一点。

我想说最简单的方法是设置一个值(例如boolean)来确定当前正在使用的ImageIcon,然后与之进行比较。

修改 对不起,我其实误解了这个问题。如果您想使用描述进行比较,请执行以下操作:

String desc = ((ImageIcon)button.getIcon()).getDescription();
if(desc.equals(image1.getDescription())
{
    //descriptions are the same
}

答案 1 :(得分:2)

  • 对我有用,必须有另一个问题,

  • 确保该图标必须是ImageIcon的实例,必须在代码中进行测试

示例

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.swing.*;

public class JButtonAndIcon {

    private static JButton label = new JButton();
    private static Random random = new Random();
    private static ImageIcon image1; // returns null don't worry about
    private static ImageIcon image2; // returns null don't worry about
    private static Timer backTtimer;
    private static int HEIGHT = 300, WEIGHT = 200;

    public static void main(String[] args) throws IOException {
        label.setPreferredSize(new Dimension(HEIGHT, WEIGHT));
        final JButton button = new JButton("Push");
        label.setBorderPainted(false);
        label.setBorder(null);
        label.setFocusable(false);
        label.setMargin(new Insets(0, 0, 0, 0));
        label.setContentAreaFilled(false);
        //button.setLayout(new BorderLayout());
        //button.add(label);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getIcon() == image1) {
                    label.setIcon(image2);
                } else {
                    label.setIcon(image1);
                }
            }
        });
        JFrame frame = new JFrame("Test");
        frame.add(label);
        frame.add(button, BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        startBackground();
        frame.setVisible(true);
    }

    private static void startBackground() {
        backTtimer = new javax.swing.Timer(2000, updateBackground());
        backTtimer.start();
        backTtimer.setRepeats(true);
    }

    private static Action updateBackground() {
        return new AbstractAction("Background action") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                image1 = new ImageIcon(getImage());
                label.setIcon(image1);
            }
        };
    }

    public static BufferedImage getImage() {
        int w = label.getWidth();
        int h = label.getHeight();
        GradientPaint gp = new GradientPaint(0f, 0f, new Color(
                127 + random.nextInt(128),
                127 + random.nextInt(128),
                127 + random.nextInt(128)),
                w, w,
                new Color(random.nextInt(128), 
                random.nextInt(128), random.nextInt(128)));
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        g2d.setColor(Color.BLACK);
        g2d.dispose(); // thanks notified by @trashgod
        return bi;
    }
}