Menggunakan JCheckBox untuk pembuatan Nota MenuMakanan. Seperti di bawah ini :
package swingsembarang;
import javax.swing.JFrame;
/*
* MenuMakan.java
*
* Created on July 21, 2009, 11:32 AM
*/
/**
*
* @author Christanti
*/
public class MenuMakan extends javax.swing.JFrame {
/** Creates new form MenuMakan */
public MenuMakan() {
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
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
cekSoto = new javax.swing.JCheckBox();
cekRawon = new javax.swing.JCheckBox();
cekBakso = new javax.swing.JCheckBox();
cekGudeg = new javax.swing.JCheckBox();
textSoto = new javax.swing.JTextField();
textRawon = new javax.swing.JTextField();
textBakso = new javax.swing.JTextField();
textGudeg = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
textHitung = new javax.swing.JTextField();
buttonHitung = new javax.swing.JButton();
buttonClear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Arial Black", 0, 12)); // NOI18N
jLabel1.setText("Menu Makanan");
cekSoto.setText("SOTO :5000");
cekSoto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cekSotoActionPerformed(evt);
}
});
cekRawon.setText("RAWON :6000");
cekRawon.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cekRawonActionPerformed(evt);
}
});
cekBakso.setText("BAKSO :4000");
cekBakso.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cekBaksoActionPerformed(evt);
}
});
cekGudeg.setText("GUDEG :8000");
cekGudeg.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cekGudegActionPerformed(evt);
}
});
textSoto.setEditable(false);
textSoto.setText("0");
textSoto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textSotoActionPerformed(evt);
}
});
textRawon.setEditable(false);
textRawon.setText("0");
textBakso.setEditable(false);
textBakso.setText("0");
textGudeg.setEditable(false);
textGudeg.setText("0");
jLabel2.setText("Hitung");
textHitung.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textHitungActionPerformed(evt);
}
});
buttonHitung.setText("Hitung");
buttonHitung.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonHitungActionPerformed(evt);
}
});
buttonClear.setText("Clear");
buttonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonClearActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(147, 147, 147)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(71, 71, 71)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(cekGudeg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cekBakso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cekRawon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cekSoto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(textGudeg, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addComponent(textRawon, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addComponent(textBakso, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addComponent(textSoto, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addComponent(textHitung))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(buttonHitung, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonClear, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(47, 47, 47))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jLabel1)
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textSoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cekSoto))
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(cekRawon)
.addComponent(textRawon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cekBakso)
.addComponent(textBakso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cekGudeg)
.addComponent(textGudeg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(6, 6, 6)
.addComponent(buttonHitung)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(textHitung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonClear))
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
// TODO add your handling code here:
cekSoto.setSelected(false);
textSoto.setText("0");
textSoto.setEditable(false);
cekRawon.setSelected(false);
textRawon.setText("0");
textRawon.setEditable(false);
cekBakso.setSelected(false);
textBakso.setText("0");
textBakso.setEditable(false);
cekGudeg.setSelected(false);
textGudeg.setText("0");
textGudeg.setEditable(false);
}//GEN-LAST:event_buttonClearActionPerformed
@SuppressWarnings("static-access")
private void cekSotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cekSotoActionPerformed
// TODO add your handling code here:
if (cekSoto.isSelected()== true){//GEN-LAST:event_cekSotoActionPerformed
//textSoto.enable(true);
textSoto.setEditable(true);
textSoto.setText("");
} else {
//textSoto.enable(false);
textSoto.setEditable(false);
textSoto.setText("0");
}
}
private void cekRawonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cekRawonActionPerformed
// TODO add your handling code here:
if (cekRawon.isSelected() == true) {
//textRawon.enable(true);
textRawon.setEditable(true);
textRawon.setText("");
} else {
//textRawon.enable(false);
textRawon.setEditable(false);
textRawon.setText("0");
}
}//GEN-LAST:event_cekRawonActionPerformed
private void cekBaksoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cekBaksoActionPerformed
// TODO add your handling code here:
if (cekBakso.isSelected()) {
//textBakso.enable(true);
textBakso.setEditable(true);
textBakso.setText("");
} else {
//textBakso.enable(false);
textBakso.setEditable(false);
textBakso.setText("0");
}
}//GEN-LAST:event_cekBaksoActionPerformed
private void cekGudegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cekGudegActionPerformed
// TODO add your handling code here:
if (cekGudeg.isSelected()) {
//textGudeg.enable(true);
textGudeg.setEditable(true);
textGudeg.setText("");
} else {
//textGudeg.enable(false);
textGudeg.setEditable(false);
textGudeg.setText("0");
}//GEN-LAST:event_cekGudegActionPerformed
}
private void buttonHitungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHitungActionPerformed
// TODO add your handling code here:
double hitung = 0;
Double soto = (Double) ((Double.parseDouble(textSoto.getText())) * 5000);
Double rawon = (Double) ((Double.parseDouble(textRawon.getText())) * 6000);
Double Bakso = (Double) ((Double.parseDouble(textBakso.getText())) * 4000);
Double Gudeg = (Double) ((Double.parseDouble(textGudeg.getText())) * 8000);
hitung = soto + rawon + Bakso + Gudeg;
textHitung.setText(hitung + "");
}//GEN-LAST:event_buttonHitungActionPerformed
private void textSotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textSotoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_textSotoActionPerformed
private void textHitungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textHitungActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_textHitungActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MenuMakan().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonClear;
private javax.swing.JButton buttonHitung;
private javax.swing.JCheckBox cekBakso;
private javax.swing.JCheckBox cekGudeg;
private javax.swing.JCheckBox cekRawon;
private javax.swing.JCheckBox cekSoto;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField textBakso;
private javax.swing.JTextField textGudeg;
private javax.swing.JTextField textHitung;
private javax.swing.JTextField textRawon;
private javax.swing.JTextField textSoto;
// End of variables declaration//GEN-END:variables
}
Selamat Mencoba . . . ^^