Contents

  1. ./ButtonPanel.java
  2. ./ColoredBackground2.java
  3. ./ColoredBackground.java
  4. ./ColoredBackgroundRefactored.java
  5. ./FlexibleLayout.java

./ButtonPanel.java 1/5

[
top][prev][next]
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 
 */

/**
 * @author sprenkle
 *
 */
public class ButtonPanel extends JFrame {

	
	/**
	 * @throws HeadlessException
	 */
	public ButtonPanel() throws HeadlessException {
		super();
		setTitle("My Buttons");
		
		Container contentPane = this.getContentPane();
		
		JButton button1 = new JButton("one");
		JButton button2 = new JButton("two");
		JButton button3 = new JButton("three");

		contentPane.add(button1, BorderLayout.NORTH);
		contentPane.add(button2, BorderLayout.SOUTH);
		contentPane.add(button3);

		pack();
		setVisible(true);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ButtonPanel bp = new ButtonPanel();
	}

}

./ColoredBackground2.java 2/5

[
top][prev][next]
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * When a button is pressed, it colors the background of the panel the
 * appropriate color.
 * 
 * Uses a separate class to update the background.
 * 
 * @author sarasprenkle
 * 
 */
public class ColoredBackground2 extends JFrame {

	public ColoredBackground2() {
		setTitle("Colored Background - Separate class");
		setBackground(Color.white);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());

		// create buttons and add to content pane
		JButton red = new JButton("Red");
		red.setForeground(Color.red);

		JButton green = new JButton("Green");
		green.setForeground(Color.GREEN);
		ColorAction yellowAction = new ColorAction(Color.green, this);
		cp.add(green);

		JButton blue = new JButton("Blue");
		blue.setForeground(Color.blue);

		ColorAction blueAction = new ColorAction(Color.blue, this);
		ColorAction redAction = new ColorAction(Color.red, this);

		green.addActionListener(yellowAction);
		blue.addActionListener(blueAction);
		red.addActionListener(redAction);

		cp.add(red);
		cp.add(blue);

		pack();
		setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ColoredBackground cb = new ColoredBackground();
	}

}

/**
 * This class could (maybe *should*) go in its own file.
 * 
 */
class ColorAction implements ActionListener {
	private Color backgroundColor;
	private JFrame frame;

	public ColorAction(Color c, JFrame frame) {
		backgroundColor = c;
		this.frame = frame;
	}

	public void actionPerformed(ActionEvent evt1) {
		frame.setBackground(backgroundColor);
		frame.repaint();
	}

}

./ColoredBackground.java 3/5

[
top][prev][next]
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * When a button is pressed, it colors the background of the panel the
 * appropriate color.
 * 
 * Uses an inner class.
 * 
 * @author sarasprenkle
 * 
 */
public class ColoredBackground extends JFrame {

	public ColoredBackground() {
		setTitle("Colored Background - Inner Class");
		setBackground(Color.white);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());

		// create buttons and add to content pane
		JButton red = new JButton("Red");
		red.setForeground(Color.red);

		JButton green = new JButton("Green");
		green.setForeground(Color.GREEN);
		ColorAction yellowAction = new ColorAction(Color.green);
		cp.add(green);

		JButton blue = new JButton("Blue");
		blue.setForeground(Color.blue);

		ColorAction blueAction = new ColorAction(Color.blue);
		ColorAction redAction = new ColorAction(Color.red);

		green.addActionListener(yellowAction);
		blue.addActionListener(blueAction);
		red.addActionListener(redAction);

		cp.add(red);
		cp.add(blue);

		pack();
		setVisible(true);
	}

	/**
	 * Example of inner class that does event handling
	 * 
	 */
	private class ColorAction implements ActionListener {
		private Color backgroundColor;

		public ColorAction(Color c) {
			backgroundColor = c;
		}

		public void actionPerformed(ActionEvent evt1) {
			// ColorAction does not have a setBackground method
			// but ColorBackground/JFrame does
			setBackground(backgroundColor);
			repaint();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ColoredBackground cb = new ColoredBackground();
	}

}

./ColoredBackgroundRefactored.java 4/5

[
top][prev][next]
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * When a button is pressed, it colors the background of the panel the
 * appropriate color.
 * <p>
 * Demonstrates using an anonymous inner class
 * 
 * @author Sara Sprenkle
 * 
 */
public class ColoredBackgroundRefactored extends JFrame {

	public ColoredBackgroundRefactored() {
		setTitle("Colored Background - Anonymous Inner Class");
		setBackground(Color.white);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());

		// create buttons and add to content pane
		makeButton("Green", Color.green);
		makeButton("Blue", Color.blue);
		makeButton("Red", Color.red);

		pack();
		setVisible(true);
	}

	/**
	 * Make a button with the given label. Pressing the button will make the
	 * panel background the given background color.
	 * 
	 * @param label
	 * @param backgroundColor
	 */
	private void makeButton(String label, final Color backgroundColor) {
		JButton button = new JButton(label);
		button.setBackground(backgroundColor);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				setBackground(backgroundColor);
				repaint();
			}
		});
		getContentPane().add(button);
	}

	/**
	 * Example of inner class that does event handling
	 * 
	 */
	class ColorAction implements ActionListener {
		private Color backgroundColor;

		public ColorAction(Color c) {
			backgroundColor = c;
		}

		public void actionPerformed(ActionEvent evt1) {
			// ColorAction does not have a setBackground method
			// but ColorBackground/JFrame does
			setBackground(backgroundColor);
			repaint();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ColoredBackground cb = new ColoredBackground();
	}

}

./FlexibleLayout.java 5/5

[
top][prev][next]
import javax.swing.*;
import java.awt.*;

/**
 * Demonstrates flexibility of layout
 */
public class FlexibleLayout {

	public static void main(String args[]) {
		JFrame frame = new JFrame();
		frame.setTitle("Demonstrate Flexibility");

		Container contentPane = frame.getContentPane();

		// create labels for each of the parts of the pane
		JLabel north = new JLabel("North");
		JLabel west = new JLabel("West");
		JLabel east = new JLabel("East");

		contentPane.add(north, BorderLayout.NORTH);
		contentPane.add(west, BorderLayout.WEST);
		contentPane.add(east, BorderLayout.EAST);

		// create buttons and add to button panel
		Button b1 = new Button("One!");
		Button b2 = new Button("a-Two!");
		Button b3 = new Button("a-Three!");

		JPanel buttonPanel = new JPanel();

		// add the buttons to the new JPanel
		// this will use the default flow layout manager
		buttonPanel.add(b1);
		buttonPanel.add(b2);
		buttonPanel.add(b3);

		// add the panel to the South part of the JFrame content pane
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		frame.pack();
		frame.setVisible(true);
	}
}

Generated by GNU enscript 1.6.4.