Wednesday, July 25, 2012

Swing Example

Here is a picture of the form operating


Here is the Program.java class

package com.spconger.www;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) {
  MainJFrame m = new MainJFrame();
  m.setTitle("tip Calculator");
  m.setBounds(200,200,350, 350);
  
  m.setVisible(true);
 }

}

Here is the MainJFrame.java class

package com.spconger.www;

import javax.swing.*;

public class MainJFrame extends JFrame{

 /**
  * this method creates the JFrame
  * and adds the panel
  */
 private static final long serialVersionUID = 1L;
 
 public MainJFrame(){
  //JFrame frame = new JFrame();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel mainPanel= new MealTipPanel();
  this.add(mainPanel);
  
  
 }
 
 

}


Here is the MealTipPanel.Java class

package com.spconger.www;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MealTipPanel extends JPanel{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 //declare all my objects
 JPanel panel;
 JPanel buttonPanel;
 JLabel lblAmount;
 JTextField txtAmount;
 JLabel lblChooseTipPercent;
 JRadioButton rbFifteenPercent;
 JRadioButton rbTenPercent;
 JRadioButton rbTwentyPercent;
 JLabel lblTaxPercent;
 JTextField txtTaxPercent;
 JLabel lblTipAmount;
 JLabel lblTaxAmount;
 JLabel lblTotalAmount;
 
 JButton btnCalculate;
 JButton btnExit;
 
 private Tip tip;
 
 
 public MealTipPanel(){
  createPanel();
 }
 
 private void createPanel(){
  panel = new JPanel();
  panel.setLayout(new GridLayout(10,2,5,5));
  
  lblAmount = new JLabel("Enter the Pre Tax Amount");
  txtAmount = new JTextField(10);
  
  lblChooseTipPercent = new JLabel("Choose the Tip Percent");
  rbTenPercent = new JRadioButton("Ten Percent");
  rbFifteenPercent = new JRadioButton("Fifteen Percent");
  rbTwentyPercent = new JRadioButton("Twenty Percent");
  
  ButtonGroup group = new ButtonGroup();
  group.add(rbTenPercent);
  group.add(rbFifteenPercent);
  group.add(rbTwentyPercent);
  
  lblTaxPercent=new JLabel("Enter Tax Percent");
  txtTaxPercent= new JTextField(10);
  
  lblTipAmount = new JLabel();
  lblTaxAmount= new JLabel();
  lblTotalAmount = new JLabel();
  
  panel.add(lblAmount);
  panel.add(txtAmount);
  panel.add(lblChooseTipPercent);
  panel.add(rbTenPercent);
  panel.add(rbFifteenPercent);
  panel.add(rbTwentyPercent);
  panel.add(lblTaxPercent);
  panel.add(txtTaxPercent);
  panel.add(lblTipAmount);
  panel.add(lblTaxAmount);
  panel.add(lblTotalAmount);
  
  buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  
  btnCalculate = new JButton("Calculate");
  btnCalculate.addActionListener(new CalculateListener());
  buttonPanel.add(btnCalculate);
  
  btnExit = new JButton("Exit");
  btnExit.addActionListener(new ExitListener());
  buttonPanel.add(btnExit);
  
  this.setLayout(new BorderLayout());
  this.add(panel, BorderLayout.CENTER);
  this.add(buttonPanel, BorderLayout.SOUTH);
  
 }//end create panel
 
 private class ExitListener implements ActionListener{

  @Override
  public void actionPerformed(ActionEvent e) {
   // TODO Auto-generated method stub
   System.exit(0);
  }
  
 }//end of ExitListenerClass
 
 private class CalculateListener implements ActionListener{

  @Override
  public void actionPerformed(ActionEvent e) {
   tip = new Tip();
   //set the field values
   tip.setMealAmount(Double.parseDouble(txtAmount.getText()));
   
   double percent=0;
   if (rbTenPercent.isSelected()){
    percent=.1;
   }
   else if (rbFifteenPercent.isSelected()){
    percent=.15;
   }
   else if (rbTwentyPercent.isSelected())
   {
    percent=.2;
   }
    
   tip.setTipPercent(percent);
   tip.setTaxPercent(Double.parseDouble(txtTaxPercent.getText()));
   
   lblTipAmount.setText("The tip amount is " 
             + Double.toString(tip.calculateTip()));
   
   lblTaxAmount.setText("The tax amount is " +
            Double.toString(tip.calculateTax()));
   
   lblTotalAmount.setText("The Total is " +
     Double.toString(tip.calculateTotal()));
   
  }
  
 }
 
 
}//end class



Here is the Tip.java class

package com.spconger.www;

public class Tip {
 private double mealAmount;
 private double tipPercent;
 private double taxPercent;
 
 public double getMealAmount() {
  return mealAmount;
 }
 public void setMealAmount(double mealAmount) {
  this.mealAmount = mealAmount;
 }
 public double getTipPercent() {
  return tipPercent;
 }
 public void setTipPercent(double tipPercent) {
  if (tipPercent >=1){
   tipPercent=tipPercent/100;
  }
  this.tipPercent = tipPercent;
 }
 public double getTaxPercent() {
 
  return taxPercent;
 }
 public void setTaxPercent(double taxPercent) {
  if (taxPercent >= 1)
  {
   taxPercent=taxPercent /100;
  }
  this.taxPercent = taxPercent;
 }
 
 public double calculateTip(){
  return getMealAmount() * getTipPercent();
 }
 
 public double calculateTax(){
  return getMealAmount() * getTaxPercent();
 }
 
 public double calculateTotal(){
  return getMealAmount() + calculateTip() + calculateTax();
 }
 

}

No comments:

Post a Comment