##################################
GridBagTest.java Layout of a complicated GUI interface with GridLayout. Uses WindowUtilities.java and ExitListener.java.
##################################
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
/** An example demonstrating a GridBagLayout GUI with
* input text area and multiple buttons.
*
*********
public class GridBagTest extends JPanel {
private JTextArea textArea;
private JButton bSaveAs, bOk, bExit;
private JTextField fileField;
private GridBagConstraints c;
public GridBagTest() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createEtchedBorder());
textArea = new JTextArea(12,40); // 12 rows, 40 cols
bSaveAs = new JButton("Save As");
fileField = new JTextField("C:\\Document.txt");
bOk = new JButton("OK");
bExit = new JButton("Exit");
c = new GridBagConstraints();
// Text Area.
c.gridx = 0;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(2,2,2,2); //t,l,b,r
add(textArea,c);
// Save As Button.
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.VERTICAL;
add(bSaveAs,c);
// Filename Input (Textfield).
c.gridx = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.weightx = 1.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.BOTH;
add(fileField,c);
// OK Button.
c.gridx = 2;
c.gridy++;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.NONE;
add(bOk,c);
// Exit Button.
c.gridx = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.NONE;
add(bExit,c);
// Filler so Column 1 has nonzero width.
Component filler = Box.createRigidArea(new Dimension(1,1));
c.gridx = 1;
c.weightx = 1.0;
add(filler,c);
}
public static void main(String[] args) {
WindowUtilities.setNativeLookAndFeel();
JFrame frame = new JFrame("GrigBagLayout Test");
frame.setContentPane(new GridBagTest());
frame.addWindowListener(new ExitListener());
frame.pack();
frame.setVisible(true);
}
}
Aug 27
Layout of a complicated GUI interface with GridLayout
Aug 27
TextAreas
TextAreas.java
**************
import java.applet.Applet;
import java.awt.*;
/././././././,/././././
public class TextAreas extends Applet {
public void init() {
setBackground(Color.lightGray);
add(new TextArea(3, 10));
add(new TextArea("Some\nInitial\nText", 3, 10));
}
}
Aug 27
Lists.java
Lists.java Inherits from CloseableFrame.java.
/./././././././././
import java.awt.*;
/*****************/
public class Lists extends CloseableFrame {
public static void main(String[] args) {
new Lists();
}
public Lists() {
super("Lists");
setLayout(new FlowLayout());
setBackground(Color.lightGray);
setFont(new Font("SansSerif", Font.BOLD, 18));
List list1 = new List(3, false);
list1.add("Vanilla");
list1.add("Chocolate");
list1.add("Strawberry");
add(list1);
List list2 = new List(3, true);
list2.add("Colored Sprinkles");
list2.add("Cashews");
list2.add("Kiwi");
add(list2);
pack();
setVisible(true);
}
}
Aug 27
ChoiceTest
import java.applet.Applet;
import java.awt.*;
/*******/
public class ChoiceTest extends Applet {
private Choice choice;
public void init() {
setFont(new Font("SansSerif", Font.BOLD, 36));
choice = new Choice();
choice.addItem("Choice 1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
add(choice);
}
}
Aug 27
ButtonExample.java Uses the following
/./././././././././
# ButtonExample.java Uses the following classes:
* CloseableFrame.java
* FgReporter.java
* BgReporter.java
* SizeReporter.java
******************
ButtonExample.java
******************
import java.awt.*;
import java.awt.event.*;
/././././././././././
public class ButtonExample extends CloseableFrame {
public static void main(String[] args) {
new ButtonExample();
}
public ButtonExample() {
super("Using ActionListeners");
setLayout(new FlowLayout());
Button b1 = new Button("Button 1");
Button b2 = new Button("Button 2");
Button b3 = new Button("Button 3");
b1.setBackground(Color.lightGray);
b2.setBackground(Color.gray);
b3.setBackground(Color.darkGray);
FgReporter fgReporter = new FgReporter();
BgReporter bgReporter = new BgReporter();
SizeReporter sizeReporter = new SizeReporter();
b1.addActionListener(fgReporter);
b2.addActionListener(fgReporter);
b2.addActionListener(bgReporter);
b3.addActionListener(fgReporter);
b3.addActionListener(bgReporter);
b3.addActionListener(sizeReporter);
add(b1);
add(b2);
add(b3);
setSize(350, 100);
setVisible(true);
}
}
/./././././././././
FgReporter.java
***************
import java.awt.event.*;
import java.awt.*;
/././././././././././
public class FgReporter implements ActionListener {
public void actionPerformed(ActionEvent event) {
Component c = (Component)event.getSource();
System.out.println("Foreground: " + c.getForeground());
}
}
****************
BgReporter.java
****************
/././././././././
import java.awt.event.*;
import java.awt.*;
/././././././././././././
public class BgReporter implements ActionListener {
public void actionPerformed(ActionEvent event) {
Component c = (Component)event.getSource();
System.out.println("Background: " + c.getBackground());
}
}
/././././././././././
SizeReporter.java
***********************
import java.awt.event.*;
import java.awt.*;
/././././././././
public class SizeReporter implements ActionListener {
public void actionPerformed(ActionEvent event) {
Component c = (Component)event.getSource();
Dimension d = c.getSize();
System.out.println("Size: " + d.width + "x" + d.height);
}
}
********************
Aug 27
Uses a FileDialog to choose the file to display
DisplayFile.java
****************
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/** Uses a FileDialog to choose the file to display.
***************
public class DisplayFile extends CloseableFrame
implements ActionListener {
public static void main(String[] args) {
new DisplayFile();
}
private Button loadButton;
private TextArea fileArea;
private FileDialog loader;
public DisplayFile() {
super("Using FileDialog");
loadButton = new Button("Display File");
loadButton.addActionListener(this);
Panel buttonPanel = new Panel();
buttonPanel.add(loadButton);
add(buttonPanel, BorderLayout.SOUTH);
fileArea = new TextArea();
add("Center", fileArea);
loader = new FileDialog(this, "Browse", FileDialog.LOAD);
// Default file extension: .java.
loader.setFile("*.java");
setSize(350, 450);
setVisible(true);
}
/** When the button is clicked, a file dialog is opened. When
* the file dialog is closed, load the file it referenced.
*/
public void actionPerformed(ActionEvent event) {
loader.show();
displayFile(loader.getFile());
}
public void displayFile(String filename) {
try {
File file = new File(filename);
FileInputStream in = new FileInputStream(file);
int fileLength = (int)file.length();
byte[] fileContents = new byte[fileLength];
in.read(fileContents);
String fileContentsString = new String(fileContents);
fileArea.setText(fileContentsString);
} catch(IOException ioe) {
fileArea.setText("IOError: " + ioe);
}
}
}
*************
CloseableFrame.java.
*************
import java.awt.*;
import java.awt.event.*;
/** A Frame that you can actually quit. Used as the starting
* point for most Java 1.1 graphical applications.
**********************
public class CloseableFrame extends Frame {
public CloseableFrame(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
/** Since we are doing something permanent, we need
* to call super.processWindowEvent first.
*/
public void processWindowEvent(WindowEvent event) {
super.processWindowEvent(event); // Handle listeners.
if (event.getID() == WindowEvent.WINDOW_CLOSING) {
// If the frame is used in an applet, use dispose().
System.exit(0);
}
}
}
Aug 27
Applet that uses a anonymous nested class to handle mouse events and draw circles. Variation on third approach to event-handling: using inner classes.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/** Draw circles centered where the user clicks.
* Uses anonymous inner classes.
*
**********************
public class CircleDrawer4 extends Applet {
public void init() {
setForeground(Color.blue);
addMouseListener
(new MouseAdapter() {
private int radius = 25;
public void mousePressed(MouseEvent event) {
Graphics g = getGraphics();
g.fillOval(event.getX()-radius,
event.getY()-radius,
2*radius,
2*radius);
}
});
}
}
*******************
Aug 27
Tiny applet that uses CircleListener to handle mouse events.
import java.applet.Applet;
import java.awt.*;
/** Draw circles centered where the user clicks.
* Uses an external listener.
*
***********
public class CircleDrawer1 extends Applet {
public void init() {
setForeground(Color.blue);
addMouseListener(new CircleListener());
}
}
Aug 27
Subclass of MouseAdapter
!!!!!!!!!!!!
ClickListener.java A simple subclass of MouseAdapter that reports where the mouse was pressed. When attached to an applet, look for the report in the Java Console.
!!!!!!!!!!!!
import java.awt.event.*;
/** The listener used by ClickReporter.
*
**************
public class ClickListener extends MouseAdapter {
public void mousePressed(MouseEvent event) {
System.out.println("Mouse pressed at (" +
event.getX() + "," +
event.getY() + ").");
}
}
<<<<<<<<<<<<<<
Aug 27
Create PopupMenu and add MenuItems
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
************************
/** Simple demo of pop-up menus.
*
********************
public class ColorPopupMenu extends Applet
implements ActionListener {
private String[] colorNames =
{ "White", "Light Gray", "Gray", "Dark Gray", "Black" };
private Color[] colors =
{ Color.white, Color.lightGray, Color.gray,
Color.darkGray, Color.black };
private PopupMenu menu;
/** Create PopupMenu and add MenuItems. */
public void init() {
setBackground(Color.gray);
menu = new PopupMenu("Background Color");
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
MenuItem colorName;
for(int i=0; i
