Java Swing RadioButton with variable, clickable icons

Design a question. The range of answers can be selected through RadioButtons

Each mouse click can change the displayed icon to icon_ 2. Vice versa I'm sorry to use

jRadioButtonActionPerformed 
ImageIcon o_ButtonIcon = new ImageIcon ("....") 
jRadioButton.setIcon(Icon m_ButtonIcon).

I didn't change the clickable image Can you help me?

Solution

It seems to be working properly

Issue sscce to show specific issues

This is an example (I don't recommend using getscaleinstance (..) Use it as a quick example)

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class Test {

    private ImageIcon ii1;
    private ImageIcon ii2;
    private JRadioButton jrb = new JRadioButton("Click me :)");
    private JFrame frame = new JFrame();

    public test() {
        try {
            ii1 = new ImageIcon(ImageIO.read(new URL("http://cdn.macrumors.com/article/2010/09/03/145454-itunes_10_icon.jpg")).getScaledInstance(48,48,Image.SCALE_SMOOTH));
            ii2 = new ImageIcon(ImageIO.read(new URL("http://www.quarktet.com/Icon-small.jpg")).getScaledInstance(48,Image.SCALE_SMOOTH));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        initComponents();
    }

    public static void main(String args[]) {
        SwingUtilities.invokelater(new Runnable() {
            @Override
            public void run() {
                new test();
            }
        });
    }

    private void initComponents() {
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

        jrb.setIcon(ii1);
        jrb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (jrb.getIcon() == ii1) {
                    jrb.setIcon(ii2);
                } else {
                    jrb.setIcon(ii1);
                }
            }
        });

        frame.add(jrb);
        frame.pack();
        frame.setVisible(true);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>