In Chapters 14 and 15, you developed an interactive GUI application for Carly\'s
ID: 3582841 • Letter: I
Question
In Chapters 14 and 15, you developed an interactive GUI application for Carly's Catering. Now, design a JPanel that uses graphics to display a logo for the company, and modify the GUI application to include it. Save the JPanel class as JCarlysLogoPanel.java, and save the GUI application as JCarlysCatering.java.
here is my code i am starting with
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.");
}
}
}
Explanation / Answer
package problem;
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JCarlysCatering extends JFrame {
JButton btnOK;
ButtonOKListener btnOKListener;
JFrame Frame1 = new JFrame("Test Frame");
JPanel j1 = new JPanel(new FlowLayout());
// new panel to hold company logo
JPanel logoPanel = 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 ");
// image component to add company logo
ImageIcon image = new ImageIcon("C:\Users ver38\Pictures\icon.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JTextField t1 = new JTextField(20);
Label headerLabel = new Label();
Label statusLabel = new Label();
public static void main(final String[] args) {
final JCarlysCatering event = new JCarlysCatering();
event.showCheckBoxGroupDemo();
}
public JCarlysCatering() {
super(); // sets the title!
btnOK = new JButton("OK");
logoPanel.add(label);
j1.add(t1);
j1.add(btnOK);
btnOKListener = new ButtonOKListener();
mainPanel.add(logoPanel, BorderLayout.PAGE_START);
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, 400);
Frame1.setVisible(true);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void showCheckBoxGroupDemo() {
headerLabel.setText("Control in action: CheckBoxGroup");
final CheckboxGroup fruitGroup = new CheckboxGroup();
final Checkbox chkApple = new Checkbox("Apple", fruitGroup, true);
final Checkbox chkMango = new Checkbox("Mango", fruitGroup, false);
final Checkbox chkPeer = new Checkbox("Peer", fruitGroup, false);
statusLabel.setText("Apple Checkbox: checked");
chkApple.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent e) {
statusLabel.setText("Apple Checkbox: checked");
}
});
chkMango.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent e) {
statusLabel.setText("Mango Checkbox: checked");
}
});
chkPeer.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent e) {
statusLabel.setText("Peer Checkbox: checked");
}
});
j2.add(chkApple);
j2.add(chkMango);
j2.add(chkPeer);
}
}
class CheckBoxPanel extends JPanel {
CheckBoxPanel(final Color c) {
super();
setOpaque(true);
setBackground(c);
}
}
class ButtonOKListener implements ActionListener {
public void actionPerformed(final ActionEvent arg0) {
// JOptionPane.showInputDialog("$35");
// TODO Auto-generated method stub
try {
// int num1 = Integer.parseInt(t1.getText());
} catch (final NumberFormatException e) {
// default title and icon
JOptionPane.showInputDialog(/* Frame1, */"Eggs are not supposed to be green.");
}
}
}
On Mon, Dec 19, 2016 at 1:18 PM, Karamjeet Kaur <karamjeet.k@hcl.com> wrote:
In Chapters 14 and 15, you developed an interactive GUI application for Carly's Catering. Now, design a JPanel that uses graphics to display a logo for the company, and modify the GUI application to include it. Save the JPanel class as JCarlysLogoPanel.java, and save the GUI application as JCarlysCatering.java.
here is my code i am starting with
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.");
}
}
}