Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java problem Create an application that allows the user to select toppings for t

ID: 657553 • Letter: J

Question

Java problem

Create an application that allows the user to select toppings for their pizza via Checkboxes. Line up the checkboxes to make your GUI look nice. You may use any layout manager that you want to do this problem. I did the problem using both the flowlayout and GridLayout managers. You may also use JPanels.

The price of the toppings (taken form my code) are shown below. You may define your toppings differently and with different names but use the same price as I did for each topping. you don

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication17;

/**
*
* @author Sravan Yadav E
*/
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

/**
* A Swing program that demonstrates how to use JCheckBox component.
* @author www.codejava.net
*
*/
public class JavaApplication17 extends JFrame {
   // create three check boxes that allow selecting three numbers
   private JCheckBox checkboxOne = new JCheckBox("SHEESE");
   private JCheckBox checkboxTwo = new JCheckBox("PEPPERONI");
   private JCheckBox checkboxThree = new JCheckBox("SAUSAGE");
   private JCheckBox checkboxFour = new JCheckBox("GREEN_PEPPERS");
   // a label and a text field to display sum
   private JLabel labelSum = new JLabel("Total: $");
        private JLabel jl1=new JLabel("Base Pizza cost $10.00");
        private JLabel jl=new JLabel("Pizza Toppoing");
   private JTextField textFieldSum = new JTextField(10);
  
   private double sum = 10;   // sum of 3 numbers
  
   public JavaApplication17() throws IOException {
       super("Swing JCheckBox Demo Program");
       setLayout(new GridLayout(0, 1));                      
                BufferedImage mypic=ImageIO.read(new File("E:\MyWork\Courses and notes\Chegg\2015\May\Qus3\logo.gif"));
                JLabel picl=new JLabel(new ImageIcon(mypic));
                add(picl);       // add the check boxes to this frame
                add(jl1);
                jl1.setForeground(Color.red);
                add(jl);
       add(checkboxOne);
       add(checkboxTwo);
       add(checkboxThree);
       add(checkboxFour);
       add(labelSum);
      
       textFieldSum.setEditable(false);
       add(textFieldSum);
      
       // add action listener for the check boxes
       ActionListener actionListener = new ActionHandler();
       checkboxOne.addActionListener(actionListener);
       checkboxTwo.addActionListener(actionListener);
       checkboxThree.addActionListener(actionListener);
       checkboxFour.addActionListener(actionListener);
       pack();
       setLocationRelativeTo(null);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   class ActionHandler implements ActionListener {
       @Override
       public void actionPerformed(ActionEvent event) {
           JCheckBox checkbox = (JCheckBox) event.getSource();
           if (checkbox.isSelected()) {
               if (checkbox == checkboxOne) {
                   sum += 2.50;
               } else if (checkbox == checkboxTwo) {
                   sum += 3.00;
               } else if (checkbox == checkboxThree) {
                   sum += 4.00;
               } else if(checkbox == checkboxFour) {
                                        sum+=1.50;
                                }
           }
                        else {
               if (checkbox == checkboxOne) {
                   sum -= 2.50;
               } else if (checkbox == checkboxTwo) {
                   sum -= 3.00;
               } else if (checkbox == checkboxThree) {
                   sum -= 4.00;
               } else if(checkbox == checkboxFour) {
                                        sum-=1.50;
                                }
           }
           textFieldSum.setText(String.valueOf(sum));
       }
   }
  
   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
                        try {
                            new JavaApplication17().setVisible(true);
                        } catch (IOException ex) {
                            Logger.getLogger(JavaApplication17.class.getName()).log(Level.SEVERE, null, ex);
                        }
           }
       });
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote