50 likes | 138 Views
Java Program with Button. Works as an applet or as an app. Includes a private class to handle mouse events, implementing the MouseListener interface. Puts up a single button labelled “Press Me!” When the button is clicked a new frame (window) is created with a message in it.
E N D
Java Program with Button • Works as an applet or as an app. • Includes a private class to handle mouse events, implementing the MouseListener interface. • Puts up a single button labelled “Press Me!” • When the button is clicked a new frame (window) is created with a message in it. • Note: the new frame doesn’t go away! CSE 341, S. Tanimoto Java-PressMe -
// PressMe.java Steven L. Tanimoto, 2 April 1999. import java.applet.*; import java.awt.*; import java.awt.event.*; public class PressMe extends Applet { public static boolean reallyAnApplet = true; public static void main( String [] args) { Frame f = new Frame(); reallyAnApplet = false; PressMe pm = new PressMe(); f.add(pm); pm.resize(550, 150); pm.init(); f.setSize(600, 200); f.show(); pm.invalidate(); pm.start(); } CSE 341, S. Tanimoto Java-PressMe -
BorderLayout layout; Button panicButton; public void init() { layout = new BorderLayout(); setLayout( layout ); panicButton = new Button("Press Me!"); panicButton.addActionListener(new PanicButtonListener()); add("North", panicButton); Label alabel; if (reallyAnApplet) alabel= new Label("This is an applet."); else alabel= new Label("This is not an applet but an application."); add("Center",alabel); } CSE 341, S. Tanimoto Java-PressMe -
private class PanicButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Frame f = new Frame(); Label lab = new Label("Don't Panic!"); f.add(lab); f.setSize(200, 100); f.show(); } } } CSE 341, S. Tanimoto Java-PressMe -
<html> <head> <title>The Java Pixel Calculator Applet</title> </head> <body> This is a test of the PressMe button. applet for viewing the numerical pixel values of an image. <br> <applet code="PressMe.class" width=600 height=400> </applet> <br> </body> </html> CSE 341, S. Tanimoto Java-PressMe -