Tag: code

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);   } }

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()); …

Continue reading

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; …

Continue reading

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 …

Continue reading

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());   } }

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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));   } }

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 …

Continue reading