Pages

Wednesday, June 27, 2012

Java GUI Tutorial 5 - Buttons in Action!

If you need to revisit the past Java GUI tutorials click the corresponding tutorial number here: 1 2 3 4 5


Program Specifications

This program involves setting up 7 buttons, each with a different color. A click on any of the 7 buttons will change the background to their corresponding color. This program is more easily planned out, than it is understood.

The 3 Classes for this Tutorial

Runner
The Runner class is where the program will begin. It will create and prepare an instance of a JFrame and add an instance of ButtonPanel too it. It is in good practice to execute programs from outside their file like Runner.java does for this tutorial.

ButtonPanel extends JPanel implements ActionListener
The ButtonPanel is a JPanel (it inherits all of its characteristics) therefore can be placed and function with a JFrame. The ButtonPanel creates 7 instances of ColorButton and sets their action listener to itself. The ColorButtons need to communicate with the ButtonPanel so that the ButtonPanel knows when to change its background color.


ColorButton extends JButton
The ColorButton class extends JButton which allows you to create a customized constructor with the parameters String label and Color color. Color is a class created in Java so that you do not have to look up the hexidecimal codes for common colors such as red, blue, and gray. The ColorButton has all of the methods and variables that JButton has in addition to the methods I coded into it.

Runner.java

import java.awt.Dimension;

import javax.swing.JFrame;

/** Coded and designed by Brian R. H. 
 *    w7r.blogspot.com
 **/
public class Runner {
 public static void main(String[] args) {
  //where the program begins
  Dimension d = new Dimension(600,400);
  JFrame frame = new JFrame("W7R Java GUI Tutorial 5");
  ButtonPanel panel = new ButtonPanel(d);
  frame.add(panel);
  frame.setSize(d);
  frame.setVisible(true);
 }
}

ButtonPanel.java

package Buttons;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

public class ButtonPanel extends JPanel implements ActionListener{

 /*Fields are used to describe the labels because we want to ensure that the 
  string used to detect action events is the same as the action command 
  created in the ColorButton constructer*/
 public static String RED="Red", BLUE="Blue",
 ORANGE="Orange", YELLOW="Yellow",
 GREEN="Green", BLACK="BLACK",
 WHITE="White", PINK="Pink";

 ButtonPanel(Dimension d){
  setBackground(Color.white); //default background
  setSize(d);
  setUpButton(RED,Color.red);
  setUpButton(GREEN,Color.green);
  setUpButton(BLUE,Color.blue);
  setUpButton(BLACK,Color.black);
  setUpButton(ORANGE,Color.orange);
  setUpButton(YELLOW,Color.yellow);
  setUpButton(WHITE,Color.white);
  setUpButton(PINK,Color.pink);
  setFocusable(true);
  setVisible(true);
 }
 private void setUpButton(String label, Color color) {
  ColorButton button = new ColorButton(label,color);

  //allows ButtonPanel to handle events of that instance of ColorButton
  button.addActionListener(this);

  this.add(button);
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  String command = arg0.getActionCommand();
  if(command.equals(ButtonPanel.BLUE)){
   this.setBackground(Color.blue);
  } else if(command.equals(ButtonPanel.RED)) {
   this.setBackground(Color.red);
  } else if(command.equals(ButtonPanel.ORANGE)) {
   this.setBackground(Color.orange);
  } else if(command.equals(ButtonPanel.GREEN)) {
   this.setBackground(Color.green);
  } else if(command.equals(ButtonPanel.YELLOW)){
   this.setBackground(Color.yellow);
  } else if(command.equals(ButtonPanel.WHITE)){
   this.setBackground(Color.white);
  } else if(command.equals(ButtonPanel.BLACK)){
   this.setBackground(Color.black);
  } else if(command.equals(ButtonPanel.PINK)){
   this.setBackground(Color.pink);
  } else {
   //nothing
  }
 }
}

ColorButton.java

import java.awt.Color;

import javax.swing.JButton;

/** Coded and designed by Brian R. H. 
 *    w7r.blogspot.com
 **/

public class ColorButton extends JButton {
 ColorButton(String label, Color color){
  super(label);
  this.setBackground(color);
  this.setActionCommand(label); 
  this.setEnabled(true); 
 }
}

For tutorial #6 I plan on expanding on this program instead of starting anew. So please copy the three classes and save them in your src folder.

If you have any questions about my code, W7R, or Java, I am just one email away, (w7rdotblogspot@gmail.com)

No comments:

Post a Comment