Contoh ;
Caranya Adalah sbb :
1. Buatlah New Project dan pilih Java Application. Beri nama AppletForm
2. Butlah new file jApplet Form dan berinama Hitung
3. Kemudian buatlah Design nya , (sesuaikan vaiabelnya dengan kode terlampir).Tambahkan Kode yang berwarna Merah:
4. Compile lah dengan cara Run Compile File jan jalan kan Run File. Maka secara otomatis akan terbentuk File Hitung.html
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* hitung.java
*
* Created on 21 Feb 13, 9:14:52
*/
package appletform;
/**
*
* @author ZYREX
*/
public class hitung extends javax.swing.JApplet {
/** Initializes the applet hitung */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel5 = new javax.swing.JLabel();
bOK = new javax.swing.JButton();
tfhasil = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
cboperator = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
tfbil1 = new javax.swing.JTextField();
tfbil2 = new javax.swing.JTextField();
jLabel5.setText("FORM PERHITUNGAN ( CONTOH JAPPLET FORM )");
bOK.setText("Ok");
bOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOKActionPerformed(evt);
}
});
jLabel4.setText("Hasil :");
cboperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));
jLabel2.setText("Bilangan 2 :");
jLabel1.setText("Bilangan 1 :");
jLabel3.setText("Operator : ");
tfbil1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfbil1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(tfbil1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(tfbil2)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cboperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(tfhasil, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(155, 155, 155)
.addComponent(bOK))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(86, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(tfbil1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cboperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tfbil2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(tfhasil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(40, 40, 40)
.addComponent(bOK)
.addContainerGap(113, Short.MAX_VALUE))
);
}// </editor-fold>
private void bOKActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double bil1 = Double.parseDouble(tfbil1.getText());
double bil2 = Double.parseDouble(tfbil2.getText());
double hasil = 0 ;
int op = cboperator.getSelectedIndex();
switch(op){
case 0 : hasil = bil1 + bil2 ; break ;
case 1 : hasil = bil1 - bil2 ; break ;
case 2 : hasil = bil1 * bil2 ; break ;
case 3 : hasil = bil1 / bil2 ; break ;
}
tfhasil.setText(hasil+"");
}
private void tfbil1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton bOK;
private javax.swing.JComboBox cboperator;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField tfbil1;
private javax.swing.JTextField tfbil2;
private javax.swing.JTextField tfhasil;
// End of variables declaration
}