0

The code works very well. But I cannot set the Icon for the JRadioButton. My guess is that it is a png file size issue. But I already choose a png file that is 250 pixel * 250 pixel.

If that is not small enough, then please tell me which website can I get free tiny PNG icon?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener {
    JRadioButton pizzaButton;
    JRadioButton hamburgerButton;
    JRadioButton hotdogButton;
    ImageIcon pizzaIcon;
    ImageIcon hamburgerIcon;
    ImageIcon hotdogIcon;

    public MyFrame() {
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pizzaButton = new JRadioButton("Pizza");
        hamburgerButton = new JRadioButton("Hamburger");
        hotdogButton = new JRadioButton("Hotdog");
        pizzaIcon = new ImageIcon("pizza.png");
        hotdogIcon = new ImageIcon("hotdog.png");
        hamburgerIcon = new ImageIcon("hamburger.png");

        //pizzaButton.setIcon(pizzaIcon);
        hamburgerButton.setIcon(hamburgerIcon);
        //hotdogButton.setIcon(hotdogIcon);

///https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html
        //The ButtonGroup component manages the selected/unselected state for a set of buttons.
        // For the group,
        // the ButtonGroup instance guarantees that only one button can be selected at a time.
        //Initially, all buttons managed by a ButtonGroup instance are unselected.
        ButtonGroup group = new ButtonGroup();
        group.add(pizzaButton);
        group.add(hamburgerButton);
        group.add(hotdogButton);

        pizzaButton.addActionListener(this);
        hamburgerButton.addActionListener(this);
        hotdogButton.addActionListener(this);

        this.add(pizzaButton);
        this.add(hamburgerButton);
        this.add(hotdogButton);

        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == pizzaButton) {System.out.println("You have choose pizza.");}
        else if (e.getSource() == hamburgerButton) {System.out.println("You have choose Hamburger.");}
        else {System.out.println("You have choose hotdog. Good for you.");}


    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jian
  • 4,119
  • 1
  • 17
  • 32
  • what's the error? 2nd question: google 'free icon library' – Reto Höhener Jun 15 '21 at 10:57
  • 1
    1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). That code needs at least a `main` method to make it self contained - lose the action listeners, they are irrelevant. .. – Andrew Thompson Jun 15 '21 at 11:07
  • .. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jun 15 '21 at 11:07
  • 1
    *"I already choose an png file that is 250 pixel * 250 pixel. If that is not small enough.."* A Swing `ImageIcon` should be able to support an image (at least) as big as the screen size. BTW - if [these images](https://i.stack.imgur.com/pcpF9.png) are good enough, note this can be done using Unicode characters (and a suitable font - this example uses Segoe UI Emoji). The font size is 100 px, but I adjusted it down from 250 to make a smaller screenshot. – Andrew Thompson Jun 15 '21 at 12:57
  • @AndrewThompson Show I create an Resource folder in project? – jian Jun 16 '21 at 10:39
  • A ButtonGroup? So... I can't order a Pizza, Hotdog, and Hamburger under one order? – DevilsHnd - 退職した Jun 16 '21 at 10:44
  • @DevilsHnd I have problem add icon to the RadioButton. – jian Jun 16 '21 at 10:48
  • @AndrewThompson Thanks. Problems solved. I don't know why. But even I placed the PNG file in scr folder (intellj), I still need type the full absolute path. – jian Jun 16 '21 at 11:02

0 Answers0