31) RADIO BUTTON DEMO

PHOTO EMBED

Mon Jul 08 2024 06:28:59 GMT+0000 (Coordinated Universal Time)

Saved by @varuntej #java

RadioButtonDemo.java

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

public class RadioButtonDemo implements ActionListener
{
    private JFrame frame;
	private JRadioButton c1, c2, c3, c4;
	private JLabel label;

	public RadioButtonDemo()
	{
		frame = new JFrame("A Simple Swing App");

		//frame.setSize(600, 400);
		Toolkit tk = frame.getToolkit();
		Dimension dim = tk.getScreenSize();
		int width = (int)dim.getWidth();
		int height = (int)dim.getHeight();
		frame.setSize(width, height);

		frame.setLayout(new FlowLayout());

		c1 = new JRadioButton("Pizza");
		c1.addActionListener(this);
		c1.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(c1);

		c2 = new JRadioButton("Burger");
		c2.addActionListener(this);
		c2.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(c2);

		c3 = new JRadioButton("Rolls");
		c3.addActionListener(this);
		c3.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(c3);

		c4 = new JRadioButton("Beverage");
		c4.addActionListener(this);
		c4.setFont(new Font("Verdana", Font.BOLD, 18));
		frame.add(c4);

		ButtonGroup bg = new ButtonGroup();
		bg.add(c1); bg.add(c2); bg.add(c3); bg.add(c4);

		label = new JLabel("I show the selected items");
		label.setFont(new Font("Verdana", Font.BOLD, 18));
		label.setForeground(Color.RED);
		frame.add(label);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae)
	{   label.setText(ae.getActionCommand());
	}

	public static void main(String[] args)
	{   new RadioButtonDemo();
	}
}
content_copyCOPY