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!

Well, probably you may get answers from people if you could have asked a specific question regarding your code instead of asking whether to optimize the code.
The answer is that you need to know whether your code is perfect or not according to your requirements.

Member Avatar for hfx642

Okay... I'll say something, then.
1. Use spaces instead of the TAB key.
(I find that 2 is too few, and 4 is too many. 3 is just right!)
2. Use arrays!

Sorry, new to java. How do you implement GUI components into arrays ?

Member Avatar for hfx642

Well, for example...

JLable My_Lable [] = new JLabel [10];
for (int I = 0; I < 10; I++)
{
   My_Label [I] = new JLabel (Integer.toString (I));
   My_Label [I].setFont (...);
// etc.
}

And the to add to a panel it would be something like:

panel.add(My_Label[I]);

?

Also, how would you set the positioning of the labels? For example on a border layout?

By the way, thanks for replying!

Member Avatar for hfx642

My_Panel.add (My_Label ); // is correct

The same as adding any other objects to a border layout.
However, if I have several objects that I want lined up
(like several labels or several text fields),
I like to use a panel with a grid layout.

No problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.