Membuat Applet Sederhana

October 30, 2009

Langsung saja.

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

/**
 * @berkas: AppletSelamatDatang.java
 * @author: The Petarung
 * @web: https://thepetarung.wordpress.com
 * @date: 30 Oktober 2009
 */

public class AppletSelamatDatang extends JApplet{
    public void init(){
        EventQueue.invokeLater(new Runnable(){
            public void run(){
                setLayout(new BorderLayout());

                JLabel label = new JLabel(getParameter("selamat"), SwingConstants.CENTER);
                label.setFont(new Font("Serif", Font.BOLD, 18));
                add(label, BorderLayout.CENTER);

                JPanel panel = new JPanel();

                JButton pet1Button = new JButton("The Petarung Blog");
                pet1Button.addActionListener(makeAction("https://thepetarung.wordpress.com"));
                panel.add(pet1Button);

                JButton pet2Button = new JButton("The Petarung Mail");
                pet2Button.addActionListener(makeAction("mailto:petarung.petarung@gmail.com"));
                panel.add(pet2Button);

                add(panel, BorderLayout.SOUTH);
            }
        });
    }
    private ActionListener makeAction(final String urlString){
        return new ActionListener(){
            public void actionPerformed(ActionEvent event){
                try{
                    getAppletContext().showDocument(new URL(urlString));
                }
                catch (MalformedURLException e){
                    e.printStackTrace();
                }
            }
        };
    }
}

Kemudian, compile-lah AppletSelamatDatang.java, sehingga menjadi AppletSelamatDatang.class

  • C:\javac AppletSelamatDatang.java
  • *)Catatan: Set path java untuk command-line telah dilakukan. Berkas AppletSelamatDatang.java berada di C:\

    Untuk menjalankan applet, dengan menggunakan IDE, tinggal compile-run.Selesai!

    Jika ingin secara manual, buat dahulu sebuah file HTML, misal test.html.

    <html>
      <body>
         <applet code="AppletSelamatDatang.class" width="400" height="200">
            <param name="selamat" value="Tulisan ini di tengah!"/>
         </applet>
      </body>
    </html>
    

    Kemudian untuk menjalankan applet-nya, ketikkan perintah:

  • C:\appletviewer test.html
  • maka hasilnya kurang lebih seperti ini:

    3 Responses to “Membuat Applet Sederhana”

    1. fathur Says:

      keren!! like this!! ^^

    2. kim yong's Says:

      caranya compile gmana ya bro ?


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: