A simple Swing-based applet : Applet « Development « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Development » Applet 
6.28.7.A simple Swing-based applet
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
 
/* 
This HTML can be used to launch the applet: 
 
<object code="MyApplet" width=240 height=100> 
</object> 
 
*/ 
 
public class MyApplet extends JApplet 
  JButton jbtnOne; 
  JButton jbtnTwo; 
 
  JLabel jlab; 
 
  public void init() { 
    try 
      SwingUtilities.invokeAndWait(new Runnable () { 
        public void run() { 
          guiInit()// initialize the GUI 
        
      })
    catch(Exception exc) { 
      System.out.println("Can't create because of "+ exc)
    
  
 
  // Called second, after init().  Also called 
  // whenever the applet is restarted.  
  public void start() { 
    // Not used by this applet. 
  
 
  // Called when the applet is stopped. 
  public void stop() { 
    // Not used by this applet. 
  
 
  // Called when applet is terminated.  This is 
  // the last method executed. 
  public void destroy() { 
    // Not used by this applet. 
  
 
  // Setup and initialize the GUI.  
  private void guiInit() { 
    // Set the applet to use flow layout. 
    setLayout(new FlowLayout())
 
    // Create two buttons and a label. 
    jbtnOne = new JButton("One")
    jbtnTwo = new JButton("Two")
 
    jlab = new JLabel("Press a button.")
 
    // Add action listeners for the buttons. 
    jbtnOne.addActionListener(new ActionListener() {      
      public void actionPerformed(ActionEvent le) {  
        jlab.setText("Button One pressed.");  
      }      
    });      
 
    jbtnTwo.addActionListener(new ActionListener() {      
      public void actionPerformed(ActionEvent le) {  
        jlab.setText("Button Two pressed.");  
      }      
    });      
 
    // Add the components to the applet's content pane. 
    getContentPane().add(jbtnOne)
    getContentPane().add(jbtnTwo)
    getContentPane().add(jlab);     
  
}
6.28.Applet
6.28.1.Applet
6.28.2.A real applet
6.28.3.Add JButton to JApplet
6.28.4.Access applet parameters
6.28.5.An Swing-based applet skeleton
6.28.6.Swing Applet
6.28.7.A simple Swing-based applet
6.28.8.Mini Appletviewer
6.28.9.Tic Tac Toe game
6.28.10.WeatherWizard JApplet
6.28.11.Change an applet background color
6.28.12.Read an applet parameters
6.28.13.Calling methods of an Applet from JavaScript code
6.28.14.Passing Parameters to Java Applet
6.28.15.Display message in browser status bar
6.28.16.System properties accessible to applets
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.