/* * Mizuki Kagaya * CS 161 Program 2 * * * Class: BookApplet * Functionality: get the data from an HTML file (initial values) and typing, * send them to a book object, and display them on the screen. * In addition, users can modify the stored data, by typing new ones and pressing the return key. */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class BookApplet extends Applet implements ActionListener { // make an object Book book1 = new Book(); // data private TextField titleText, publisherText, authorText, isbnText; private TextArea outputArea; private Label commentLabel, titleLabel, publisherLabel, authorLabel, isbnLabel; private String paramTitle, paramPublisher, paramAuthor, paramIsbn; // parameters to read values from HTML file. // methods /* * init() method: initialization of an applet. */ public void init() { // create instances and set other information of components. commentLabel = new Label(" Type some information about a book. "); titleLabel = new Label(" Book title "); // for input fields. titleText = new TextField(60); publisherLabel = new Label(" Publisher name "); publisherText = new TextField(60); authorLabel = new Label(" Author name "); authorText = new TextField(60); isbnLabel = new Label(" ISBN number "); isbnText = new TextField(60); outputArea = new TextArea(16, 60); // for an output field. outputArea.setEditable(false); // make the textarea noneditable. // read the initial values from an HTML file, and send them to a book object. book1.setTitle(paramTitle = getParameter("title")); book1.setPublisher(paramPublisher = getParameter("publisher")); book1.setAuthor(paramAuthor = getParameter("author")); book1.setISBN(paramIsbn = getParameter("isbn")); // display components (labels, textfields, button, textarea, and action). add(commentLabel); add(titleLabel); add(titleText); add(publisherLabel); add(publisherText); add(authorLabel); add(authorText); add(isbnLabel); add(isbnText); add(outputArea); // add ActionListener on text fields. titleText.addActionListener(this); publisherText.addActionListener(this); authorText.addActionListener(this); isbnText.addActionListener(this); // display the data from HTML inside each textfield. titleText.setText(paramTitle); publisherText.setText(paramPublisher); authorText.setText(paramAuthor); isbnText.setText(paramIsbn); // (display the initial output to check if data from HTML file were stored in memory(book object)) outputArea.setText("The information of the book is:\n\n\tTitle: " + book1.getTitle() + "\n\tPublisher: " + book1.getPublisher() + "\n\tAuthor: " + book1.getAuthor() + "\n\tISBN: " + book1.getISBN() + "\n\nThis is the all informaion about the book.\n"); } // init() /* * actionPerformed() method: performs actions when the action call occurs. */ public void actionPerformed(ActionEvent e) { // When a return key is pressed in a textfield (input field), data are stored at a book object. if (e.getSource() == titleText || e.getSource() == publisherText || e.getSource() == authorText || e.getSource() == isbnText) { book1.setTitle(titleText.getText()); book1.setPublisher(publisherText.getText()); book1.setAuthor(authorText.getText()); book1.setISBN(isbnText.getText()); } // display the output of information about a book. outputArea.setText("The information of the book is:\n\n\tTitle: " + book1.getTitle() + "\n\tPublisher: " + book1.getPublisher() + "\n\tAuthor: " + book1.getAuthor() + "\n\tISBN: " + book1.getISBN() + "\n\nThis is the all informaion about the book.\n"); } // actionPerformed() } // end of class BookApplet