You are on page 1of 13

Modul 3:

Tujuan : Mengenal Komponen dan Kontainer GUI SWING

1.Desain Interface:

2.Coding:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* java_swing.java
*
* Created on Jun 16, 2009, 3:09:31 PM
*/

/**
*
* @author Acer
*/
import javax.swing.JOptionPane;
public class java_swing extends javax.swing.JFrame {

/** Creates new form java_swing */


public java_swing() {
initComponents();
}

/** This method is called from within the constructor to


* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
Write Once, Run Everywhere
Rahmadi, S.E, S.Kom
Page |1
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();


jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jchk_paket1 = new javax.swing.JCheckBox();
jchk_paket2 = new javax.swing.JCheckBox();
jLabel3 = new javax.swing.JLabel();
jtxt_total = new javax.swing.JTextField();
jchk_paket3 = new javax.swing.JCheckBox();
jInternalFrame3 = new javax.swing.JInternalFrame();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jcmb_paket = new javax.swing.JComboBox();
jLabel6 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jtxtarea = new javax.swing.JTextArea();
jInternalFrame2 = new javax.swing.JInternalFrame();
jLabel7 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel8 = new javax.swing.JLabel();
jrdo_paket1 = new javax.swing.JRadioButton();
jrdo_paket2 = new javax.swing.JRadioButton();
jrdo_paket3 = new javax.swing.JRadioButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Form Java Swing");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});

jInternalFrame1.setBackground(new java.awt.Color(0, 255, 204));


jInternalFrame1.setVisible(true);

jLabel1.setFont(new java.awt.Font("Cooper Black", 1, 14)); // NOI18N


jLabel1.setText("Paket Pembayaran Cepat Saji");

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |2
jLabel2.setText("Jenis Paket");

jchk_paket1.setText("Paket 1");
jchk_paket1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jchk_paket1ActionPerformed(evt);
}
});

jchk_paket2.setText("Paket 2");
jchk_paket2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jchk_paket2ActionPerformed(evt);
}
});

jLabel3.setText("Total Harga");

jtxt_total.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtxt_totalActionPerformed(evt);
}
});

jchk_paket3.setText("Paket 3");
jchk_paket3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jchk_paket3ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(32, 32, 32)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |3
.addComponent(jchk_paket3)
.addComponent(jchk_paket2)
.addComponent(jchk_paket1)
.addComponent(jtxt_total, javax.swing.GroupLayout.PREFERRED_SIZE, 157,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jchk_paket1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jchk_paket2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jchk_paket3)
.addGap(22, 22, 22)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jtxt_total, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(41, Short.MAX_VALUE))
);

javax.swing.GroupLayout jInternalFrame1Layout = new


javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(48, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jInternalFrame1Layout.createSequentialGroup()
.addContainerGap(64, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(63, 63, 63))
);

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |4
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(341, Short.MAX_VALUE))
);

jInternalFrame3.setBackground(new java.awt.Color(204, 255, 102));


jInternalFrame3.setVisible(true);

jLabel4.setFont(new java.awt.Font("Cooper Black", 1, 14)); // NOI18N


jLabel4.setText("Paket Pembayaran Cepat Saji");

jLabel5.setText("Jenis Paket");

jcmb_paket.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Paket 1", "Paket


2", "Paket 3" }));
jcmb_paket.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmb_paketActionPerformed(evt);
}
});

jLabel6.setText("Total Harga");

jtxtarea.setColumns(20);
jtxtarea.setEditable(false);
jtxtarea.setRows(5);
jScrollPane1.setViewportView(jtxtarea);

javax.swing.GroupLayout jInternalFrame3Layout = new


javax.swing.GroupLayout(jInternalFrame3.getContentPane());
jInternalFrame3.getContentPane().setLayout(jInternalFrame3Layout);
jInternalFrame3Layout.setHorizontalGroup(
jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame3Layout.createSequentialGroup()
.addGap(74, 74, 74)

.addGroup(jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |5
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jInternalFrame3Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addGap(22, 22, 22))
.addGroup(jInternalFrame3Layout.createSequentialGroup()

.addGroup(jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66,
Short.MAX_VALUE)

.addGroup(jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
.addComponent(jcmb_paket, javax.swing.GroupLayout.PREFERRED_SIZE, 157,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(127, 127, 127))
);
jInternalFrame3Layout.setVerticalGroup(
jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame3Layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jLabel4)
.addGap(45, 45, 45)

.addGroup(jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE
)
.addComponent(jLabel5)
.addComponent(jcmb_paket, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)

.addGroup(jInternalFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(351, Short.MAX_VALUE))
);

jInternalFrame2.setBackground(new java.awt.Color(0, 255, 204));


jInternalFrame2.setVisible(true);

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |6
jLabel7.setFont(new java.awt.Font("Cooper Black", 1, 14)); // NOI18N
jLabel7.setText("Paket Pembayaran Cepat Saji");

jLabel8.setText("Jenis Paket");

jrdo_paket1.setText("Paket 1");
jrdo_paket1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jrdo_paket1ActionPerformed(evt);
}
});

jrdo_paket2.setText("Paket 2");
jrdo_paket2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jrdo_paket2ActionPerformed(evt);
}
});

jrdo_paket3.setText("Paket 3");
jrdo_paket3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jrdo_paket3ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);


jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jrdo_paket3)
.addComponent(jrdo_paket2)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(57, 57, 57)
.addComponent(jrdo_paket1)))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |7
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(30, 30, 30)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jrdo_paket1))
.addGap(18, 18, 18)
.addComponent(jrdo_paket2)
.addGap(30, 30, 30)
.addComponent(jrdo_paket3)
.addContainerGap(42, Short.MAX_VALUE))
);

javax.swing.GroupLayout jInternalFrame2Layout = new


javax.swing.GroupLayout(jInternalFrame2.getContentPane());
jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
jInternalFrame2Layout.setHorizontalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addGap(44, 44, 44)

.addGroup(jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
)
.addComponent(jLabel7)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(90, Short.MAX_VALUE))
);
jInternalFrame2Layout.setVerticalGroup(
jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame2Layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jLabel7)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(334, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |8
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jInternalFrame3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jInternalFrame2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(349, 349, 349))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jInternalFrame2)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jInternalFrame1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame3))
.addContainerGap())
);

pack();
}// </editor-fold>

private void formWindowActivated(java.awt.event.WindowEvent evt) {


// TODO add your handling code here:
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}

private void jchk_paket1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==false&&jchk_paket3.isSelected()==fals
e) {
jtxt_total.setText("Rp. 15,000.00");
} else

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==false
){
jtxt_total.setText("Rp. 25,000.00");
} else

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
Page |9
if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==true
){
jtxt_total.setText("Rp. 35,000.00");
} else {
jtxt_total.setText("Rp. 0.00");
}
}

private void jchk_paket2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==false&&jchk_paket3.isSelected()==fals
e) {
jtxt_total.setText("Rp. 15,000.00");
} else

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==false
){
jtxt_total.setText("Rp. 25,000.00");
} else

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==true
){
jtxt_total.setText("Rp. 35,000.00");
} else {
jtxt_total.setText("Rp. 0.00");
}
}

private void jtxt_totalActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void jchk_paket3ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==false&&jchk_paket3.isSelected()==fals
e) {
jtxt_total.setText("Rp. 15,000.00");
} else

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==false
){

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
P a g e | 10
jtxt_total.setText("Rp. 25,000.00");
} else

if(jchk_paket1.isSelected()==true&&jchk_paket2.isSelected()==true&&jchk_paket3.isSelected()==true
){
jtxt_total.setText("Rp. 35,000.00");
} else {
jtxt_total.setText("Rp. 0.00");
}
}

private void jcmb_paketActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
int pilih = jcmb_paket.getSelectedIndex();
switch (pilih){
case 0 :
jtxtarea.setText("Paket 1 Harga Rp. 5,000.00");
break;
case 1 :
jtxtarea.setText("Paket 2 Harga Rp. 10,000.00");
break;
case 2 :
jtxtarea.setText("Paket 3 Harga Rp. 15,000.00");
break;
default:
jtxtarea.setText("Paket Belum Tersedia");
}
}

private void jrdo_paket1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
if(jrdo_paket1.isSelected()==true)
{
JOptionPane.showMessageDialog(null,"Paket 1 Harga Rp. 5,000.00");
}
else
JOptionPane.showMessageDialog(null,"Silahkan Pilih Paket 1");
}

private void jrdo_paket2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
if(jrdo_paket2.isSelected()==true)
{
JOptionPane.showMessageDialog(null,"Paket 2 Harga Rp. 10,000.00");

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
P a g e | 11
}
else
JOptionPane.showMessageDialog(null,"Silahkan Pilih Paket 2");
}

private void jrdo_paket3ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
if(jrdo_paket3.isSelected()==true)
{
JOptionPane.showMessageDialog(null,"Paket 3 Harga Rp. 15,000.00");
}
else
JOptionPane.showMessageDialog(null,"Silahkan Pilih Paket 3");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new java_swing().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JInternalFrame jInternalFrame2;
private javax.swing.JInternalFrame jInternalFrame3;
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.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JCheckBox jchk_paket1;
private javax.swing.JCheckBox jchk_paket2;
private javax.swing.JCheckBox jchk_paket3;

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
P a g e | 12
private javax.swing.JComboBox jcmb_paket;
private javax.swing.JRadioButton jrdo_paket1;
private javax.swing.JRadioButton jrdo_paket2;
private javax.swing.JRadioButton jrdo_paket3;
private javax.swing.JTextField jtxt_total;
private javax.swing.JTextArea jtxtarea;
// End of variables declaration

3.Implementation:

Write Once, Run Everywhere


Rahmadi, S.E, S.Kom
P a g e | 13

You might also like