Home Listings By Name By Subject Email www.tomswan.com Help |
© 2001 by Tom Swan. All rights reserved.
Updated: 6/4/01 12:20:54 PM
Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page
001: import java.applet.Applet; 002: import java.awt.Color; 003: import java.awt.TextArea; 004: 005: public class AppletADay extends Applet { 006: // This method initializes the Applet 007: public void init() { 008: setBackground(Color.gray); 009: String s = "An Applet a Day Keeps the Debugger Away!"; 010: add(new TextArea(s, 4, s.length())); 011: } 012: }Return to top
001: import java.awt.*; 002: 003: public class SimpleApp { 004: public static void main(String args[]) { 005: System.out.println("Creating application window..."); 006: Frame f = new Frame("Application Frame Window"); 007: String s = "Press Ctrl+C to close this window!"; 008: f.add(new TextArea(s, 4, s.length())); 009: f.pack(); 010: f.show(); 011: } 012: }Return to top
001: import java.applet.Applet; 002: import java.awt.*; 003: import java.util.Random; 004: 005: public class BackColor extends Applet { 006: Random gen; // Random number generator for color selction 007: String buttonLabel = "Click Me!"; 008: 009: // Initialize applet 010: public void init() { 011: gen = new Random(); 012: Button colorButton = new Button(buttonLabel); 013: add(colorButton); // Added to Applet container 014: } 015: 016: // Respond to button click 017: public boolean action(Event evt, Object what) { 018: Color c; 019: if (buttonLabel.equals(what)) { // Is it our button? 020: do { 021: c = new Color(gen.nextInt()); 022: } while (c == getBackground()); 023: setBackground(c); 024: repaint(); 025: } 026: return true; // Kill event 027: } 028: } 029:Return to top
001: import java.applet.*; 002: import java.awt.*; 003: 004: public class MouseXY extends Applet { 005: String location; // String for X=0 Y=0 display 006: 007: // Initialize applet variables and window 008: public void init() { 009: setBackground(Color.yellow); 010: resize(200, 100); 011: location = new String("Move mouse inside window"); 012: } 013: 014: // Paint the location string inside window 015: public void paint(Graphics g) { 016: g.drawString(location, 10, 10); 017: } 018: 019: // Create the location string from x and y 020: public void makeString(int x, int y) { 021: location = new String( 022: " X=" + String.valueOf(x) + 023: " Y=" + String.valueOf(y) ); 024: } 025: 026: // Handle all events for this applet 027: public boolean handleEvent(Event evt) { 028: boolean eventHandled = false; 029: switch (evt.id) { 030: case Event.MOUSE_DOWN: 031: case Event.MOUSE_UP: 032: case Event.MOUSE_DRAG: 033: case Event.MOUSE_ENTER: 034: case Event.MOUSE_MOVE: { 035: makeString(evt.x, evt.y); 036: repaint(); 037: eventHandled = true; 038: break; 039: } 040: case Event.MOUSE_EXIT: { 041: location = new String("Move mouse inside window"); 042: repaint(); 043: eventHandled = true; 044: } 045: } 046: if (eventHandled) 047: return true; 048: else 049: return super.handleEvent(evt); 050: } 051: }Return to top
001: import java.awt.*; 002: import java.awt.event.*; 003: import java.applet.Applet; 004: 005: public class Delegate extends Applet { 006: 007: // Define two AWT button objects 008: private Button button1 = new Button("Click This!"); 009: private Button button2 = new Button("Click Me!"); 010: 011: // Declare an inner class for the listener object 012: // Toggles between the two buttons 013: class ButtonListener implements ActionListener { 014: public void actionPerformed(ActionEvent e) { 015: if (e.getActionCommand().equals("button1action")) { 016: button1.setEnabled(false); 017: button2.setEnabled(true); 018: } else { 019: button1.setEnabled(true); 020: button2.setEnabled(false); 021: } 022: } 023: } 024: 025: // Applet class constructor 026: public Delegate() { 027: ButtonListener actionObject = new ButtonListener(); 028: button1.setActionCommand("button1action"); 029: button1.addActionListener(actionObject); 030: button2.setActionCommand("button2action"); 031: button2.addActionListener(actionObject); 032: button1.setEnabled(true); 033: button2.setEnabled(false); 034: add(button1); 035: add(button2); 036: } 037: }Return to top
001: import java.applet.Applet; 002: import java.awt.*; 003: import java.awt.event.*; 004: import java.util.Random; 005: 006: public class RandomColor extends Applet { 007: 008: // Constructor 009: public RandomColor() { 010: // Create GUI button object and random generator 011: Button clickMe = new Button("Click Me!"); 012: final Random gen = new Random(); 013: 014: // Create listener using an anonymous class 015: clickMe.addActionListener(new ActionListener() { 016: public void actionPerformed(ActionEvent e) { 017: Color c; 018: do { 019: c = new Color(gen.nextInt()); 020: } while (c == getBackground()); 021: setBackground(c); 022: repaint(); 023: } 024: }); 025: 026: // Add button to Applet container 027: add(clickMe); 028: } 029: }Return to top
001: import java.applet.Applet; 002: import java.awt.*; 003: import java.awt.event.*; 004: import java.util.Random; 005: 006: public class ColorApp 007: extends Panel 008: implements ActionListener { 009: 010: protected Random gen = new Random(); 011: 012: // Constructor 013: public ColorApp() { 014: Button clickMe = new Button("Click Me!"); 015: clickMe.addActionListener(this); 016: add(clickMe); // Add button to panel 017: } 018: 019: // The button's event handler 020: public void actionPerformed(ActionEvent e) { 021: Color c; 022: do { 023: c = new Color(gen.nextInt()); 024: } while (c == getBackground()); 025: setBackground(c); 026: repaint(); 027: } 028: 029: // The main program 030: public static void main(String[] args) { 031: 032: // Create frame and set its size 033: Frame frame = new Frame("Color Application Demo"); 034: frame.setSize(250, 200); 035: frame.setLocation(50, 50); 036: 037: // End the program when the window is closed 038: frame.addWindowListener(new WindowAdapter() { 039: public void windowClosing(WindowEvent e) { 040: System.exit(0); 041: } 042: }); 043: 044: // Add the ColorApp panel to the frame and show it 045: frame.add(new ColorApp(), BorderLayout.CENTER); 046: frame.show(); 047: } 048: }Return to top