Java #12: How to make a Radio Button and ButtonGroup

//JRadio Button Creation Visible Name
JRadioButton Dogs = new JRadioButton(“Dogs”);
JRadioButton Cats = new JRadioButton(“Cats”);

//Create ButtonGroup so only one button can get selected at a time
ButtonGroup pets = new ButtonGroup();

//Adding the Cats and dogs to the ButtonGroup
pets.add(Dogs);
pets.add(Cats);

//Adding the ButtonGroup to the Frame so you can use it
frame.getContentPane().add(Dogs, BorderLayout.LINE_START);
frame.getContentPane().add(Cats, BorderLayout.CENTER);

Newest Radio Buttons

//Programmer:Akinwale Owi
//Program Purpose: How to make a Radio Button/ButtonGroup
//Company:Dagba Computers
//——————————————————

//package name
package dagba.computers;

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

//class name
public class DagbaComputers
{

//private class where I build the GUI
private static void buildingJFrame ()
{

JFrame frame = new JFrame(“Dagba Sample JFrame”); //Frame Heading
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //What happens when the frame closes

//JRadio Button Creation Visible Name
JRadioButton Dogs = new JRadioButton(“Dogs”);
JRadioButton Cats = new JRadioButton(“Cats”);

//Create ButtonGroup so only one button can get selected at a time
ButtonGroup pets = new ButtonGroup();

//Adding the Cats and dogs to the ButtonGroup
pets.add(Dogs);
pets.add(Cats);

//Adding the ButtonGroup to the Frame so you can use it
frame.getContentPane().add(Dogs, BorderLayout.LINE_START);
frame.getContentPane().add(Cats, BorderLayout.CENTER);

//Setting frame dimensions
frame.setPreferredSize(new Dimension (400, 400));

//Changing the frame color
frame.getContentPane().setBackground(Color.WHITE);

//Frame
frame.pack();
frame.setVisible(true); //So we can see the frame

}

//main function
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
//The Function
buildingJFrame();//Function where we built the Frame in
}
});

}//End of main function

}//end of DagbaComputers Class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: