1. Write an application that displays a JFrame containing the opening sentence or two from your favorite book. Save the file as JBookQuote.java.

      import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JBookQuote extends JFrame { FlowLayout flow = new FlowLayout(); JLabel msg1 = new JLabel("To begin with,"); JLabel msg2 = new JLabel("Marley was dead."); public JBookQuote() { super("Book Quote"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(msg1); add(msg2); } public static void main(String[] args) { JBookQuote aFrame = new JBookQuote(); aFrame.setSize(300, 150); aFrame.setVisible(true); } }
    2. Add a button to the frame in the JBookQuote program. When the user clicks the button, display the title of the book that contains the quote. Save the file as JBookQuote2.java.

      import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JBookQuote2 extends JFrame implements ActionListener { FlowLayout flow = new FlowLayout(); JLabel msg1 = new JLabel("To begin with,"); JLabel msg2 = new JLabel("Marley was dead."); JButton button = new JButton("Click for source"); JLabel msg3 = new JLabel("** A Christmas Carol **"); public JBookQuote2() { super("Book Quote 2"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(msg1); add(msg2); add(button); button.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { add(msg3); validate(); repaint(); } public static void main(String[] args) { JBookQuote2 aFrame = new JBookQuote2(); aFrame.setSize(260, 150); aFrame.setVisible(true); } }
    1. Write an application that instantiates a JFrame that contains a JButton. Disable the JButton after the user clicks it. Save the file as JFrameDisableButton.java.

      import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JFrameDisableButton extends JFrame implements ActionListener { final int SIZE = 180; Container con = getContentPane(); JButton button = new JButton("Press Me"); public JFrameDisableButton() { super("Frame"); con.setLayout(new FlowLayout()); setSize(SIZE, SIZE); setVisible(true); con.add(button); button.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { button.setEnabled(false); } public static void main(String[] args) { JFrameDisableButton frame = new JFrameDisableButton(); } }
    2. Modify the JFrameDisableButton program so that the JButton is not disabled until the user has clicked at least eight times. At that point, display a JLabel that indicates “That’s enough!” Save the file as JFrameDisableButton2.java.

      import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JFrameDisableButton2 extends JFrame implements ActionListener { final int SIZE = 180; Container con = getContentPane(); JButton button = new JButton("Press Me"); int count = 0; final int MAX = 8; JLabel label = new JLabel("That's enough!"); public JFrameDisableButton2() { super("Frame"); con.setLayout(new FlowLayout()); setSize(SIZE, SIZE); setVisible(true); con.add(button); con.add(label); label.setVisible(false); button.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { ++count; if(count == MAX) { button.setEnabled(false); label.setVisible(true); } } public static void main(String[] args) { JFrameDisableButton2 frame = new JFrameDisableButton2(); } }
  1. Create an application with a JFrame and at least six labels that contain facts about your favorite topic—for example, the French Revolution or astronomy. Every time the user clicks a JButton, remove one of the labels and add a different one. Save the file as JFacts.java.

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JFacts extends JFrame implements ActionListener { FlowLayout flow = new FlowLayout(); JButton b = new JButton("Press to change fact"); JLabel stars = new JLabel("**********************************************"); JLabel f1 = new JLabel("Only seven prisoners were in the Bastille when it was stormed. "); JLabel f2 = new JLabel("A Tale of Two Cities is a novel set during the French Revolution. "); JLabel f3 = new JLabel("Perhaps 40,000 people were executed at the guillotine. "); JLabel f4 = new JLabel("A loaf of bread cost a weeks wages prior to the French Revolution. "); JLabel f5 = new JLabel("Thomas Jefferson was the U.S. Minister to France during the Revolution. "); JLabel f6 = new JLabel("Protestant and Jewish religions were illegal in France before 1879. "); int counter = 0; public JFacts() { super("Facts"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(b); add(stars); add(f1); b.addActionListener(this); } public static void main(String[] args) { JFacts rFrame = new JFacts(); rFrame.setSize(440, 100); rFrame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { ++counter; if(counter == 6) counter = 0; if(counter == 1) { remove(f1); add(f2); } else if(counter == 2) { remove(f2); add(f3); } else if(counter == 3) { remove(f3); add(f4); } else if(counter == 4) { remove(f4); add(f5); } else if(counter == 5) { remove(f5); add(f6); } else { remove(f6); add(f1); } validate(); repaint(); } }
  2. Write an application for Lambert’s Vacation Rentals. Use separate ButtonGroups to allow a client to select one of three locations, the number of bedrooms, and whether meals are included in the rental. Assume that the locations are parkside for $600 per week, poolside for $750 per week, or lakeside for $825 per week. Assume that the rentals have one, two, or three bedrooms and that each bedroom greater than one adds $75 to the base price. Assume that if meals are added, the price is $200 more per rental. Save the file as JVacationRental.java.

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JVacationRental extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); String companyName = new String("Lambert's Vacation Rentals"); Font bigFont = new Font("Arial", Font.PLAIN, 24); final int PARKPRICE = 600, POOLPRICE = 750, LAKEPRICE = 825; int totalPrice = 0; ButtonGroup locationGrp = new ButtonGroup(); JCheckBox parkBox = new JCheckBox("Parkside",false); JCheckBox poolBox = new JCheckBox("Poolside", false); JCheckBox lakeBox = new JCheckBox("Lakeside", false); final int ONEPRICE = 0, TWOPRICE = 75, THREEPRICE = 150; ButtonGroup bdrmGrp = new ButtonGroup(); JCheckBox oneBox = new JCheckBox("1 bedroom", false); JCheckBox twoBox = new JCheckBox("2 bedrooms", false); JCheckBox threeBox = new JCheckBox("3 bedrooms", false); final int NOMEALSPRICE = 0, MEALSPRICE = 200; ButtonGroup mealGrp = new ButtonGroup(); JCheckBox noMealsBox = new JCheckBox("No meals",false); JCheckBox mealsBox = new JCheckBox("Meals included",false); JTextField totPrice = new JTextField(10); String output; public JVacationRental() { super("Vacation rentals"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); locationGrp.add(parkBox); parkBox.addItemListener(this); locationGrp.add(poolBox); poolBox.addItemListener(this); locationGrp.add(lakeBox); lakeBox.addItemListener(this); bdrmGrp.add(oneBox); twoBox.addItemListener(this); bdrmGrp.add(twoBox); threeBox.addItemListener(this); bdrmGrp.add(threeBox); mealGrp.add(noMealsBox); noMealsBox.addItemListener(this); mealGrp.add(mealsBox); mealsBox.addItemListener(this); add(parkBox); add(poolBox); add(lakeBox); add(oneBox); add(twoBox); add(threeBox); add(noMealsBox); add(mealsBox); totPrice.setText("0"); add(totPrice); } public static void main(String[] arguments) { JVacationRental aFrame = new JVacationRental(); aFrame.setSize(320,200); aFrame.setVisible(true); } @Override public void itemStateChanged(ItemEvent check) { Object source = check.getItem(); if(source == parkBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += PARKPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= PARKPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == poolBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += POOLPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= POOLPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == lakeBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += LAKEPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= LAKEPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == oneBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += ONEPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= ONEPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == twoBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += TWOPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= TWOPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == threeBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += THREEPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= THREEPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == noMealsBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += NOMEALSPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= NOMEALSPRICE; output = " " + totalPrice; totPrice.setText(output); } else if(source == mealsBox) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) totalPrice += MEALSPRICE; else if(select == ItemEvent.DESELECTED) totalPrice -= MEALSPRICE; output = " " + totalPrice; totPrice.setText(output); } } }
    1. Write an application that allows a user to select one of at least five television shows to watch on demand. When the user selects a show, display a brief synopsis. Save the file as JTVDownload.java.

      import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JTVDownload extends JFrame implements ItemListener { JComboBox programBox = new JComboBox(); JLabel programList = new JLabel("*** Program List ***"); JLabel stars = new JLabel("*****************************"); JTextField descripField = new JTextField(20); String[] progs = {"Breaking Bad", "Orange is the New Black", "Twilight Zone", "Parks and Recreation", "Everybody Loves Raymond"}; String[] descrips = {"", "Teacher becomes meth dealer", "Life in women's prison", "Classic science fiction episodes", "Comedy in local government office", "Family comedy with meddling in-laws"}; public JTVDownload() { super("JTVDownload"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); programBox.addItemListener(this); add(stars); add(programList); programBox.addItem("none"); for(int x = 0; x < progs.length; ++x) programBox.addItem(progs[x]); add(programBox); add(descripField); } public static void main(String[] arguments) { JTVDownload frame = new JTVDownload(); frame.setSize(300,150); frame.setVisible(true); } @Override public void itemStateChanged(ItemEvent list) { Object source = list.getSource(); if(source == programBox) { int num = programBox.getSelectedIndex(); descripField.setText(descrips[num]); } } }
    2. Change the JTVDownload application to include an editable combo box. Allow the user to type the name of a television show and display an appropriate error message if the desired show is not available. Save the file as JTVDownload2.java.

      import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JTVDownload2 extends JFrame implements ItemListener { JComboBox programBox = new JComboBox(); JLabel programList = new JLabel("*** Program List ***"); JLabel stars = new JLabel("*****************************"); JTextField descripField = new JTextField(20); String[] progs = {"Breaking Bad", "Orange is the New Black", "Twilight Zone", "Parks and Recreation", "Everybody Loves Raymond"}; String[] descrips = {"", "Teacher becomes meth dealer", "Life in women's prison", "Classic science fiction episodes", "Comedy in local government office", "Family comedy with meddling in-laws"}; public JTVDownload2() { super("JTVDownload"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); programBox.addItemListener(this); add(stars); add(programList); programBox.addItem("none"); for(int x = 0; x < progs.length; ++x) programBox.addItem(progs[x]); add(programBox); programBox.setEditable(true); add(descripField); } public static void main(String[] arguments) { JTVDownload2 frame = new JTVDownload2(); frame.setSize(350,150); frame.setVisible(true); } @Override public void itemStateChanged(ItemEvent list) { Object source = list.getSource(); if(source == programBox) { int num = programBox.getSelectedIndex(); try { descripField.setText(descrips[num]); } catch(ArrayIndexOutOfBoundsException e) { descripField.setText("Sorry - request not recognized"); } } } }
  3. Design an application for the Sublime Sandwich Shop. The user makes sandwich order choices from list boxes, and the application displays the price. The user can choose from three main sandwich ingredients of your choice (for example, chicken) at three different prices. The user also can choose from three different bread types (for example, rye) from a list of at least three options. Save the file as JSandwich.java.

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JSandwich extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); JLabel companyName = new JLabel("Sublime Sandwich"); JComboBox mainBox = new JComboBox(); JLabel mainList = new JLabel("Main ingredient"); JComboBox breadBox = new JComboBox(); JLabel breadList = new JLabel("Breads"); JTextField totPrice = new JTextField(10); int mainNum, breadNum; double[] mainPrices = {6.99, 7.99, 8.99}; double sumPrice = mainPrices[0]; double breadPrice = 0; double mainPrice = mainPrices[0]; double[] breadPrices = {0, 1, 1.5}; String output; public JSandwich() { super("Sublime Sandwich Shop"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(companyName); companyName.setFont(new Font("Ariel", Font.BOLD, 20)); mainBox.addItem("Chicken"); mainBox.addItem("Tuna"); mainBox.addItem("Roast beef"); breadBox.addItem("White"); breadBox.addItem("Rye"); breadBox.addItem("Whole wheat"); add(mainList); add(mainBox); mainBox.addItemListener(this); add(breadList); add(breadBox); breadBox.addItemListener(this); output = "Total Price $" + sumPrice; totPrice.setText(output); add(totPrice); } public static void main(String[] arguments) { JSandwich sandframe = new JSandwich(); sandframe.setSize(240,200); sandframe.setVisible(true); } @Override public void itemStateChanged(ItemEvent list) { Object source = list.getSource(); if (source == mainBox) { mainNum = mainBox.getSelectedIndex(); mainPrice = mainPrices[mainNum]; sumPrice = mainPrice + breadPrice; output = "Total Price $" + sumPrice; totPrice.setText(output); } else if (source == breadBox) { breadNum = breadBox.getSelectedIndex(); breadPrice = breadPrices[breadNum]; sumPrice = mainPrice + breadPrice; output = "Total Price $" + sumPrice; totPrice.setText(output); } } }
  4. Write an application that allows a user to select a country from a list box that contains at least seven options. After the user makes a selection, display the country’s capital city. Save the file as JCapitals.java.

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JCapitals extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); JLabel countryLabel = new JLabel("Select a country"); JComboBox countryBox = new JComboBox(); JTextField capitalField = new JTextField(25); String[] countries = {"Austria", "Canada", "England", "France", "Italy", "Mexico", "Spain"}; String[] capitals = {"Vienna", "Toronto", "London", "Paris", "Rome", "Mexico City", "Madrid"}; String myCountry; String output; int countryNum; public JCapitals() { super("Capitals"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); countryBox.addItemListener(this); for(int x = 0; x < countries.length; ++x) countryBox.addItem(countries[x]); add(countryLabel); add(countryBox); add(capitalField); } public static void main(String[] arguments) { JCapitals cframe = new JCapitals(); cframe.setSize(400, 150); cframe.setVisible(true); } @Override public void itemStateChanged(ItemEvent list) { Object source = list.getSource(); if(source == countryBox) { countryNum = countryBox.getSelectedIndex(); myCountry = countries[countryNum]; output = "The capital of " + myCountry + " is " + capitals[countryNum]; capitalField.setText(output); } } }
  5. Write an application that allows the user to choose insurance options in JCheckBoxes. Use a ButtonGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use regular (single) JCheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the PPO costs $600 per month, the dental coverage adds $75 per month, and the vision care adds $20 per month. When a user deselects an item, make the text field blank. Save the file as JInsurance.java.

    import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JInsurance extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); ButtonGroup insGrp = new ButtonGroup(); JCheckBox hmo = new JCheckBox("HMO", false); JCheckBox ppo = new JCheckBox("PPO", false); JCheckBox dental = new JCheckBox("Dental", false); JCheckBox vision = new JCheckBox("Vision", false); JTextField insChoice = new JTextField(20); String output, insChosen; public JInsurance() { super("Check Box"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); insGrp.add(hmo); insGrp.add(ppo); hmo.addItemListener(this); ppo.addItemListener(this); dental.addItemListener(this); vision.addItemListener(this); add(hmo); add(ppo); add(dental); add(vision); add(insChoice); } public static void main(String[] arguments) { JInsurance iFrame = new JInsurance(); iFrame.setSize(400,100); iFrame.setVisible(true); } @Override public void itemStateChanged(ItemEvent check) { int HMOPRICE = 200; int PPOPRICE = 600; int DENTALPRICE = 75; int VISIONPRICE = 20; Object source = check.getItem(); if(source == hmo) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) insChosen = "hmo $" + HMOPRICE; else if(select == ItemEvent.DESELECTED) insChosen = ""; output = " " + insChosen; insChoice.setText(output); } if(source == ppo) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) insChosen = "ppo $" + PPOPRICE; else if(select == ItemEvent.DESELECTED) insChosen = ""; output = " " + insChosen; insChoice.setText(output); } if(source == dental) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) insChosen = "dental $" + DENTALPRICE + " additional"; else if(select == ItemEvent.DESELECTED) insChosen = ""; output = " " + insChosen; insChoice.setText(output); } if(source == vision) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) insChosen = "vision $" + VISIONPRICE + " additional"; else if(select == ItemEvent.DESELECTED) insChosen = ""; output = " " + insChosen; insChoice.setText(output); } } }
    1. Search the Java website for information about how to use a JTextArea, its constructors, and its setText() and append() methods. Write an application that allows the user to select options for a dormitory room. Use JCheckBoxes for options such as private room, Internet connection, cable TV connection, microwave, refrigerator, and so on. When the application starts, use a text area to display a message listing the options that are not yet selected. As the user selects and deselects options, add appropriate messages to the common text area so it accumulates a running list that reflects the user’s choices. Save the file as JDorm.java

      import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JDorm extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); JCheckBox privateRm = new JCheckBox("Private"); JCheckBox internet = new JCheckBox("Internet"); JCheckBox cable = new JCheckBox("Cable TV"); JCheckBox microwave = new JCheckBox("Microwave"); JCheckBox refrigerator = new JCheckBox("Refrigerator"); JTextArea outArea = new JTextArea(5, 10); String dormOption; String output; public JDorm() { super("Dorm Room Options"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(privateRm); privateRm.addItemListener(this); add(internet); internet.addItemListener(this); add(cable); cable.addItemListener(this); add(microwave); microwave.addItemListener(this); add(refrigerator); refrigerator.addItemListener(this); outArea.append("Shared room\nNo cable\nNo Internet"); outArea.append("\nNo microwave\nNo refrigerator"); add(outArea); } public static void main(String[] arguments) { JDorm dFrame = new JDorm(); dFrame.setSize(450,300); dFrame.setVisible(true); } @Override public void itemStateChanged(ItemEvent check) { Object source = check.getItem(); if(source == privateRm) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption = "\nPrivate Room"; else dormOption = "\nShared room"; } if(source == cable) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption = "\nCable TV"; else dormOption = "\nNo cable"; } if(source == internet) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption = "\nInternet Connection"; else dormOption = "\nNo internet"; } if(source == microwave) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption = "\nMicrowave"; else dormOption = "\nNo micowave"; } if(source == refrigerator) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption = "\nRefrigerator"; else dormOption = "\nNo refrigerator"; } output = " " + dormOption; outArea.append(output); } }
    2. Modify the JDorm application so that instead of a running list of the user’s choices, the application displays only the current choices. Save the file as JDorm2.java.

      import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JDorm2 extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); JCheckBox privateRm = new JCheckBox("Private"); JCheckBox internet = new JCheckBox("Internet"); JCheckBox cable = new JCheckBox("Cable TV"); JCheckBox microwave = new JCheckBox("Microwave"); JCheckBox refrigerator = new JCheckBox("Refrigerator"); JTextArea outArea = new JTextArea(5, 10); String[] dormOption = {"\nShared room", "\nNo Cable", "\nNo Internet", "\nNo microwave", "\nNo refrigerator"}; String output; int x; public JDorm2() { super("Dorm Room Options"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); add(privateRm); privateRm.addItemListener(this); add(internet); internet.addItemListener(this); add(cable); cable.addItemListener(this); add(microwave); microwave.addItemListener(this); add(refrigerator); refrigerator.addItemListener(this); for(x = 0; x < dormOption.length; ++x) outArea.append(dormOption[x]); add(outArea); } public static void main(String[] arguments) { JDorm2 dFrame = new JDorm2(); dFrame.setSize(450,300); dFrame.setVisible(true); } @Override public void itemStateChanged(ItemEvent check) { Object source = check.getItem(); if(source == privateRm) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption[0] = "\nPrivate room"; else dormOption[0] = "\nShared room"; } if(source == cable) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption[1] = "\nCable TV"; else dormOption[1] = "\nNo cable"; } if(source == internet) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption[2] = "\nInternet Connection"; else dormOption[2] = "\nNo internet"; } if(source == microwave) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption[3] = "\nMicrowave"; else dormOption[3] = "\nNo micowave"; } if(source == refrigerator) { int select = check.getStateChange(); if(select == ItemEvent.SELECTED) dormOption[4] = "\nRefrigerator"; else dormOption[4] = "\nNo refrigerator"; } // for(x = 0; x < dormOption.length; ++x) // outArea.remove(dormOption[x]); outArea.setText(dormOption[0]); for(x = 1; x < dormOption.length; ++x) outArea.append(dormOption[x]); } }
  6. Create an application for Paula’s Portraits, a photography studio. The application allows users to compute the price of a photography session. Paula’s base price is $40 for an in-studio photo session with one person. The in-studio fee is $75 for a session with two or more subjects, and $95 for a session with a pet. A $90 fee is added to take photos on location instead of in the studio. Include a set of mutually exclusive check boxes to select the portrait subject and another set for the session location. Include labels as appropriate to explain the application’s functionality. Save the file as JPhotoFrame.java.

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JPhotoFrame extends JFrame implements ItemListener { FlowLayout flow = new FlowLayout(); JCheckBox locBox = new JCheckBox("On location",false); JCheckBox studioBox = new JCheckBox("In studio", false); JCheckBox oneBox = new JCheckBox("One human subject ", false); JCheckBox moreBox = new JCheckBox("Two or more human subjects ", false); JCheckBox petBox = new JCheckBox("Pet subject ", false); JLabel mainLabel = new JLabel("Paula's Portaits"); Font font = new Font("Ariel",Font.ITALIC, 30); JLabel label2 = new JLabel("Portait fee"); JLabel label1 = new JLabel("Select options"); JTextField totPrice = new JTextField(10); String output; final double LOC_PRICE = 90.00; double ONE_HUMAN_SUBJECT_PRICE = 40.00, MORE_HUMAN_SUBJECTS_PRICE = 75.00; double PET_SUBJECT_PRICE = 95.00; double locPrice = 0.00; double subPrice = 0.0; public JPhotoFrame() { super("Portait options selection"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(flow); ButtonGroup subjectsGroup = new ButtonGroup(); subjectsGroup.add(oneBox); subjectsGroup.add(moreBox); subjectsGroup.add(petBox); add(label1); add(oneBox); add(moreBox); add(petBox); ButtonGroup locGroup = new ButtonGroup(); locGroup.add(locBox); locGroup.add(studioBox); add(locBox); add(studioBox); mainLabel.setFont(font); add(mainLabel); add(label2); add(totPrice); totPrice.setText("$0"); oneBox.addItemListener(this); moreBox.addItemListener(this); petBox.addItemListener(this); locBox.addItemListener(this); studioBox.addItemListener(this); } @Override public void itemStateChanged(ItemEvent check) { Object source = check.getItem(); int select; select = check.getStateChange(); if(source == locBox) { if(select == ItemEvent.SELECTED) locPrice = locPrice + LOC_PRICE; else locPrice = locPrice - LOC_PRICE; } else if(source == oneBox) { if(select == ItemEvent.SELECTED) subPrice = subPrice + ONE_HUMAN_SUBJECT_PRICE; else subPrice = subPrice - ONE_HUMAN_SUBJECT_PRICE; } else if(source == moreBox) { if(select == ItemEvent.SELECTED) subPrice = subPrice + MORE_HUMAN_SUBJECTS_PRICE; else subPrice = subPrice - MORE_HUMAN_SUBJECTS_PRICE; } else { if(source == petBox) { if(select == ItemEvent.SELECTED) subPrice = subPrice + PET_SUBJECT_PRICE; else subPrice = subPrice - PET_SUBJECT_PRICE; } } output = " " + (subPrice + locPrice); totPrice.setText(output); } public static void main(String[] args) { JPhotoFrame frame = new JPhotoFrame(); frame.setSize(320, 250); frame.setVisible(true); } }