JAVA: An Introduction to
Problem Solving & Programming, 8th Ed.
By Walter Savitch.
ISBN 01334462033 © 2018 Pearson Education, Inc., Hoboken, NJ.
All Rights Reserved
Listing 13.1 |
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
A simple demonstration of a window constructed using Swing.
*/
public class FirstSwingDemo
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public static void main (String [] args)
{
JFrame myWindow = new JFrame ();
myWindow.setSize (WIDTH, HEIGHT);
JLabel myLabel =
new JLabel ("Please dont click that button!");
myWindow.getContentPane ().add (myLabel);
WindowDestroyer myListener = new WindowDestroyer ();
myWindow.addWindowListener (myListener);
myWindow.setVisible (true);
}
}
|
Listing 13.2 |
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
If you register an object of this class as a listener to any
object of the class JFrame, the object will end the program
and close the JFrame if the user clicks the JFrame's
close-window button.
*/
public class WindowDestroyer extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
|
Listing 13.3 |
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
A simple window class.
*/
public class FirstWindow extends JFrame
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public FirstWindow ()
{
super ();
setSize (WIDTH, HEIGHT);
JLabel myLabel = new JLabel ("Please dont click that button!");
getContentPane ().add (myLabel);
WindowDestroyer listener = new WindowDestroyer ();
addWindowListener (listener);
}
}
|
Listing 13.4 |
/**
A simple demonstration of using a window class. To see
both windows, you will probably have to move the top window.
*/
public class FirstWindowDemo
public static void main (String [] args)
{
FirstWindow window1 = new FirstWindow ();
window1.setVisible (true);
FirstWindow window2 = new FirstWindow ();
window2.setVisible (true);
}
}
|
Listing 13.5 |
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Container;
public class SecondWindow extends JFrame
{
public static final int WIDTH = 200;
public static final int HEIGHT = 200;
public SecondWindow ()
{
super ();
setSize (WIDTH, HEIGHT);
Container contentPane = getContentPane ();
JLabel label = new JLabel ("Now available in color!");
contentPane.add (label);
setTitle ("Second Window");
contentPane.setBackground (Color.BLUE);
addWindowListener (new WindowDestroyer ());
}
public SecondWindow (Color customColor)
{
super ();
setSize (WIDTH, HEIGHT);
Container contentPane = getContentPane ();
JLabel label = new JLabel ("Now available in color!");
contentPane.add (label);
setTitle ("Second Window");
contentPane.setBackground (customColor);
addWindowListener (new WindowDestroyer ());
}
}
|
Listing 13.6 |
import java.awt.Color;
public class SecondWindowDemo
{
/**
Creates and displays two windows of the class SecondWindow.
*/
public static void main (String [] args)
{
SecondWindow window1 = new SecondWindow ();
window1.setVisible (true);
SecondWindow window2 = new SecondWindow (Color.PINK);
window2.setVisible (true);
}
}
|
Listing 13.7 |
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Container;
/**
Simple demonstration of the use of a layout manager
to arrange labels.
*/
public class BorderLayoutDemo extends JFrame
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public BorderLayoutDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Layout Demonstration");
Container content = getContentPane ();
content.setLayout (new BorderLayout ());
JLabel label1 = new JLabel ("First label here.");
content.add (label1, BorderLayout.NORTH);
JLabel label2 = new JLabel ("Second label there.");
content.add (label2, BorderLayout.SOUTH);
JLabel label3 = new JLabel ("Third label anywhere.");
content.add (label3, BorderLayout.CENTER);
}
/*
Creates and displays a window of the class BorderLayoutDemo.
*/
public static void main (String [] args)
{
BorderLayoutDemo gui = new BorderLayoutDemo ();
gui.setVisible (true);
}
}
|
Listing 13.8 |
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Simple demonstration of putting buttons in a JFrame.
*/
public class ButtonDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public ButtonDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Button Demo");
Container contentPane = getContentPane ();
contentPane.setBackground (Color.BLUE);
contentPane.setLayout (new FlowLayout ());
JButton stopButton = new JButton ("Red");
stopButton.addActionListener (this);
contentPane.add (stopButton);
JButton goButton = new JButton ("Green");
goButton.addActionListener (this);
contentPane.add (goButton);
}
public void actionPerformed (ActionEvent e)
{
Container contentPane = getContentPane ();
if (e.getActionCommand ().equals ("Red"))
contentPane.setBackground (Color.RED);
else if (e.getActionCommand ().equals ("Green"))
contentPane.setBackground (Color.GREEN);
else
System.out.println ("Error in button interface.");
}
/**
Creates and displays a window of the class ButtonDemo.
*/
public static void main (String [] args)
{
ButtonDemo buttonGui = new ButtonDemo ();
buttonGui.setVisible (true);
}
}
|
Listing 13.9 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Simple demonstration of putting buttons in a panel.
*/
public class PanelDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public static void main (String [] args)
{
PanelDemo guiWithPanel = new PanelDemo ();
guiWithPanel.setVisible (true);
}
public PanelDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Panel Demonstration");
Container contentPane = getContentPane ();
contentPane.setBackground (Color.BLUE);
contentPane.setLayout (new BorderLayout ());
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.WHITE);
buttonPanel.setLayout (new FlowLayout ());
JButton stopButton = new JButton ("Red");
stopButton.setBackground (Color.RED);
stopButton.addActionListener (this);
buttonPanel.add (stopButton);
JButton goButton = new JButton ("Green");
goButton.setBackground (Color.GREEN);
goButton.addActionListener (this);
buttonPanel.add (goButton);
contentPane.add (buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed (ActionEvent e)
{
Container contentPane = getContentPane ();
if (e.getActionCommand ().equals ("Red"))
contentPane.setBackground (Color.RED);
else if (e.getActionCommand ().equals ("Green"))
contentPane.setBackground (Color.GREEN);
else
System.out.println ("Error in button interface.");
}
}
|
Listing 13.10 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MemoSaver extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
private JTextArea theText;
private String memo1 = "No Memo 1.";
private String memo2 = "No Memo 2.";
public MemoSaver ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Memo Saver");
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.WHITE);
buttonPanel.setLayout (new FlowLayout ());
JButton memo1Button = new JButton ("Save Memo 1");
memo1Button.addActionListener (this);
buttonPanel.add (memo1Button);
JButton memo2Button = new JButton ("Save Memo 2");
memo2Button.addActionListener (this);
buttonPanel.add (memo2Button);
JButton clearButton = new JButton ("Clear");
clearButton.addActionListener (this);
buttonPanel.add (clearButton);
JButton get1Button = new JButton ("Get Memo 1");
get1Button.addActionListener (this);
buttonPanel.add (get1Button);
JButton get2Button = new JButton ("Get Memo 2");
get2Button.addActionListener (this);
buttonPanel.add (get2Button);
contentPane.add (buttonPanel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel ();
textPanel.setBackground (Color.BLUE);
theText = new JTextArea (LINES, CHAR_PER_LINE);
theText.setBackground (Color.WHITE);
textPanel.add (theText);
contentPane.add (textPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
String actionCommand = e.getActionCommand ();
if (actionCommand.equals ("Save Memo 1"))
memo1 = theText.getText ();
else if (actionCommand.equals ("Save Memo 2"))
memo2 = theText.getText ();
else if (actionCommand.equals ("Clear"))
theText.setText ("");
else if (actionCommand.equals ("Get Memo 1"))
theText.setText (memo1);
else if (actionCommand.equals ("Get Memo 2"))
theText.setText (memo2);
else
theText.setText ("Error in memo interface");
}
public static void main (String [] args)
{
MemoSaver guiMemo = new MemoSaver ();
guiMemo.setVisible (true);
}
}
|
Listing 13.11 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Class to demonstrate placing a label on a text field.
*/
public class LabelDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JTextField name;
public LabelDemo ()
{
setTitle ("Name Tester");
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
Container content = getContentPane ();
content.setLayout (new GridLayout (2, 1));
JPanel namePanel = new JPanel ();
namePanel.setLayout (new BorderLayout ());
namePanel.setBackground (Color.LIGHT_GRAY);
name = new JTextField (20);
namePanel.add (name, BorderLayout.SOUTH);
JLabel nameLabel = new JLabel ("Enter your name here:");
namePanel.add (nameLabel, BorderLayout.CENTER);
content.add (namePanel);
JPanel buttonPanel = new JPanel ();
buttonPanel.setLayout (new FlowLayout ());
JButton b = new JButton ("Test");
b.addActionListener (this);
buttonPanel.add (b);
b = new JButton ("Clear");
b.addActionListener (this);
buttonPanel.add (b);
content.add (buttonPanel);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Test"))
name.setText ("A very good name!");
else if (e.getActionCommand ().equals ("Clear"))
name.setText ("");
else
name.setText ("Error in window interface.");
}
public static void main (String [] args)
{
LabelDemo w = new LabelDemo ();
w.setVisible (true);
}
}
|
Listing 13.12 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
GUI for totaling a series of numbers.
*/
public class Adder extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 200;
private JTextField inOutField;
private double sum = 0;
public Adder ()
{
setTitle ("Adding Machine");
addWindowListener (new WindowDestroyer ());
setSize (WIDTH, HEIGHT);
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.GRAY);
buttonPanel.setLayout (new FlowLayout ());
JButton addButton = new JButton ("Add");
addButton.addActionListener (this);
buttonPanel.add (addButton);
JButton resetButton = new JButton ("Reset");
resetButton.addActionListener (this);
buttonPanel.add (resetButton);
contentPane.add (buttonPanel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel ();
textPanel.setBackground (Color.BLUE);
textPanel.setLayout (new FlowLayout ());
inOutField = new JTextField ("Numbers go here.", 30);
inOutField.setBackground (Color.WHITE);
textPanel.add (inOutField);
contentPane.add (textPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Add"))
{
sum = sum + stringToDouble (inOutField.getText ());
inOutField.setText (Double.toString (sum));
}
else if (e.getActionCommand ().equals ("Reset"))
{
sum = 0;
inOutField.setText ("0.0");
}
else
inOutField.setText ("Error in adder code.");
}
private static double stringToDouble (String stringObject)
{
return Double.parseDouble (stringObject.trim ());
}
public static void main (String [] args)
{
Adder guiAdder = new Adder ();
guiAdder.setVisible (true);
}
}
|
Listing 13.13 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
GUI for adding a series of numbers. If the user
enters a number in an incorrect format, such as
2,000 with a comma, an error message is generated
and the user can restart the computation.
*/
public class ImprovedAdder extends JFrame
implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 200;
private JTextField inOutField;
private double sum = 0;
public ImprovedAdder ()
{
setTitle ("Adding Machine");
addWindowListener (new WindowDestroyer ());
setSize (WIDTH, HEIGHT);
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.GRAY);
buttonPanel.setLayout (new FlowLayout ());
JButton addButton = new JButton ("Add");
addButton.addActionListener (this);
buttonPanel.add (addButton);
JButton resetButton = new JButton ("Reset");
resetButton.addActionListener (this);
buttonPanel.add (resetButton);
contentPane.add (buttonPanel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel ();
textPanel.setBackground (Color.BLUE);
textPanel.setLayout (new FlowLayout ());
inOutField = new JTextField ("Numbers go here.", 30);
inOutField.setBackground (Color.WHITE);
textPanel.add (inOutField);
contentPane.add (textPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
try
{
tryingCorrectNumberFormats (e);
}
catch (NumberFormatException e2)
{
inOutField.setText ("Error: Reenter Number.");
}
}
//This method can throw a NumberFormatException.
public void tryingCorrectNumberFormats (ActionEvent e)
{
if (e.getActionCommand ().equals ("Add"))
{
sum = sum + stringToDouble (inOutField.getText ());
inOutField.setText (Double.toString (sum));
}
else if (e.getActionCommand ().equals ("Reset"))
{
sum = 0;
inOutField.setText ("0.0");
}
else
inOutField.setText ("Error in adder code.");
}
//This method can throw a NumberFormatException.
private static double stringToDouble (String stringObject)
{
return Double.parseDouble (stringObject.trim ());
}
public static void main (String [] args)
{
ImprovedAdder guiAdder = new ImprovedAdder ();
guiAdder.setVisible (true);
}
}
|
Listing 14.1 |
import javax.swing.JApplet; import javax.swing.JLabel; import java.awt.Container; import java.awt.FlowLayout; public class HelloApplet extends JApplet { public void init () { Container contentPane = getContentPane (); contentPane.setLayout (new FlowLayout ()); JLabel friendlyLabel = new JLabel ("Hello out there!"); contentPane.add (friendlyLabel); } } |
Listing 14.2 |
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AdderApplet extends JApplet implements ActionListener { private JTextField inputOutputField; private double sum = 0; public void init () { Container contentPane = getContentPane (); contentPane.setLayout (new BorderLayout ()); JPanel buttonPanel = new JPanel (); buttonPanel.setBackground (Color.GRAY); buttonPanel.setLayout (new FlowLayout ()); JButton addButton = new JButton ("Add"); addButton.addActionListener (this); buttonPanel.add (addButton); JButton resetButton = new JButton ("Reset"); resetButton.addActionListener (this); buttonPanel.add (resetButton); contentPane.add (buttonPanel, BorderLayout.SOUTH); JPanel textPanel = new JPanel (); textPanel.setBackground (Color.BLUE); textPanel.setLayout (new FlowLayout ()); inputOutputField = new JTextField ("Numbers go here.", 30); inputOutputField.setBackground (Color.WHITE); textPanel.add (inputOutputField); contentPane.add (textPanel, BorderLayout.CENTER); } public void actionPerformed (ActionEvent e) { if (e.getActionCommand ().equals ("Add")) { sum = sum + stringToDouble (inputOutputField.getText ()); inputOutputField.setText (Double.toString (sum)); } else if (e.getActionCommand ().equals ("Reset")) { sum = 0; inputOutputField.setText ("0.0"); } else inputOutputField.setText ("Error in adder code."); } private static double stringToDouble (String stringObject) { return Double.parseDouble (stringObject.trim ()); } } |
Listing 14.3 |
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import java.awt.Container;
public class DukeApplet extends JApplet
{
public void init ()
{
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JLabel spacer = new JLabel (" ");
contentPane.add (spacer, "West");
JLabel friendlyLabel = new JLabel ("Hello out there!");
ImageIcon dukeIcon = new ImageIcon ("duke_waving.gif");
friendlyLabel.setIcon (dukeIcon);
contentPane.add (friendlyLabel, BorderLayout.CENTER);
}
}
|
Listing 15.1 |
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MemoGUI extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
private JTextArea theText;
private String memo1 = "No Memo 1.";
private String memo2 = "No Memo 2.";
public MemoGUI ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Memo Saver");
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JMenu memoMenu = new JMenu ("Memos");
JMenuItem m;
m = new JMenuItem ("Save Memo 1");
m.addActionListener (this);
memoMenu.add (m);
m = new JMenuItem ("Save Memo 2");
m.addActionListener (this);
memoMenu.add (m);
m = new JMenuItem ("Get Memo 1");
m.addActionListener (this);
memoMenu.add (m);
m = new JMenuItem ("Get Memo 2");
m.addActionListener (this);
memoMenu.add (m);
m = new JMenuItem ("Clear");
m.addActionListener (this);
memoMenu.add (m);
m = new JMenuItem ("Exit");
m.addActionListener (this);
memoMenu.add (m);
JMenuBar mBar = new JMenuBar ();
mBar.add (memoMenu);
setJMenuBar (mBar);
JPanel textPanel = new JPanel ();
textPanel.setBackground (Color.BLUE);
theText = new JTextArea (LINES, CHAR_PER_LINE);
theText.setBackground (Color.WHITE);
textPanel.add (theText);
contentPane.add (textPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
String actionCommand = e.getActionCommand ();
if (actionCommand.equals ("Save Memo 1"))
memo1 = theText.getText ();
else if (actionCommand.equals ("Save Memo 2"))
memo2 = theText.getText ();
else if (actionCommand.equals ("Clear"))
theText.setText ("");
else if (actionCommand.equals ("Get Memo 1"))
theText.setText (memo1);
else if (actionCommand.equals ("Get Memo 2"))
theText.setText (memo2);
else if (actionCommand.equals ("Exit"))
System.exit (0);
else
theText.setText ("Error in memo interface");
}
public static void main (String [] args)
{
MemoGUI gui = new MemoGUI ();
gui.setVisible (true);
}
}
|
Listing 15.2 |
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Simple demonstration of putting icons in buttons and labels.
*/
public class IconDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 200;
private JTextField message;
public IconDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Icon Demonstration");
Container content = getContentPane ();
content.setBackground (Color.WHITE);
content.setLayout (new BorderLayout ());
JLabel niceLabel = new JLabel ("Nice day!");
ImageIcon smileyIcon = new ImageIcon ("smiley.gif");
niceLabel.setIcon (smileyIcon);
content.add (niceLabel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel ();
buttonPanel.setLayout (new FlowLayout ());
JButton helloButton = new JButton ("Hello");
ImageIcon dukeWavingIcon = new ImageIcon ("duke_waving.gif");
helloButton.setIcon (dukeWavingIcon);
helloButton.addActionListener (this);
buttonPanel.add (helloButton);
JButton byeButton = new JButton ("Good bye");
ImageIcon dukeStandingIcon =
new ImageIcon ("duke_standing.gif");
byeButton.setIcon (dukeStandingIcon);
byeButton.addActionListener (this);
buttonPanel.add (byeButton);
content.add (buttonPanel, BorderLayout.SOUTH);
message = new JTextField (30);
content.add (message, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Hello"))
message.setText ("Glad to meet you!");
else if (e.getActionCommand ().equals ("Good bye"))
message.setText (
"OK, click the upper right button. Ill miss you.");
else
System.out.println ("Error in button interface.");
}
/**
Creates and displays a window of the class IconDemo.
*/
public static void main (String [] args)
{
IconDemo iconGui = new IconDemo ();
iconGui.setVisible (true);
}
}
|
Listing 15.3 |
import javax.swing.JScrollPane;
//Other import statements are identical to the ones in Listing 15.1
public class ScrollBarDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
//Definitions of the constants HEIGHT, LINES, and CHAR_PER_LINE are identical
// to those in Listing 15.1
private JTextArea theText;
< Definitions of instance variables memo1 and memo2 are identical to those in Listing 15.1 . >
public ScrollBarDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Scrolling Memo Saver");
Container contentPane = getContentPane ();
//. . .
JPanel textPanel = new JPanel ();
textPanel.setBackground (Color.BLUE);
theText = new JTextArea (LINES, CHAR_PER_LINE);
theText.setBackground (Color.WHITE);
JScrollPane scrolledText = new JScrollPane (theText);
scrolledText.setHorizontalScrollBarPolicy (
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolledText.setVerticalScrollBarPolicy (
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textPanel.add (scrolledText);
contentPane.add (textPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
// This method is identical to the one in Listing 15.1
}
public static void main (String [] args)
{
ScrollBarDemo guiMemo = new ScrollBarDemo ();
guiMemo.setVisible (true);
}
}
|
Listing 15.4 |
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Class to demonstrate adding borders to components.
*/
public class BorderDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
private JTextField name;
public BorderDemo ()
{
setTitle ("Name Tester with Borders");
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
Container content = getContentPane ();
content.setLayout (new GridLayout (2, 1));
JPanel namePanel = new JPanel ();
namePanel.setLayout (new BorderLayout ());
namePanel.setBackground (Color.WHITE);
name = new JTextField (20);
//The following border is not as dramatic as others,
//but look closely and you will see it.
name.setBorder (new EtchedBorder (Color.GREEN, Color.BLUE));
namePanel.add (name, BorderLayout.SOUTH);
JLabel nameLabel = new JLabel ("Enter your name here:");
//The following border inserts space around the label.
//To see the difference, comment out the following line:
nameLabel.setBorder (new EmptyBorder (20, 10, 0, 0));
namePanel.add (nameLabel, BorderLayout.CENTER);
namePanel.setBorder (new LineBorder (Color.BLACK, 10));
content.add (namePanel);
JPanel buttonPanel = new JPanel ();
buttonPanel.setLayout (new FlowLayout ());
JButton testButton = new JButton ("Test");
testButton.addActionListener (this);
testButton.setBorder (new BevelBorder (BevelBorder.LOWERED));
buttonPanel.add (testButton);
JButton clearButton = new JButton ("Clear");
clearButton.addActionListener (this);
clearButton.setBorder (new BevelBorder (BevelBorder.RAISED));
buttonPanel.add (clearButton);
buttonPanel.setBorder (
new MatteBorder (60, 40, 30, 20, Color.PINK));
content.add (buttonPanel);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Test"))
name.setText ("A very good name!");
else if (e.getActionCommand ().equals ("Clear"))
name.setText ("");
else
name.setText ("Error in window interface.");
}
public static void main (String [] args)
{
BorderDemo w = new BorderDemo ();
w.setVisible (true);
}
}
|
Listing 15.5 |
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Simple demonstration of BoxLayout manager class and the use of
struts to separate components (in this case, buttons). (For an
alternative implementation, see BoxClassDemo in Listing 15.6.)
*/
public class BoxLayoutDemo extends JFrame
implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public static final int HORIZONTAL_STRUT_SIZE = 15;
public static final int VERTICAL_STRUT_SIZE = 10;
private JPanel colorPanel;
public BoxLayoutDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Box Demonstration");
Container content = getContentPane ();
content.setLayout (new BorderLayout ());
colorPanel = new JPanel ();
colorPanel.setBackground (Color.BLUE);
content.add (colorPanel, BorderLayout.CENTER);
//Horizontal buttons at bottom of frame:
JPanel horizontalPanel = new JPanel ();
horizontalPanel.setLayout (
new BoxLayout (horizontalPanel, BoxLayout.X_AXIS));
Component horizontalStrut =
Box.createHorizontalStrut (HORIZONTAL_STRUT_SIZE);
horizontalPanel.add (horizontalStrut);
JButton hStopButton = new JButton ("Red");
hStopButton.addActionListener (this);
horizontalPanel.add (hStopButton);
Component horizontalStrut2 =
Box.createHorizontalStrut (HORIZONTAL_STRUT_SIZE);
horizontalPanel.add (horizontalStrut2);
JButton hGoButton = new JButton ("Green");
hGoButton.addActionListener (this);
horizontalPanel.add (hGoButton);
content.add (horizontalPanel, BorderLayout.SOUTH);
//Vertical buttons on right side of frame:
JPanel verticalPanel = new JPanel ();
verticalPanel.setLayout (
new BoxLayout (verticalPanel, BoxLayout.Y_AXIS));
Component verticalStrut =
Box.createVerticalStrut (VERTICAL_STRUT_SIZE);
verticalPanel.add (verticalStrut);
JButton vStopButton = new JButton ("Red");
vStopButton.addActionListener (this);
verticalPanel.add (vStopButton);
Component verticalStrut2 =
Box.createVerticalStrut (VERTICAL_STRUT_SIZE);
verticalPanel.add (verticalStrut2);
JButton vGoButton = new JButton ("Green");
vGoButton.addActionListener (this);
verticalPanel.add (vGoButton);
content.add (verticalPanel, BorderLayout.EAST);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Red"))
colorPanel.setBackground (Color.RED);
else if (e.getActionCommand ().equals ("Green"))
colorPanel.setBackground (Color.GREEN);
else
System.out.println ("Error in button interface.");
}
public static void main (String [] args)
{
BoxLayoutDemo gui = new BoxLayoutDemo ();
gui.setVisible (true);
}
}
|
Listing 15.6 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
Simple demonstration of the Box container class and the use of
struts to separate components (in this case, buttons). For an
alternative implementation, see BoxLayoutDemo in Listing 15.5.
*/
public class BoxClassDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public static final int HORIZONTAL_STRUT_SIZE = 15;
public static final int VERTICAL_STRUT_SIZE = 10;
private JPanel colorPanel;
public BoxClassDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("Box Demonstration");
Container content = getContentPane ();
content.setLayout (new BorderLayout ());
colorPanel = new JPanel ();
colorPanel.setBackground (Color.BLUE);
content.add (colorPanel, BorderLayout.CENTER);
//Horizontal buttons at bottom of frame:
Box horizontalBox = Box.createHorizontalBox ();
Component horizontalStrut =
Box.createHorizontalStrut (HORIZONTAL_STRUT_SIZE);
horizontalBox.add (horizontalStrut);
JButton hStopButton = new JButton ("Red");
hStopButton.addActionListener (this);
horizontalBox.add (hStopButton);
Component horizontalStrut2 =
Box.createHorizontalStrut (HORIZONTAL_STRUT_SIZE);
horizontalBox.add (horizontalStrut2);
JButton hGoButton = new JButton ("Green");
hGoButton.addActionListener (this);
horizontalBox.add (hGoButton);
content.add (horizontalBox, BorderLayout.SOUTH);
//Vertical buttons on right side of frame:
Box verticalBox = Box.createVerticalBox ();
Component verticalStrut =
Box.createVerticalStrut (VERTICAL_STRUT_SIZE);
verticalBox.add (verticalStrut);
JButton vStopButton = new JButton ("Red");
vStopButton.addActionListener (this);
verticalBox.add (vStopButton);
Component verticalStrut2 =
Box.createVerticalStrut (VERTICAL_STRUT_SIZE);
verticalBox.add (verticalStrut2);
JButton vGoButton = new JButton ("Green");
vGoButton.addActionListener (this);
verticalBox.add (vGoButton);
content.add (verticalBox, BorderLayout.EAST);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Red"))
colorPanel.setBackground (Color.RED);
else if (e.getActionCommand ().equals ("Green"))
colorPanel.setBackground (Color.GREEN);
else
System.out.println ("Error in button interface.");
}
public static void main (String [] args)
{
BoxClassDemo gui = new BoxClassDemo ();
gui.setVisible (true);
}
}
|
Listing 15.7 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CardLayoutDemo extends JFrame
implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private CardLayout dealer;
private JPanel deckPanel;
public CardLayoutDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (new WindowDestroyer ());
setTitle ("CardLayout Demonstration");
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
deckPanel = new JPanel ();
dealer = new CardLayout ();
deckPanel.setLayout (dealer);
JPanel startCardPanel = new JPanel ();
startCardPanel.setLayout (new FlowLayout ());
startCardPanel.setBackground (Color.LIGHT_GRAY);
JLabel startLabel = new JLabel ("Hello");
startCardPanel.add (startLabel);
deckPanel.add ("start", startCardPanel);
JPanel greenCardPanel = new JPanel ();
greenCardPanel.setLayout (new FlowLayout ());
greenCardPanel.setBackground (Color.GREEN);
JLabel goLabel = new JLabel ("Go");
greenCardPanel.add (goLabel);
deckPanel.add ("green", greenCardPanel);
JPanel redCardPanel = new JPanel ();
redCardPanel.setLayout (new FlowLayout ());
redCardPanel.setBackground (Color.RED);
JLabel stopLabel = new JLabel ("Stop");
redCardPanel.add (stopLabel);
deckPanel.add ("red", redCardPanel);
contentPane.add (deckPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.WHITE);
buttonPanel.setLayout (new FlowLayout ());
JButton stopButton = new JButton ("Red");
stopButton.addActionListener (this);
buttonPanel.add (stopButton);
JButton goButton = new JButton ("Green");
goButton.addActionListener (this);
buttonPanel.add (goButton);
JButton resetButton = new JButton ("Reset");
resetButton.addActionListener (this);
buttonPanel.add (resetButton);
contentPane.add (buttonPanel, BorderLayout.SOUTH);
dealer.first (deckPanel); //Optional
}
public void actionPerformed (ActionEvent e)
{
String actionCommand = e.getActionCommand ();
if (actionCommand.equals ("Red"))
dealer.show (deckPanel, "red");
else if (actionCommand.equals ("Green"))
dealer.show (deckPanel, "green");
else if (actionCommand.equals ("Reset"))
dealer.show (deckPanel, "start");
else
System.out.println ("Error in CardLayout Demo.");
}
public static void main (String [] args)
{
CardLayoutDemo demoGui = new CardLayoutDemo ();
demoGui.setVisible (true);
}
}
|
Listing 15.8 |
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class InnerClassDemo extends JFrame
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
/**
Creates and displays a window of the class InnerClassDemo.
*/
public static void main (String [] args)
{
InnerClassDemo sampleGUI = new InnerClassDemo ();
sampleGUI.setVisible (true);
}
public InnerClassDemo ()
{
setSize (WIDTH, HEIGHT);
setTitle ("Inner Class Demo");
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
JLabel label = new JLabel (
"Please dont click that button!");
contentPane.add (label, BorderLayout.CENTER);
addWindowListener (new InnerDestroyer ());
}
//An inner class with the same functionality
//as the class WindowDestroyer.
private class InnerDestroyer extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
}
|
Listing 15.9 |
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowListenerDemo extends JFrame
implements ActionListener, WindowListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public WindowListenerDemo ()
{
setSize (WIDTH, HEIGHT);
addWindowListener (this);
setTitle ("Window Listener Demonstration");
Container content = getContentPane ();
content.setBackground (Color.BLUE);
content.setLayout (new FlowLayout ());
JButton stopButton = new JButton ("Red");
stopButton.addActionListener (this);
content.add (stopButton);
JButton goButton = new JButton ("Green");
goButton.addActionListener (this);
content.add (goButton);
}
public void actionPerformed (ActionEvent e)
{
Container content = getContentPane ();
if (e.getActionCommand ().equals ("Red"))
content.setBackground (Color.RED);
else if (e.getActionCommand ().equals ("Green"))
content.setBackground (Color.GREEN);
else
System.out.println ("Error in WindowListenerDemo.");
}
public void windowClosing (WindowEvent e)
{
this.dispose ();
System.exit (0);
}
public void windowOpened (WindowEvent e)
{ }
public void windowClosed (WindowEvent e)
{ }
public void windowIconified (WindowEvent e)
{ }
public void windowDeiconified (WindowEvent e)
{ }
public void windowActivated (WindowEvent e)
{ }
public void windowDeactivated (WindowEvent e)
{ }
public static void main (String [] args)
{
WindowListenerDemo demoWindow = new WindowListenerDemo ();
demoWindow.setVisible (true);
}
}
|
Listing 15.10 |
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** Demonstration of programming the close-window button. */ public class CloseWindowDemo extends JFrame { public static final int WIDTH = 300; public static final int HEIGHT = 200; public CloseWindowDemo () { setSize (WIDTH, HEIGHT); setDefaultCloseOperation ( WindowConstants.DO_NOTHING_ON_CLOSE); addWindowListener (new InnerDestroyer ()); setTitle ("Close Window Demo"); Container contentPane = getContentPane (); contentPane.setLayout (new BorderLayout ()); JLabel message = new JLabel ( "Please don't click that button."); contentPane.add (message, BorderLayout.CENTER); } //An inner class that is the window listener. private class InnerDestroyer extends WindowAdapter { //Displays a window that checks if the user wants to exit. public void windowClosing (WindowEvent e) { ConfirmWindow askWindow = new ConfirmWindow (); askWindow.setVisible (true); } } //An inner class to be used with the inner class //InnerDestroyer. Checks if the user wants to exit. private class ConfirmWindow extends JFrame implements ActionListener { public static final int WIDTH = 200; public static final int HEIGHT = 100; public ConfirmWindow () { setSize (WIDTH, HEIGHT); Container confirmContent = getContentPane (); confirmContent.setBackground (Color.WHITE); confirmContent.setLayout (new BorderLayout ()); JLabel msgLabel = new JLabel ( "Are you sure you want to exit?"); confirmContent.add (msgLabel, BorderLayout.CENTER); JPanel buttonPanel = new JPanel (); buttonPanel.setLayout (new FlowLayout ()); JButton exitButton = new JButton ("Yes"); exitButton.addActionListener (this); buttonPanel.add (exitButton); JButton cancelButton = new JButton ("No"); cancelButton.addActionListener (this); buttonPanel.add (cancelButton); confirmContent.add (buttonPanel, BorderLayout.SOUTH); } public void actionPerformed (ActionEvent e) { if (e.getActionCommand ().equals ("Yes")) System.exit (0); else if (e.getActionCommand ().equals ("No")) dispose (); //Destroys only the ConfirmWindow. else System.out.println ("Error in Confirm Window."); } } public static void main (String [] args) { CloseWindowDemo gui = new CloseWindowDemo (); gui.setVisible (true); } } |
Listing 15.11 |
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class VisibilityDemo extends JFrame
implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JLabel upLabel;
private JLabel downLabel;
public VisibilityDemo ()
{
setSize (WIDTH, HEIGHT);
setDefaultCloseOperation (
WindowConstants.EXIT_ON_CLOSE);
setTitle ("Visibility Demonstration");
Container contentPane = getContentPane ();
contentPane.setLayout (new BorderLayout ());
contentPane.setBackground (Color.WHITE);
upLabel = new JLabel ("Here I am up here!");
contentPane.add (upLabel, BorderLayout.NORTH);
upLabel.setVisible (false);
downLabel = new JLabel ("Here I am down here!");
contentPane.add (downLabel, BorderLayout.SOUTH);
downLabel.setVisible (false);
JPanel buttonPanel = new JPanel ();
buttonPanel.setBackground (Color.WHITE);
buttonPanel.setLayout (new FlowLayout ());
JButton upButton = new JButton ("Up");
upButton.addActionListener (this);
buttonPanel.add (upButton);
JButton downButton = new JButton ("Down");
downButton.addActionListener (this);
buttonPanel.add (downButton);
contentPane.add (buttonPanel, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Up"))
{
upLabel.setVisible (true);
downLabel.setVisible (false);
validate (); //Update graphics
}
else if (e.getActionCommand ().equals ("Down"))
{
downLabel.setVisible (true);
upLabel.setVisible (false);
validate (); //Update graphics
}
else
System.out.println ("Error in VisibilityDemo interface.");
}
public static void main (String [] args)
{
VisibilityDemo demoGui = new VisibilityDemo ();
demoGui.setVisible (true);
}
}
|