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(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter