Question
Please Provide me all necssary Screenshort, Commentds and make all class are separate with highlight:(I use eclipse) Goals : To solidify the concept of event
Please Provide me all necssary Screenshort, Commentds and make all class are separate with highlight:(I use eclipse)
Goals: To solidify the concept of event model and to offer the opportunity of writing GUI applications (you will be forced to read the given code in details). Particularly, you will learn Combobox, learn how to group radio buttons together using ButtonGroup, learn to use layout managers effectively, and learn to use Adapter class to handle the Frame close event.
Problem Statement:
This lab covers a lot of materials related to GUI event model and visual gadgets. You are given almost the complete code of a GUI program that allows the user to enter information about books. The GUI appearance is shown in Figure 1.
Figure 1: the layout of the required GUI window.
Here, we will describe the functionality of each gadget shown in Figure 1. The textfield next to Title offers a user to type in a book title. The book title will be used as input; the Combobox offers a foolproof way of letting the user to select the type of a book (in this lab, you will be asked to add 2 more entries into this gadget); the four radio buttons work together to let the user to rate the book by clicking a radio button (these buttons are grouped together. Thus, only one can be selected at any time); the three buttons have the following behaviors: button process---will get all the input information of a book, do some formatting, then, output the information to the text area that is located at the central part; button clear---will clear the text in the text field of the book title; button end---will end the program and close the main window (this button has the same effect when the user clicks the Close button on the title bar (the rightmost icon with X on it)).
Here are some highlights on the program:
a JComboBox is a gadget that offers a predefined items of selection. To add an item to the combobox, you can use something like:
JComboBox jCmb = new JComboBox();
jCmb.addItem(Novel);
To get a selected item, you can use the following:
String s = jCmb.getSelectedItem();
When a user selects one item from a combobox, it will generate two events:
ActionEvent (its corresponding method is actionPerformed()) and ItemEvent (its corresponding method is itemStateChanged()).
The line separator. In this lab, we will output formatted information about a book. This means that we need to insert new lines in the output string. One way of achieving this is to use platform independent line separator. This is achieved by using the System class as shown in the following:
String lineSeparator = System.getProperty(line.separator);
To help you finish the lab, we give you partial code as shown List 1 and List 2.
List 1: Code for class Lab 7 (an application class)
====================================================================
public class ICS141Lab7 {
public static void main(String[] s) {
new BookReview("ICS 141 Lab 7");
}
}
-----------------------------------------------------------------------------------------------------------------
List 2: partial code for class BookReview (a work class, there is some work to do for this class)
====================================================================
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class BookReview extends JFrame implements ActionListener {
private JLabel titleLabel;
private JTextField titleTxtFd;
private JComboBox typeCmb;
private ButtonGroup ratingGP;
private JButton processBnt;
private JButton endBnt;
private JButton clearBnt;
private JTextArea entriesTxtAr;
private JRadioButton excellentRdBnt;
private JRadioButton veryGoodRdBnt;
private JRadioButton fairRdBnt;
private JRadioButton poorRdBnt;
private String ratingString;
private final String EXCELLENT = "Excellent";
private final String VERYGOOD = "Very Good";
private final String FAIR = "Fair";
private final String POOR = "Poor";
String lineSeparator = System.getProperty("line.separator");
public BookReview(String name) {
super(name);
JPanel infoPanel = new JPanel();
titleLabel = new JLabel("Title: ");
infoPanel.add(titleLabel);
titleTxtFd = new JTextField(20);
infoPanel.add(titleTxtFd);
typeCmb = new JComboBox();
typeCmb.addItem("Novel");
typeCmb.addItem("Short Stories");
typeCmb.addItem("Essays");
typeCmb.addItem("Poems");
typeCmb.addItem("Biography");
// Add two new entries
infoPanel.add(typeCmb);
getContentPane().add(infoPanel, BorderLayout.NORTH);
JPanel ratingPanel = new JPanel();
ratingGP = new ButtonGroup();
excellentRdBnt = new JRadioButton("Excellent");
veryGoodRdBnt = new JRadioButton("Very Good");
fairRdBnt = new JRadioButton("Fair");
poorRdBnt = new JRadioButton("Poor");
ratingGP.add(excellentRdBnt);
ratingGP.add(veryGoodRdBnt);
ratingGP.add(fairRdBnt);
ratingGP.add(poorRdBnt);
ratingPanel.add(excellentRdBnt);
ratingPanel.add(veryGoodRdBnt);
ratingPanel.add(fairRdBnt);
ratingPanel.add(poorRdBnt);
getContentPane().add(ratingPanel, BorderLayout.WEST);
JPanel actionPanel = new JPanel(new GridLayout(1,0));
processBnt = new JButton("process");
endBnt = new JButton("end");
clearBnt = new JButton("clear");
actionPanel.add(processBnt);
actionPanel.add(clearBnt);
actionPanel.add(endBnt);
getContentPane().add(actionPanel, BorderLayout.EAST);
entriesTxtAr = new JTextArea(20, 20);
getContentPane().add(entriesTxtAr, BorderLayout.SOUTH);
pack();
setVisible(true);
processBnt.addActionListener(this);
clearBnt.addActionListener(this);
// hand write the code for registering the
//other 6 components
//in the space below. The 6 components are: endBnt,
//excellentRdBnt, veryGoodRdBnt, fairRdBnt, poorRdBnt,
//and typeCmb.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event) {
Object srcObj = event.getSource();
if (srcObj == clearBnt) {
titleTxtFd.setText("");
} else if (srcObj == endBnt) {
System.exit(0);
} else if (srcObj == processBnt) {
entriesTxtAr.append(lineSeparator +
titleTxtFd.getText() + " " +
(String) typeCmb.getSelectedItem()
+ " " + ratingString);
} else if (srcObj == excellentRdBnt) {
ratingString = EXCELLENT;
} else if (srcObj == veryGoodRdBnt) {
ratingString = VERYGOOD;
} else if (srcObj == fairRdBnt) {
ratingString = FAIR;
} else if (srcObj == poorRdBnt) {
ratingString = POOR;
}
}
}
-----------------------------------------------------------------------------------------------------------------
SCORE YOUR POINTS BY FINISHING FOLLOWING ACTIVITIES
In the space below, supply the missing code for registering events of the other 6 components endBnt, excellentRdBnt, veryGoodRdBnt, fairRdBnt, poorRdBnt, and typeCmb.
Answer:
Inspect the code. After you understood the layout structure that is implied by the code in List 1, 2 fill out the name and the layout manager of each panel in the dotted rectangle boxes.
Answer: (you can write your answer to the above diagram)
Inspect the code for actionPerformed() carefully, and write down the selection branch (two lines of code) in the space below that is responsible for exiting after a user clicking the end button.
Answer:
As indicated in the lecture, there are different ways of implementing an event handler. Inspect the code for BookReview() carefully, and answer the following question: when the Close button of the frame is pressed, a window close message is sent. In this case, which way of implementation is used (interface or adapter class)?
Answer:
In the actionPerformed() method, there is a selection branch that is responsible for extract the type of the book? Cut and paste the code of that branch (test condition and the body of the branch) in the space below.
Answer:
In the instance variable section of the class BookReview, there is a line that is responsible for returning the character combination that indicates a new line (in a robust way (means will work on different platforms). Cut and paste that line of code in the space below.
Answer:
Change code: Add two new categories for books to the JComboBox and name them Travelogue, Recipes. Cut and paste the code in the space below (just two lines you added).
Answer:
Change code: When the clear button is clicked, clear the text area as well. Cut and paste the code in the space below (just one line you added).
Answer:
Capture the output window. You should following sequence of actions:
Type Harry Potter in the book title, select Novel from the combobox, select Excellent from the radio button, press the process button.
Type World War II in the book title, select Biography from the combobox, select Fair from the radio button, press the process button.
Type Present Bush in the book title, select Travelogue from the combobox, select Poor from the radio button, press the process button.
Then, do a screen capture and paste it in the pace below:
Lab 5 Novel> Title: ?Excellent Very Good ?Fair O Poor | process clear end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started