In Chapter 14, you created an interactive GUI application for Carly\'s Catering
ID: 3580159 • Letter: I
Question
In Chapter 14, you created an interactive GUI application for Carly's Catering that allows the user to enter a number of guests for an event and to choose an entrée, two side dishes, and a dessert from groups of choices. Then, the application displays the cost of the event and a list of the chosen items. Now, modify the interface to include separate panels for the guest number entry, each group of menu choices, and the output. Use at least two different layout managers and at least two different colors in your application. Save the program as JCarlysCatering.java. here is my code so far
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JCarlysCatering extends JFrame
{
JButton btnOK;
ButtonOKListener btnOKListener;
JFrame Frame1 = new JFrame("Test Frame");
JPanel j1 = new JPanel (new FlowLayout());
JTextField t1 = new JTextField(20);
Label headerLabel = new Label();
Label statusLabel = new Label();
public static void main(String[] args)
{
JCarlysCatering event = new JCarlysCatering();
event.showCheckBoxGroupDemo();
}
public JCarlysCatering()
{
super(); //sets the title!
btnOK = new JButton("OK");
j1.add(t1);
j1.add(btnOK);
btnOKListener = new ButtonOKListener();
Frame1.add(j1);
btnOK.addActionListener(btnOKListener);
Frame1.setLayout(new FlowLayout());
Frame1.setSize(400,150);
Frame1.setVisible(true);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void showCheckBoxGroupDemo()
{
headerLabel.setText("Control in action: CheckBoxGroup");
CheckboxGroup fruitGroup = new CheckboxGroup();
Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);
statusLabel.setText("Apple Checkbox: checked");
chkApple.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Apple Checkbox: checked");
}
});
chkMango.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Mango Checkbox: checked");
}
});
chkPeer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Peer Checkbox: checked");
}
});
j1.add(chkApple);
j1.add(chkMango);
j1.add(chkPeer);
}
}
class ButtonOKListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
JOptionPane.showInputDialog("$35");
// TODO Auto-generated method stub
try
{
//int num1 = Integer.parseInt(t1.getText());
}
catch (NumberFormatException e)
{
//default title and icon
JOptionPane.showInputDialog(/*Frame1,*/"Eggs are not supposed to be green.");
}
}
}
Explanation / Answer
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JCarlysCatering extends JFrame
{
JButton btnOK;
ButtonOKListener btnOKListener;
JFrame Frame1 = new JFrame("Test Frame");
JPanel j1 = new JPanel (new FlowLayout());
JPanel mainPanel = new JPanel(new BorderLayout());
////Make new panel to hold checkboxes with orange colorcolor
CheckBoxPanel j2 = new CheckBoxPanel (Color.ORANGE);
////Make new panel for output with blue colorcolor
CheckBoxPanel output = new CheckBoxPanel (Color.CYAN);
Label outputLabel = new Label(" Output ");
JTextField t1 = new JTextField(20);
Label headerLabel = new Label();
Label statusLabel = new Label();
public static void main(String[] args)
{
JCarlysCatering event = new JCarlysCatering();
event.showCheckBoxGroupDemo();
}
public JCarlysCatering()
{
super(); //sets the title!
btnOK = new JButton("OK");
j1.add(t1);
j1.add(btnOK);
btnOKListener = new ButtonOKListener();
mainPanel.add(j1, BorderLayout.NORTH);
mainPanel.add(output, BorderLayout.SOUTH);
mainPanel.add(j2);
output.add(outputLabel);
Frame1.add(mainPanel);
btnOK.addActionListener(btnOKListener);
Frame1.setLayout(new FlowLayout());
Frame1.setSize(400,150);
Frame1.setVisible(true);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void showCheckBoxGroupDemo()
{
headerLabel.setText("Control in action: CheckBoxGroup");
CheckboxGroup fruitGroup = new CheckboxGroup();
Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);
statusLabel.setText("Apple Checkbox: checked");
chkApple.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Apple Checkbox: checked");
}
});
chkMango.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Mango Checkbox: checked");
}
});
chkPeer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Peer Checkbox: checked");
}
});
j2.add(chkApple);
j2.add(chkMango);
j2.add(chkPeer);
}
}
class CheckBoxPanel extends JPanel
{
CheckBoxPanel(Color c)
{
super();
this.setOpaque(true);
this.setBackground(c);
}
}
class ButtonOKListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
//JOptionPane.showInputDialog("$35");
// TODO Auto-generated method stub
try
{
//int num1 = Integer.parseInt(t1.getText());
}
catch (NumberFormatException e)
{
//default title and icon
JOptionPane.showInputDialog(/*Frame1,*/"Eggs are not supposed to be green.");
}
}
}