Hi All,
The GUI works as intended to. But I was wondering if some of you java gurus could look over my code and maybe give me some tips on how I could have written this code better. Thanks!
GUI CLASS:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
/*
* ColorSectionGUI Class, creates an program that allows user to select a direction or multiple selections
* from 4 checkboxes and allows them to choose one of four colors to change the direction that they selected
* colors.
*/
public class ColorSelectionGUI extends JFrame
{
private static final long serialVersionUID = 1L;
//Declares the windows width and height and gives it a value.
final int WINDOW_WIDTH = 600;
final int WINDOW_HEIGHT = 300;
//Declares the labels used in the program.
private JLabel northLabel, southLabel, eastLabel, westLabel;
//Declares the checkboxes.
private JCheckBox northCB, southCB, eastCB, westCB;
//Declares the radiobuttons.
private JRadioButton redRB, greenRB, blueRB, cyanRB;
//Declares the panels.
private JPanel panel1, panel2, checkboxPanel, radioPanel;
//Declares the buttongroup in which the radiobuttons will be added to.
private ButtonGroup ColorSelectGroup;
//Declares the container.
private Container pane;
//Declares the borders.
private TitledBorder borderCB, borderRB;
//ColorSelectionGui Constructor.
public ColorSelectionGUI()
{
//Sets the title, size, and default close option for the window.
setTitle("Color Selection GUI");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Calls the getContentPane method.
pane= getContentPane();
//Calls the buildGUI method.
buildGUI();
//Adds panel1 to the pane.
pane.add(panel1);
setVisible(true);
}
//buildGUI method, it puts the majority of the GUI together.
public void buildGUI()
{
//Creates Label objects, and names all of the labels and positions them.
northLabel = new JLabel("North", JLabel.CENTER);
southLabel = new JLabel("South", JLabel.CENTER);
eastLabel = new JLabel("East");
westLabel = new JLabel("West");
//Calls setOpaque method to true so that the labels are not transparent as default and
//sets the background of the labels to cyan.
northLabel.setOpaque(true);
northLabel.setBackground(Color.CYAN);
southLabel.setOpaque(true);
southLabel.setBackground(Color.CYAN);
eastLabel.setOpaque(true);
eastLabel.setBackground(Color.CYAN);
westLabel.setOpaque(true);
westLabel.setBackground(Color.CYAN);
//Creates checkbox objects, and names them. Sets the checkboxes to all be checked.
northCB = new JCheckBox("North");
southCB = new JCheckBox("South");
eastCB = new JCheckBox("East");
westCB = new JCheckBox("West");
northCB.setSelected(true);
southCB.setSelected(true);
eastCB.setSelected(true);
westCB.setSelected(true);
//Creates Panel object and sets a GridLayout so the checkboxes can be added to panel.
checkboxPanel = new JPanel();
checkboxPanel.setLayout(new GridLayout(4,1));
checkboxPanel.add(northCB);
checkboxPanel.add(southCB);
checkboxPanel.add(eastCB);
checkboxPanel.add(westCB);
//Creates a border for the checkboxes and names it. Sets the border on the checkbox panel.
borderCB = BorderFactory.createTitledBorder("Locations");
checkboxPanel.setBorder(borderCB);
//Creates radiobutton objects and names them.
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");
cyanRB = new JRadioButton("Cyan");
//Adds ActionListener to the radiobuttons and calls on their method.
redRB.addActionListener(new RadioListener());
greenRB.addActionListener(new RadioListener());
blueRB.addActionListener(new RadioListener());
cyanRB.addActionListener(new RadioListener());
//Sets the foreground color to the radiobuttons.
redRB.setForeground(Color.RED);
greenRB.setForeground(Color.GREEN);
blueRB.setForeground(Color.BLUE);
cyanRB.setForeground(Color.CYAN);
//Creates new Panel object and sets GridLayout for the radiobuttons to be added to.
//Sets the cyan radiobutton to be selected.
radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(4,1));
radioPanel.add(redRB);
radioPanel.add(greenRB);
radioPanel.add(blueRB);
radioPanel.add(cyanRB);
cyanRB.setSelected(true);
//Creates a border for the radiobutton panel and names it.
borderRB = BorderFactory.createTitledBorder("Bg Color");
radioPanel.setBorder(borderRB);
//Creates a ButtonGroup object and adds the radiobuttons to it.
ColorSelectGroup = new ButtonGroup();
ColorSelectGroup.add(redRB);
ColorSelectGroup.add(greenRB);
ColorSelectGroup.add(blueRB);
ColorSelectGroup.add(cyanRB);
//Creates a Panel object and sets a GridLayout. Adds the checkbox and radiobutton
//panels to this panel.
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,2));
panel2.add(checkboxPanel);
panel2.add(radioPanel);
//Creates a Panel object and BorderLayout for labels and panel to be added to.
//Adds the labels and panel.
panel1 = new JPanel(new BorderLayout());
panel1.add(northLabel, BorderLayout.NORTH);
panel1.add(southLabel, BorderLayout.SOUTH);
panel1.add(panel2, BorderLayout.CENTER);
panel1.add(eastLabel, BorderLayout.EAST);
panel1.add(westLabel, BorderLayout.WEST);
}
//RadioListener Class that implements ActionListener.
private class RadioListener implements ActionListener
{
//actionPerformed method that decides what happens based off of the users decisions
//with the checkboxes and radiobuttons.
public void actionPerformed(ActionEvent rb)
{
//Checks to see if the red radiobutton is selected.
if(redRB.isSelected()){
//Checks to see if the north checkbox is selected.
if(northCB.isSelected())
{
//Sets north background color to red.
northLabel.setBackground(Color.RED);
}
//Checks to see if the south checkbox is selected.
if(southCB.isSelected())
{
//Sets south background color to red.
southLabel.setBackground(Color.RED);
}
//Checks to see if the east checkbox is selected.
if(eastCB.isSelected())
{
//Sets east background color to red.
eastLabel.setBackground(Color.RED);
}
//Checks to see if the west checkbox is selected.
if(westCB.isSelected())
{
//Sets west background color to red.
westLabel.setBackground(Color.RED);
}
}
//Checks to see if the green radiobutton is selected.
if(greenRB.isSelected()){
//Checks to see if the north checkbox is selected.
if(northCB.isSelected())
{
//Sets north background color to green.
northLabel.setBackground(Color.GREEN);
}
//Checks to see if the south checkbox is selected.
if(southCB.isSelected())
{
//Sets south background color to green.
southLabel.setBackground(Color.GREEN);
}
//Checks to see if the east checkbox is selected.
if(eastCB.isSelected())
{
//Sets east background color to green.
eastLabel.setBackground(Color.GREEN);
}
//Checks to see if the west checkbox is selected.
if(westCB.isSelected())
{
//Sets west background color to green.
westLabel.setBackground(Color.GREEN);
}
}
//Checks to see if the blue radiobutton is selected.
if(blueRB.isSelected()){
//Checks to see if the north checkbox is selected.
if(northCB.isSelected())
{
//Sets north background color to blue.
northLabel.setBackground(Color.BLUE);
}
//Checks to see if the south checkbox is selected.
if(southCB.isSelected())
{
//Sets south background color to blue.
southLabel.setBackground(Color.BLUE);
}
//Checks to see if the east checkbox is selected.
if(eastCB.isSelected())
{
//Sets east background color to blue.
eastLabel.setBackground(Color.BLUE);
}
//Checks to see if the west checkbox is selected.
if(westCB.isSelected())
{
//Sets west background color to blue.
westLabel.setBackground(Color.BLUE);
}
}
//Checks to see if the cyan radiobutton is selected.
if(cyanRB.isSelected()){
//Checks to see if the north checkbox is selected.
if(northCB.isSelected())
{
//Sets north background color to cyan.
northLabel.setBackground(Color.CYAN);
}
//Checks to see if the south checkbox is selected.
if(southCB.isSelected())
{
//Sets south background color to cyan.
southLabel.setBackground(Color.CYAN);
}
//Checks to see if the east checkbox is selected.
if(eastCB.isSelected())
{
//Sets east background color to cyan.
eastLabel.setBackground(Color.CYAN);
}
//Checks to see if the west checkbox is selected.
if(westCB.isSelected())
{
//Sets west background color to cyan.
westLabel.setBackground(Color.CYAN);
}
}
}
}
}
DRIVER CLASS :
//ColorSelectionDUIDriver Class that contains main Method.
public class ColorSelectionDUIDriver {
//main Methods that creates a ColorSelectionGUI object.
public static void main(String[] args) {
//Creates object
ColorSelectionGUI cs = new ColorSelectionGUI();
}
}
Thanks Again!