Question
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
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.
c
====================================================================
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;
}
}
}
Lab 5 Title: Novel OExcellent Very Good Fair Poorprocess clear endStep 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