Contents
- ./ColoredBackground2.java
- ./ColoredBackground.java
- ./ListFonts.java
- ./PanelWithButtons.java
- ./FlexibleLayout.java
./ColoredBackground2.java 1/5
[top][prev][next]
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Demonstrates using an anonymous inner class
* @author Sara Sprenkle
*
*/
public class ColoredBackground2 extends JFrame {
public ColoredBackground2() {
setTitle("Colored Background");
setBackground(Color.white);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
// create buttons and add to content pane
makeButton("Yellow", Color.yellow);
makeButton("Blue", Color.blue);
makeButton("Red", Color.red);
pack();
setVisible(true);
}
/**
*
* @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();
}
}
./ColoredBackground.java 2/5
[top][prev][next]
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author sarasprenkle
*
*/
public class ColoredBackground extends JFrame {
public ColoredBackground() {
setTitle("Colored Background");
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 yellow = new JButton("Yellow");
yellow.setBackground(Color.yellow);
ColorAction yellowAction = new ColorAction(Color.yellow);
cp.add(yellow);
JButton blue = new JButton("Blue");
blue.setForeground(Color.blue);
ColorAction blueAction = new ColorAction(Color.blue);
ColorAction redAction = new ColorAction(Color.red);
yellow.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
*
*/
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();
}
}
./ListFonts.java 3/5
[top][prev][next]
import java.awt.*;
/**
* List all the available fonts on the system
* @author Sara Sprenkle
*
*/
public class ListFonts {
public static void main(String[] args) {
String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
for (int i = 0; i < fontNames.length; i++)
System.out.println(fontNames[i]);
}
}
./PanelWithButtons.java 4/5
[top][prev][next]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class PanelWithButtons extends JFrame {
public PanelWithButtons() {
init();
setVisible(true);
}
private void init() {
this.setSize(200, 200);
// otherwise, just closes the window, doesn't quit
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
//pane.setLayout(new FlowLayout());
JButton red = new JButton("Red");
red.setForeground(Color.red);
JButton yellow = new JButton("Yellow");
yellow.setBackground(Color.yellow);
JButton blue = new JButton("Blue");
blue.setForeground(Color.blue);
pane.add(red, BorderLayout.NORTH);
pane.add(yellow, BorderLayout.SOUTH);
pane.add(blue, BorderLayout.CENTER);
// pack();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PanelWithButtons ex = new PanelWithButtons();
}
}
./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
Label north = new Label("North");
Label west = new Label("West");
Label east = new Label("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.