Senin, 01 Desember 2014

Pizza Calculator (java Netbeans)

Assamulaikum :)

contoh coding menggunakan checkbox dan radiobutton :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * PizzaCalculatorApp.java
 *
 * Created on Des 2, 2014, 10:10:13 AM
 */
/**
 *
 * @author Eka Mahendra
 */
public class PizzaCalculatorApp extends javax.swing.JFrame {
private PizzaCalculatorPanel pizzapanel;
/** Creates new form PizzaCalculatorApp */
public PizzaCalculatorApp() {
initComponents();
pizzapanel = new PizzaCalculatorPanel();
this.add(pizzapanel);
setTitle("Pizza Calculator");
pack();
setLocationRelativeTo(null);
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pack();
}// </editor-fold>
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PizzaCalculatorApp().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
/*
 * PizzaCalculatorPanel.java
 *
 */
/**
 *
 * @author Eka
 */
public class PizzaCalculatorPanel extends javax.swing.JPanel {
private static final double [] sizeprices = {6.99,8.99,10.99};
private static final double [] toppingprices = {1.49,1.49,1.49,0.99,0.99,0.99};
/** Creates new form PizzaCalculatorPanel */
public PizzaCalculatorPanel() {
initComponents();
btngroupSize.add(radioSizeLarge);
btngroupSize.add(radioSizeMedium);
btngroupSize.add(radioSizeSmall);
radioSizeMedium.setSelected(true);
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btngroupSize = new javax.swing.ButtonGroup();
panelSize = new javax.swing.JPanel();
radioSizeSmall = new javax.swing.JRadioButton();
radioSizeMedium = new javax.swing.JRadioButton();
radioSizeLarge = new javax.swing.JRadioButton();
panelToppings = new javax.swing.JPanel();
cbPepperoni = new javax.swing.JCheckBox();
cbSausage = new javax.swing.JCheckBox();
cbSalami = new javax.swing.JCheckBox();
cbOlives = new javax.swing.JCheckBox();
cbMushroms = new javax.swing.JCheckBox();
cbAnchovies = new javax.swing.JCheckBox();
jLabel1 = new javax.swing.JLabel();
labelTotalPrice = new javax.swing.JLabel();
btnCalculate = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
panelSize.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Size", javax.
swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.
DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
radioSizeSmall.setText("Small");
radioSizeSmall.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioSizeSmallActionPerformed(evt);
}
});
radioSizeMedium.setText("Medium");
radioSizeMedium.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioSizeMediumActionPerformed(evt);
}
});
radioSizeLarge.setText("Large");
radioSizeLarge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioSizeLargeActionPerformed(evt);
}
});
javax.swing.GroupLayout panelSizeLayout = new javax.swing.GroupLayout(panelSize);
panelSize.setLayout(panelSizeLayout);
panelSizeLayout.setHorizontalGroup(
panelSizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelSizeLayout.createSequentialGroup()
.addContainerGap()
.addComponent(radioSizeSmall)
.addGap(18, 18, 18)
.addComponent(radioSizeMedium)
.addGap(18, 18, 18)
.addComponent(radioSizeLarge)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panelSizeLayout.setVerticalGroup(
panelSizeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelSizeLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelSizeLayout.createParallelGroup(javax.swing.GroupLayout.
Alignment.BASELINE)
.addComponent(radioSizeSmall)
.addComponent(radioSizeLarge)
.addComponent(radioSizeMedium))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panelToppings.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
"ToppingsPanel", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.
TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0
))); // NOI18N
cbPepperoni.setText("Pepperoni");
cbSausage.setText("Sausage");
cbSalami.setText("salami");
cbSalami.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbSalamiActionPerformed(evt);
}
});
cbOlives.setText("Olives");
cbMushroms.setText("Mushroms");
cbAnchovies.setText("Anchovies");
javax.swing.GroupLayout panelToppingsLayout = new javax.swing.GroupLayout(
panelToppings);
panelToppings.setLayout(panelToppingsLayout);
panelToppingsLayout.setHorizontalGroup(
panelToppingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelToppingsLayout.createSequentialGroup()
.addGap(14, 14, 14)
.addGroup(panelToppingsLayout.createParallelGroup(javax.swing.GroupLayout.
Alignment.LEADING)
.addComponent(cbSausage)
.addGroup(panelToppingsLayout.createSequentialGroup()
.addComponent(cbSalami)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addComponent(cbPepperoni))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26,
Short.MAX_VALUE)
.addGroup(panelToppingsLayout.createParallelGroup(javax.swing.GroupLayout.
Alignment.LEADING)
.addComponent(cbOlives)
.addComponent(cbAnchovies)
.addComponent(cbMushroms))
.addGap(23, 23, 23))
);
panelToppingsLayout.setVerticalGroup(
panelToppingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelToppingsLayout.createSequentialGroup()
.addGroup(panelToppingsLayout.createParallelGroup(javax.swing.GroupLayout.
Alignment.LEADING)
.addGroup(panelToppingsLayout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(cbPepperoni)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cbSalami))
.addGroup(panelToppingsLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelToppingsLayout.createParallelGroup(javax.swing.
GroupLayout.Alignment.LEADING)
.addGroup(panelToppingsLayout.createSequentialGroup()
.addComponent(cbOlives)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.
UNRELATED)
.addComponent(cbMushroms)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.
UNRELATED)
.addComponent(cbAnchovies))
.addComponent(cbSausage))))
.addContainerGap(18, Short.MAX_VALUE))
);
jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel1.setText("Price");
labelTotalPrice.setText("$");
btnCalculate.setText("Calculate");
btnCalculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateActionPerformed(evt);
}
});
btnExit.setText("Exit");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(labelTotalPrice))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.
Alignment.TRAILING, false)
.addComponent(panelToppings, javax.swing.GroupLayout.
Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE
, Short.MAX_VALUE)
.addComponent(panelSize, javax.swing.GroupLayout.Alignment.
LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.
MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(btnCalculate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExit)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panelSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.
GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(panelToppings, 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(jLabel1)
.addComponent(labelTotalPrice))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
BASELINE)
.addComponent(btnCalculate)
.addComponent(btnExit))
.addContainerGap(12, Short.MAX_VALUE))
);
}// </editor-fold>
private void radioSizeSmallActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void radioSizeMediumActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void radioSizeLargeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void cbSalamiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("good bye");
System.exit(0);
}
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double sizeprice;
if(radioSizeSmall.isSelected()){
sizeprice = sizeprices[0];
}else if(radioSizeMedium.isSelected()){
sizeprice = sizeprices[1];
}else{
sizeprice = sizeprices[2];
}
double toppingprice = 0;
if(cbSausage.isSelected()){
toppingprice = toppingprice + toppingprices[0];
}
if(cbPepperoni.isSelected()){
toppingprice = toppingprice + toppingprices[1];
}
if(cbSalami.isSelected()){
toppingprice = toppingprice + toppingprices[2];
}
if(cbOlives.isSelected()){
toppingprice = toppingprice + toppingprices[3];
}
if(cbMushroms.isSelected()){
toppingprice = toppingprice + toppingprices[4];
}
if(cbAnchovies.isSelected()){
toppingprice = toppingprice + toppingprices[5];
}
double totalprice = sizeprice + toppingprice;
//System.out.println("total:"+totalprice);
labelTotalPrice.setText("$"+totalprice);
}
// Variables declaration - do not modify
private javax.swing.JButton btnCalculate;
private javax.swing.JButton btnExit;
private javax.swing.ButtonGroup btngroupSize;
private javax.swing.JCheckBox cbAnchovies;
private javax.swing.JCheckBox cbMushroms;
private javax.swing.JCheckBox cbOlives;
private javax.swing.JCheckBox cbPepperoni;
private javax.swing.JCheckBox cbSalami;
private javax.swing.JCheckBox cbSausage;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel labelTotalPrice;
private javax.swing.JPanel panelSize;
private javax.swing.JPanel panelToppings;
private javax.swing.JRadioButton radioSizeLarge;
private javax.swing.JRadioButton radioSizeMedium;
private javax.swing.JRadioButton radioSizeSmall;
// End of variables declaration

}

Gambar Design

sorry gan program singkat^^ waalalaikumsalam :)