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
Uses a FileDialog to choose the file to display
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
Aug 27
ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java.
ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java.
**********************
ReverseLabels.java
**********************
import java.awt.*;
******************
public class ReverseLabels extends CloseableFrame {
public static void main(String[] args) {
new ReverseLabels();
}
public ReverseLabels() {
super("Reversible Labels");
setLayout(new FlowLayout());
setBackground(Color.lightGray);
setFont(new Font("Serif", Font.BOLD, 18));
ReversibleLabel label1 =
new ReversibleLabel("Black on White",
Color.white, Color.black);
add(label1);
ReversibleLabel label2 =
new ReversibleLabel("White on Black",
Color.black, Color.white);
add(label2);
pack();
setVisible(true);
}
}
/./././././././././.
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);
}
}
}
&&&&&&&&&&&&&&&&&&&&
ReversibleLabel.java.
&&&&&&&&&&&&&&&&&&&&
import java.awt.*;
import java.awt.event.*;
/** A Label that reverses its background and
* foreground colors when the mouse is over it.
*
**********************
public class ReversibleLabel extends Label {
public ReversibleLabel(String text,
Color bgColor, Color fgColor) {
super(text);
MouseAdapter reverser = new MouseAdapter() {
public void mouseEntered(MouseEvent event) {
reverseColors();
}
public void mouseExited(MouseEvent event) {
reverseColors(); // or mouseEntered(event);
}
};
addMouseListener(reverser);
setText(text);
setBackground(bgColor);
setForeground(fgColor);
}
protected void reverseColors() {
Color fg = getForeground();
Color bg = getBackground();
setForeground(bg);
setBackground(fg);
}
}
#############################
Aug 27
TextFields
import java.applet.Applet;
import java.awt.*;
/** A TextField from each of the four constructors.
*
*********************
public class TextFields extends Applet {
public void init() {
add(new TextField());
add(new TextField(30));
add(new TextField("Initial String"));
add(new TextField("Initial", 30));
}
}
Aug 27
ChoiceTest2
ChoiceTest2.java
/././././././././
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/***********************/
public class ChoiceTest2 extends Applet
implements ItemListener {
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");
choice.addItemListener(this);
add(choice);
}
public void itemStateChanged(ItemEvent event) {
Choice choice = (Choice)event.getSource();
String selection = choice.getSelectedItem();
if (selection.equals("Choice 1")) {
doChoice1Action();
} else if (selection.equals("Choice 2")) {
doChoice2Action();
} else if (selection.equals("Choice 3")) {
doChoice3Action();
}
}
private void doChoice1Action() {
System.out.println("Choice 1 Action");
}
private void doChoice2Action() {
System.out.println("Choice 2 Action");
}
private void doChoice3Action() {
System.out.println("Choice 3 Action");
}
}
Aug 27
CheckboxGroups
CheckboxGroups.java
/./././././././././
/////////////////////
import java.applet.Applet;
import java.awt.*;
////////////////////
public class CheckboxGroups extends Applet {
public void init() {
setLayout(new GridLayout(4, 2));
setBackground(Color.lightGray);
setFont(new Font("Serif", Font.BOLD, 16));
add(new Label("Flavor", Label.CENTER));
add(new Label("Toppings", Label.CENTER));
CheckboxGroup flavorGroup = new CheckboxGroup();
add(new Checkbox("Vanilla", flavorGroup, true));
add(new Checkbox("Colored Sprinkles"));
add(new Checkbox("Chocolate", flavorGroup, false));
add(new Checkbox("Cashews"));
add(new Checkbox("Strawberry", flavorGroup, false));
add(new Checkbox("Kiwi"));
}
}
Aug 26
A textfield and three buttons arranged by a verticle BoxLayout
BoxLayoutTest.java A textfield and three buttons arranged by a verticle BoxLayout. Uses WindowUtilities.java and ExitListener.java.
##################
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** An example of BoxLayout.
*
***********
public class BoxLayoutTest extends JPanel
implements ActionListener{
BoxLayout layout;
JButton topButton, middleButton, bottomButton;
public BoxLayoutTest() {
layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
JLabel label = new JLabel("BoxLayout Demo");
topButton = new JButton("Left Alignment");
middleButton = new JButton("Center Alignment");
bottomButton = new JButton("Right Alignment");
topButton.addActionListener(this);
middleButton.addActionListener(this);
bottomButton.addActionListener(this);
add(label);
add(topButton);
add(middleButton);
add(bottomButton);
setBackground(Color.white);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == topButton) {
refresh(Component.LEFT_ALIGNMENT);
} else if (event.getSource() == middleButton) {
refresh(Component.CENTER_ALIGNMENT);
} else if (event.getSource() == bottomButton) {
refresh(Component.RIGHT_ALIGNMENT);
}
}
private void refresh(float alignment){
topButton.setAlignmentX(alignment);
middleButton.setAlignmentX(alignment);
bottomButton.setAlignmentX(alignment);
revalidate();
System.out.println("x: "+layout.getLayoutAlignmentX(this));
}
public static void main(String[] args) {
WindowUtilities.setNativeLookAndFeel();
WindowUtilities.openInJFrame(new BoxLayoutTest(), 300, 135,
"BoxLayoutTest");
}
}
