/**
* A Simple stand-alone GUI program based on Swing.
* The program changes some colors, borders and fonts
* as an idea of how to customize some swing components
*
* This example uses a class that extends JFrame
* instead of creating a JFrame object!
* (inheritance vs. composition)
*
* @author Dave Hollinger
* @version 1.0
*/
import javax.swing.*;
import java.awt.*; // needed for Container
class Visuals extends JFrame {
public static void main(String [] args) {
Visuals v = new Visuals();
}
// constructor (called by main)
Visuals() {
Container c = getContentPane();
// add a bunch of buttons
c.add( makeButton( "First", "Arial",
Color.BLACK,
new Color(220,220,220)));
c.add( makeButton( "Second", "Courier",
Color.WHITE,
Color.BLUE));
c.add( makeButton( "Third", "TimesRoman",
Color.RED,
Color.BLACK));
// add some labels
c.add( makeLabel("Label 1","fixed",
Color.BLACK,Color.GRAY));
c.add( makeLabel("Label 2","Helvetica",
Color.GRAY,Color.WHITE));
// establish the layout manager
c.setLayout( new FlowLayout(FlowLayout.CENTER,5,5));
// set the size
setSize(200,400);
// set the title
setTitle("Demo Program");
// establish what happens when the window is closed
// (without this the program would keep running!)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the frame visible
setVisible(true);
}
JButton makeButton(String label, String fontname,
Color foreground, Color background) {
// create the button object
JButton j = new JButton(label);
// change the font
j.setFont(new Font(fontname,Font.BOLD,20));
// change the background
j.setBackground(background);
// change the foreground color
j.setForeground(foreground);
return(j);
}
JLabel makeLabel(String label, String fontname,
Color foreground, Color background) {
// create the label object
JLabel l = new JLabel(label);
// change the font
l.setFont(new Font(fontname,Font.BOLD,30));
// change the background
l.setBackground(background);
// change the foreground color
l.setForeground(foreground);
// change the border
l.setBorder(BorderFactory.createRaisedBevelBorder());
return(l);
}
}