Lost Password?

  #1 (permalink)  
Old 09-23-2007, 10:02 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,437
Last Blog:
Object Oriented Design...
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default Java: JApplet

All of my tutorials thus far have been using JFrame to create windows. However, Java also supports the Applet, this enables your applications to be viewable in your internet browser. All swing components that have been discussed in my previous tutorials can be used in an Applet. Lets take a look at how an applet with a JButton might look.

We are going to start with our basic class:

Java Code:
  1. package tutorials;
  2.  
  3. public class ApplTut {
  4.  
  5.     public ApplTut() {
  6.        
  7.     }
  8.    
  9. }

When creating the basic class, one of the main differences between a java application and an java applet is that applets do not use the
Java Code:
  1. public static void main(String args[])
as its main method for instantiation - it uses the init method.

First we must extend the JApplet, just as we extend JFrame when making applications. Also we import javax.swing.JApplet.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JApplet;
  4.  
  5. public class ApplTut extends JApplet {
  6.  
  7.     public ApplTut() {
  8.        
  9.     }
  10.  
  11.    
  12. }

Next we add the init method.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JApplet;
  4.  
  5. public class ApplTut extends JApplet {
  6.  
  7.     public ApplTut() {
  8.        
  9.     }
  10.  
  11.     public void init() {
  12.        
  13.     }
  14.    
  15. }

And that is the basic shell of an applet. To add swing components, use the same method's you would for an application. Here is an example of how you would add a JButton.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JApplet;
  4. import javax.swing.JButton;
  5.  
  6. public class ApplTut extends JApplet {
  7.     private JButton button;
  8.    
  9.     public ApplTut() {
  10.        
  11.     }
  12.  
  13.     public void init() {
  14.         button = new JButton("Click Me");
  15.         add(button);
  16.        
  17.     }
  18.    
  19. }
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about java games stack Java Help 3 07-02-2007 05:35 PM
MS-SQL deadlock and hang the Java application reachpradeep Database & Database Programming 1 03-11-2007 04:20 AM
Java Facts techni68 Java Help 0 01-17-2007 01:41 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM
Java Help Files xXHalfSliceXx Java Help 3 11-28-2006 11:30 PM


All times are GMT -5. The time now is 10:30 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads