1 / 5

Java Program with Button

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.

danielwoods
Download Presentation

Java Program with Button

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 -

  2. // 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 -

  3. 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 -

  4. 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 -

  5. <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 -

More Related