Contents
- ./examples/ColoredBackground2.java
- ./examples/ColoredBackground.java
- ./examples/ColoredBackgroundRefactored.java
- ./examples/ColoredBackgroundSelfListener.java
- ./examples/FlexibleLayout.java
- ./examples/ThreeButtonsFrame.java
./examples/ColoredBackground2.java 1/6
[top][prev][next]
package examples;
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);
JButton blue = new JButton("Blue");
blue.setForeground(Color.blue);
ColorAction greenAction = new ColorAction(Color.green, this);
ColorAction blueAction = new ColorAction(Color.blue, this);
ColorAction redAction = new ColorAction(Color.red, this);
green.addActionListener(greenAction);
blue.addActionListener(blueAction);
red.addActionListener(redAction);
cp.add(green);
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();
}
}
./examples/ColoredBackground.java 2/6
[top][prev][next]
package examples;
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);
JButton blue = new JButton("Blue");
blue.setForeground(Color.blue);
ColorAction greenAction = new ColorAction(Color.green);
ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);
green.addActionListener(greenAction);
blue.addActionListener(blueAction);
red.addActionListener(redAction);
cp.add(green);
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();
}
}
./examples/ColoredBackgroundRefactored.java 3/6
[top][prev][next]
package examples;
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();
}
}
./examples/ColoredBackgroundSelfListener.java 4/6
[top][prev][next]
package examples;
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.
*
* The JFrame listens to its button's actions
*
* @author sarasprenkle
*
*/
public class ColoredBackgroundSelfListener extends JFrame implements ActionListener{
public ColoredBackgroundSelfListener() {
setTitle("Colored Background - Self-Listener");
setBackground(Color.white);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
// create buttons and add to content pane
red = new JButton("Red");
red.setForeground(Color.red);
red.addActionListener(this);
green = new JButton("Green");
green.setForeground(Color.GREEN);
green.addActionListener(this);
blue = new JButton("Blue");
blue.setForeground(Color.blue);
blue.addActionListener(this);
cp.add(red);
cp.add(green);
cp.add(blue);
pack();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
// needs to determine source
Object source = event.getSource();
if( source == blue) {
setBackground(Color.blue);
} else if( source == red ) {
setBackground(Color.red);
} else if( source == green ) {
setBackground(Color.green);
}
}
/**
* @param args
*/
public static void main(String[] args) {
ColoredBackground cb = new ColoredBackground();
}
private JButton blue;
private JButton green;
private JButton red;
}
./examples/FlexibleLayout.java 5/6
[top][prev][next]
package examples;
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);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
./examples/ThreeButtonsFrame.java 6/6
[top][prev][next]
/**
*
*/
package examples;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* Example of creating three buttons within a JFrame, which uses a BorderLayout
* by default.
*
* @author sprenkle
*/
public class ThreeButtonsFrame extends JFrame {
/**
* @throws HeadlessException
*/
public ThreeButtonsFrame() 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);
contentPane.add(button2);
contentPane.add(button3);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
ThreeButtonsFrame myframe = new ThreeButtonsFrame();
}
}
Generated by GNU enscript 1.6.4.