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

In Java Assume you have a GUI program where the user selects up to three items t

ID: 3691679 • Letter: I

Question

In Java
Assume you have a GUI program where the user selects up to three items they want to buy and then selects a shipping speed.
The user then clicks a "purchase" button. Your program should output a summary statement, for example:

You purchased these items at regular shipping: item1 item2 item3
You purchased these items at fast shipping: item2 item3

Write the code that would go inside of the handler/listener for the button.
Use these references:
item1Checkbox
item2Checkbox
item3Checkbox
regularShippingSpeedRadioButton
fastShippingSpeedRadioButton
summaryTextField

Explanation / Answer

Answer:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class GuiExample extends Applet implements ActionListener
{   
//SUMMARY TEXTFIELD
    TextField summary;

    CheckboxGroup radioGroup;

    Checkbox item1;
    Checkbox item2;
    Checkbox item3;
    RadioButton regularShippingSpeed;
   RadioButton fastShippingSpeed;
   Button purchase;
   ButtonGroup bg;
//INITIALIZES THE COMPONENT
    public void init()
    {

   setLayout(new FlowLayout()); //SET LAYOUT
  
   summary = new TextField(100);
  
   itemGroup = new CheckboxGroup();
  
   item1 = new Checkbox("item1", itemGroup,false);
  
   item2 = new Checkbox("item2", itemGroup,true);
  
   item3 = new Checkbox("item3",false);
//RADIO BUTTON
       regularShippingSpeed=new RadioButton("Regular");
       regularShippingSpeed.setActionCommand("Regular");

       fastShippingSpeed=new RadioButton("Fast") ;
       fastShippingSpeed.setActionCommand("Fast");
//PURCHASE BUTTON
       purchase=new Button("Purchase");
//ADD ACTIONLISTENER
       purchase.addActionLister(this);
       bg=new ButtonGroup();
//ADDING RADIO BUTTON TO THE BUTTON GROUP
       bg.add(regularShippingSpeed);
       bg.add(fastShippingSpeed);
//ADDING CHECKBOXES,RADIO BUTTONS, BUTTONS, TEXTFIELD
       add(item1);
       add(item2);
       add(item3);
       add(regularShippingSpeed);
       add(fastShippingSpeed);
       add(summary);
       add(purchase);

    }
//ACTIONLISTENER
   public void actionPerformed(ActionEvent actEt)
   {
       String itemChecked="";
       String t;
       if(actEt.getActionCommand().equals("Purchase"))
       {
           if(item1.getState())
               itemChecked+="item1";
           if(item2.getState())
               itemChecked+=" item2";
           if(item2.getState())
               itemChecked+=" item3";
      
           if(bg.getSelection().getActionCommand().equals("Regular"))
           {
               t="You purchased these items at regular shipping:"+itemChecked;  
               summary.setText(t);
           }
           else if(bg.getSelection().getActionCommand().equals("Fast"))
           {
               t="You purchased these items at fast shipping:"+itemChecked;  
               summary.setText(t);
           }
       }
   }
}

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