Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7-2 (Book Chapter 14)

Similar presentations


Presentation on theme: "Chapter 7-2 (Book Chapter 14)"— Presentation transcript:

1 Chapter 7-2 (Book Chapter 14)
Introduction to OOP with Java 4th Ed, C. Thomas Wu Chapter 7-2 (Book Chapter 14) GUI and Event-Driven Programming ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. © The McGraw-Hill Companies, Inc.

2 GUI Classes for Handling Text
Intro to OOP with Java, C. Thomas Wu GUI Classes for Handling Text The Swing GUI classes JLabel, JTextField, and JTextArea deal with text. A JLabel object displays uneditable text (or image). A JTextField object allows the user to enter a single line of text. A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text. Notice that JLabel objects are not limited to text. Using them is actually the easiest and quickest way to display an image. JTextArea has dual pursposes. It can be used to display noneditable multi-line text, similar to using multiple JLabel objects. It can also be used to allow the user to enter multiple lines of text, similar to using multiple JTextField objects. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

3 Intro to OOP with Java, C. Thomas Wu
JTextField We use a JTextField object to accept a single line to text from a user. An action event is generated when the user presses the ENTER key. The getText method of JTextField is used to retrieve the text that the user entered. JTextField input = new JTextField( ); contentPane.add(input); The getText method is most commonly used inside the actionPerformed method of an action listener. input.addActionListener(eventListener); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

4 JTextField We want to write a program that changes frame title to the text of the clicked button (OK or CANCEL), or the text entered in the text field ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

5 JTextField import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14TextFrame1 extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private JButton cancelButton; private JButton okButton; private JTextField inputLine; public static void main(String[] args) { Ch14TextFrame1 frame = new Ch14TextFrame1(); frame.setVisible(true); } public Ch14TextFrame1() { Container contentPane; setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Program Ch14TextFrame1"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane( ); contentPane.setLayout(new FlowLayout()); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

6 JTextField inputLine = new JTextField();
inputLine.setColumns(22); inputLine.setFont(new Font("Courier", Font.PLAIN, 14)); contentPane.add(inputLine); inputLine.addActionListener(this); okButton = new JButton("OK"); contentPane.add(okButton); cancelButton = new JButton("CANCEL"); contentPane.add(cancelButton); //register this frame as an action listener of the two buttons cancelButton.addActionListener(this); okButton.addActionListener(this); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

7 JTextField public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle("You clicked " + buttonText); } else { //the event source is inputLine setTitle("You entered '" + inputLine.getText() + "'"); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

8 Intro to OOP with Java, C. Thomas Wu
JLabel We use a JLabel object to display a label. A label can be a text or an image. When creating an image label, we pass ImageIcon object instead of a string. JLabel textLabel = new JLabel(); textLabel.setText("Please enter your name"); textLabel.setSize(150,25); contentPane.add(textLabel); //we can also set the text at the time of object creation. JLabel Prompt = new JLabel ("Please enter your age"); ... A JLabel is strictly for displaying noneditable text or image. A JLabel object does not generate any events. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

9 Intro to OOP with Java, C. Thomas Wu
JLabel with an Image JLabel imgLabel = new JLabel(new ImageIcon("cat.gif")); contentPane.add(imgLabel); JLabel (with a text) JLabel (with an image) JTextField The sample Ch7TextFrame2 class includes five GUI components: two JLabel objects, one JTextField object, and two JButton objects. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

10 Intro to OOP with Java, C. Thomas Wu
JTextArea We use a JTextArea object to display or allow the user to enter multiple lines of text. The setText method assigns the text to a JTextArea, replacing the current content. The append method appends the text to the current text. JTextArea textArea = new JTextArea( ); . . . textArea.setText("Hello\n"); textArea.append("the lost "); textArea.append("world"); Hello the lost world Notice how the append method works. If you want to place the text to be appended on a new line, you have ouput the newline control character \n at the appropriate point. JTextArea ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

11 JTextArea We want to create a frame with a JTextField and a JTextArea
When the user enters a text in the JTextField, and then presses Enter or clicks the ADD button,the entered text will be appended to the JTextArea When the user clicks CLEAR the text in the JTextArea will be cleared JTextArea JTextField ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

12 JTextArea import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch14TextFrame3 extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 250; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; private static final String EMPTY_STRING = ""; private static final String NEWLINE = "\n"; private JButton clearButton; private JButton addButton; private JTextField inputLine; private JTextArea textArea; public static void main(String[] args) { Ch14TextFrame3 frame = new Ch14TextFrame3(); frame.setVisible(true); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

13 JTextArea public Ch14TextFrame3() { Container contentPane; setSize (FRAME_WIDTH, FRAME_HEIGHT); setResizable (false); setTitle ("Program Ch14TextFrame3"); setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane = getContentPane( ); contentPane.setLayout(new FlowLayout()); textArea = new JTextArea(); textArea.setColumns(22); textArea.setRows(8); textArea.setBorder(BorderFactory.createLineBorder(Color.red)); textArea.setEditable(false); contentPane.add(textArea); ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

14 JTextArea inputLine = new JTextField(); inputLine.setColumns(22); contentPane.add(inputLine); inputLine.addActionListener(this); //create and place two buttons on the frame addButton = new JButton("ADD"); contentPane.add(addButton); addButton.addActionListener(this); clearButton = new JButton("CLEAR"); contentPane.add(clearButton); clearButton.addActionListener(this); //register 'Exit upon closing' as a default close operation setDefaultCloseOperation( EXIT_ON_CLOSE ); } ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

15 JTextArea public void actionPerformed(ActionEvent event) {
if (event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); if (clickedButton == addButton) { addText(inputLine.getText()); } else { clearText( ); } } else { //the event source is inputLine addText(inputLine.getText()); } } // private void addText(String userInput) { textArea.append(userInput + NEWLINE); inputLine.setText(EMPTY_STRING); } // private void clearText( ) { textArea.setText(EMPTY_STRING); inputLine.setText(EMPTY_STRING); } }

16 Intro to OOP with Java, C. Thomas Wu
Ch14TextFrame3 The state of a Ch14TextFrame3 window after six words are entered. Run the program and explore it. This frame has one JTextField object and one JTextArea object. Every time a text is entered in the text field and the ENTER key is pressed or the ADD button is clicked, the entered text is append to the text area. The border of red color is adorned to the text area to demarcate the region. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

17 Adding Scroll Bars to JTextArea
Intro to OOP with Java, C. Thomas Wu Adding Scroll Bars to JTextArea By default a JTextArea does not have any scroll bars. To add scroll bars, we place a JTextArea in a JScrollPane object. JTextArea textArea = new JTextArea(); . . . JScrollPane scrollText = new JScrollPane(textArea); contentPane.add(scrollText); Here's a sample to wrap the text area with scroll bars. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.

18 Ch14TextFrame3 with Scroll Bars
Intro to OOP with Java, C. Thomas Wu Ch14TextFrame3 with Scroll Bars A sample Ch14TextFrame3 window when a JScrollPane is used. Initially, there will be no scroll bars. Run the program and confirm this behavior. When the displayed text goes beyond the horizontal or vertical boundary, the corresponding scroll bar appears. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ©The McGraw-Hill Companies, Inc.


Download ppt "Chapter 7-2 (Book Chapter 14)"

Similar presentations


Ads by Google