Event Handling and GUI Components Big Java by
Event Handling and GUI Components Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Graphical User Interfaces • A Graphical User Interface (GUI) is created with at least three kinds of objects – components – events – listeners • A GUI component defines a screen element to display information or allow the user to interact with the program – buttons, text fields, labels, menus, etc. • A container is a special component that holds and organizes other components – dialog boxes, applets, frames, panels, etc. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Events • An event is an object that represents some activity to which we may want to respond • For example, we may want our program to perform some action when the following occurs: – – – the mouse is moved a mouse button is clicked the mouse is dragged a graphical button is clicked a keyboard key is pressed a timer expires • Events often correspond to user actions, but not always Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Events and Listeners Event Component Listener This object may generate an event This object waits for and responds to an event When an event occurs, the generator calls the appropriate method of the listener, passing an object that describes the event Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Events, Event Sources, and Event Listeners • User interface events include key presses, mouse moves, button clicks, and so on • Most programs don't want to be flooded by boring events • A program can indicate that it only cares about certain specific events • Event listener: • Notified when event happens • Belongs to a class that is provided by the application programmer • Its methods describe the actions to be taken when an event occurs • Event source: • Event sources report on events • When an event occurs, the event source notifies all event listeners Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Events, Event Sources, and Event Listeners • Example: Use JButton components for buttons; attach an Action. Listener to each button • Action. Listener interface: public interface Action. Listener { void action. Performed(Action. Event event); } • Need to supply a class whose action. Performed method contains instructions to be executed when button is clicked • event parameter contains details about the event, such as the time at which it occurred Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Events, Event Sources, and Event Listeners (cont. ) • Construct an object of the listener and add it to the button: Action. Listener listener = new Click. Listener(); button. add. Action. Listener(listener); Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Click. Listener. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: import java. awt. event. Action. Event; import java. awt. event. Action. Listener; /** An action listener that prints a message. */ public class Click. Listener implements Action. Listener { public void action. Performed(Action. Event event) { System. out. println("I was clicked. "); } } Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Button. Viewer. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: import java. awt. event. Action. Listener; import javax. swing. JButton; import javax. swing. JFrame; /** This program demonstrates how to install an action listener. */ public class Button. Viewer { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame. add(button); Action. Listener listener = new Click. Listener(); button. add. Action. Listener(listener); frame. set. Size(FRAME_WIDTH, FRAME_HEIGHT); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); } Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Button. Viewer. java (cont. ) 23: 24: 25: 26: } private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; Output: Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check Which objects are the event source and the event listener in the Button. Viewer program? Answer: The button object is the event source. The listener object is the event listener. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Using Inner Classes for Listeners • Implement simple listener classes as inner classes like this: JButton button = new JButton(". . . "); // This inner class is declared in the same method as the button variable class My. Listener implements Action. Listener {. . . }; Action. Listener listener = new My. Listener(); button. add. Action. Listener(listener); • This places the trivial listener class exactly where it is needed, without cluttering up the remainder of the project • Methods of an inner class can access local variables from surrounding blocks and fields from surrounding classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Using Inner Classes for Listeners • Local variables that are accessed by an inner class method must be declared as final • Example: add interest to a bank account whenever a button is clicked: JButton button = new JButton("Add Interest"); final Bank. Account account = new Bank. Account(INITIAL_BALANCE); // This inner class is declared in the same method as the account // and button variables. class Add. Interest. Listener implements Action. Listener { Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Using Inner Classes for Listeners (cont. ) public void action. Performed(Action. Event event) { // The listener method accesses the account variable // from the surrounding block double interest = account. get. Balance() * INTEREST_RATE / 100; account. deposit(interest); } }; Action. Listener listener = new Add. Interest. Listener(); button. add. Action. Listener(listener); Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 1. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: import java. awt. event. Action. Event; java. awt. event. Action. Listener; javax. swing. JButton; javax. swing. JFrame; /** This program demonstrates how an action listener can access a variable from a surrounding block. */ public class Investment. Viewer 1 { public static void main(String[] args) { JFrame frame = new JFrame(); // The button to trigger the calculation JButton button = new JButton("Add Interest"); frame. add(button); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 1. java (cont. ) 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: // The application adds interest to this bank account final Bank. Account account = new Bank. Account(INITIAL_BALANCE); class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { // The listener method accesses the account variable // from the surrounding block double interest = account. get. Balance() * INTEREST_RATE / 100; account. deposit(interest); System. out. println("balance: " + account. get. Balance()); } } Action. Listener listener = new Add. Interest. Listener(); button. add. Action. Listener(listener); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 1. java (cont. ) 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: } frame. set. Size(FRAME_WIDTH, FRAME_HEIGHT); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); } private static final double INTEREST_RATE = 10; private static final double INITIAL_BALANCE = 1000; private static final int FRAME_WIDTH = 120; private static final int FRAME_HEIGHT = 60; Output: balance: 1100. 0 balance: 1210. 0 balance: 1331. 0 balance: 1464. 1 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Building Applications with Buttons • Example: investment viewer program; whenever button is clicked, interest is added, and new balance is displayed Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Building Applications with Buttons (cont. ) • Construct an object of the JButton class: JButton button = new JButton("Add Interest"); • We need a user interface component that displays a message: JLabel label = new JLabel("balance: " + account. get. Balance()); • Use a JPanel container to group multiple user interface components together: JPanel panel = new JPanel(); panel. add(button); panel. add(label); frame. add(panel); Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Building Applications with Buttons • Listener class adds interest and displays the new balance: class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { double interest = account. get. Balance() * INTEREST_RATE / 100; account. deposit(interest); label. set. Text("balance=" + account. get. Balance()); } } • Add. Interest. Listener as inner class so it can have access to surrounding final variables (account and label) Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 2. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: import import java. awt. event. Action. Event; java. awt. event. Action. Listener; javax. swing. JButton; javax. swing. JFrame; javax. swing. JLabel; javax. swing. JPanel; javax. swing. JText. Field; /** This program displays the growth of an investment. */ public class Investment. Viewer 2 { public static void main(String[] args) { JFrame frame = new JFrame(); // The button to trigger the calculation JButton button = new JButton("Add Interest"); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 2. java (cont. ) 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: // The application adds interest to this bank account final Bank. Account account = new Bank. Account(INITIAL_BALANCE); // The label for displaying the results final JLabel label = new JLabel( "balance: " + account. get. Balance()); // The panel that holds the user interface components JPanel panel = new JPanel(); panel. add(button); panel. add(label); frame. add(panel); class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { double interest = account. get. Balance() * INTEREST_RATE / 100; Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 2. java (cont. ) 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: } account. deposit(interest); label. set. Text( "balance: " + account. get. Balance()); } } Action. Listener listener = new Add. Interest. Listener(); button. add. Action. Listener(listener); frame. set. Size(FRAME_WIDTH, FRAME_HEIGHT); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); } private static final double INTEREST_RATE = 10; private static final double INITIAL_BALANCE = 1000; private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 100; Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Mouse Events • Use a mouse listener to capture mouse events • Implement the Mouse. Listener interface: public interface Mouse. Listener { void mouse. Pressed(Mouse. Event event); // Called when a mouse button has been pressed on a component void mouse. Released(Mouse. Event event); // Called when a mouse button has been released on a component void mouse. Clicked(Mouse. Event event); // Called when the mouse has been clicked on a component void mouse. Entered(Mouse. Event event); // Called when the mouse enters a component Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Mouse Events void mouse. Exited(Mouse. Event event); // Called when the mouse exits a component } • mouse. Pressed, mouse. Released: called when a mouse button is pressed or released • mouse. Clicked: if button is pressed and released in quick succession, and mouse hasn't moved • mouse. Entered, mouse. Exited: mouse has entered or exited the component's area Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Mouse Events • Add a mouse listener to a component by calling the add. Mouse. Listener method: public class My. Mouse. Listener implements Mouse. Listener { // Implements five methods } Mouse. Listener listener = new My. Mouse. Listener(); component. add. Mouse. Listener(listener); • Sample program: enhance Rectangle. Component – when user clicks on rectangle component, move the rectangle Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Rectangle. Component. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: import java. awt. Graphics; java. awt. Graphics 2 D; java. awt. Rectangle; javax. swing. JComponent; /** This component displays a rectangle that can be moved. */ public class Rectangle. Component extends JComponent { public Rectangle. Component() { // The rectangle that the paint method draws box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT); } // override paint. Component of JComponent public void paint. Component(Graphics g) { super. paint. Component(g); Graphics 2 D g 2 = (Graphics 2 D) g; Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Rectangle. Component. java (cont. ) 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: } g 2. draw(box); } /** Moves the rectangle to the @param x the x-position of @param y the y-position of */ public void move. To(int x, int { box. set. Location(x, y); repaint(); } given location. the new location y) private Rectangle box; private static final int int BOX_X = 100; BOX_Y = 100; BOX_WIDTH = 20; BOX_HEIGHT = 30; Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Mouse Events • Call repaint when you modify the shapes that paint. Component draws box. set. Location(x, y); repaint(); • Mouse listener: if the mouse is pressed, listener moves the rectangle to the mouse location class Mouse. Press. Listener implements Mouse. Listener { public void mouse. Pressed(Mouse. Event event) { int x = event. get. X(); int y = event. get. Y(); component. move. To(x, y); } Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Mouse Events • // Do-nothing methods public void mouse. Released(Mouse. Event event) {} public void mouse. Clicked(Mouse. Event event) {} public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} } • All five methods of the interface must be implemented; unused methods can be empty Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Rectangle. Component. Viewer Program Output Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Rectangle. Component. Viewer. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: import java. awt. event. Mouse. Listener; import java. awt. event. Mouse. Event; import javax. swing. JFrame; /** This program displays a Rectangle. Component. */ public class Rectangle. Component. Viewer { public static void main(String[] args) { final Rectangle. Component component = new Rectangle. Component(); // Add mouse press listener class Mouse. Press. Listener implements Mouse. Listener { public void mouse. Pressed(Mouse. Event event) { int x = event. get. X(); int y = event. get. Y(); Continued component. move. To(x, y); } Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Rectangle. Component. Viewer. java (cont. ) 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: } // Do-nothing methods public void mouse. Released(Mouse. Event event) {} public void mouse. Clicked(Mouse. Event event) {} public void mouse. Entered(Mouse. Event event) {} public void mouse. Exited(Mouse. Event event) {} } Mouse. Listener listener = new Mouse. Press. Listener(); component. add. Mouse. Listener(listener); JFrame frame = new JFrame(); frame. add(component); frame. set. Size(FRAME_WIDTH, FRAME_HEIGHT); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); } private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 400; Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check Why must the Mouse. Press. Listener class supply five methods? Answer: It implements the Mouse. Listener interface, which has five methods. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Using Inheritance to Customize Frames • Use inheritance for complex frames to make programs easier to understand • Design a subclass of JFrame • Store the components as instance fields • Initialize them in the constructor of your subclass • If initialization code gets complex, simply add some helper methods Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Processing Text Input • Use JText. Field components to provide space for user input final int FIELD_WIDTH = 10; // In characters final JText. Field rate. Field = new JText. Field(FIELD_WIDTH); • Place a JLabel next to each text field JLabel rate. Label = new JLabel("Interest Rate: "); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Processing Text Input • Supply a button that the user can press to indicate that the input is ready for processing Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Processing Text Input (cont. ) • The button's action. Performed method reads the user input from the text fields (use get. Text) Class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { double rate = Double. parse. Double(rate. Field. get. Text()); . . . } } Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Viewer 3. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: import javax. swing. JFrame; /** This program displays the growth of an investment. */ public class Investment. Viewer 3 { public static void main(String[] args) { JFrame frame = new Investment. Frame(); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Visible(true); } } Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java 01: import java. awt. event. Action. Event; 02: import java. awt. event. Action. Listener; 03: import javax. swing. JButton; 04: import javax. swing. JFrame; 05: import javax. swing. JLabel; 06: import javax. swing. JPanel; 07: import javax. swing. JText. Field; 08: 09: /** 10: A frame that shows the growth of an investment with variable interest. 11: */ 12: public class Investment. Frame extends JFrame 13: { 14: public Investment. Frame() 15: { 16: account = new Bank. Account(INITIAL_BALANCE); 17: 18: // Use instance fields for components 19: result. Label = new JLabel("balance: " + account. get. Balance()); 20: Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: // Use helper methods create. Text. Field(); create. Button(); create. Panel(); set. Size(FRAME_WIDTH, FRAME_HEIGHT); } private void create. Text. Field() { rate. Label = new JLabel("Interest Rate: "); final int FIELD_WIDTH = 10; rate. Field = new JText. Field(FIELD_WIDTH); rate. Field. set. Text("" + DEFAULT_RATE); } private void create. Button() { button = new JButton("Add Interest"); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { double rate = Double. parse. Double( rate. Field. get. Text()); double interest = account. get. Balance() * rate / 100; account. deposit(interest); result. Label. set. Text( "balance: " + account. get. Balance()); } } Action. Listener listener = new Add. Interest. Listener(); button. add. Action. Listener(listener); } private void create. Panel() Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: } { panel = new JPanel(); panel. add(rate. Label); panel. add(rate. Field); panel. add(button); panel. add(result. Label); add(panel); } private private JLabel rate. Label; JText. Field rate. Field; JButton button; JLabel result. Label; JPanel panel; Bank. Account account; private static final int FRAME_WIDTH = 450; private static final int FRAME_HEIGHT = 100; private static final double DEFAULT_RATE = 5; private static final double INITIAL_BALANCE = 1000; Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Text Areas • Use a JText. Area to show multiple lines of text • You can specify the number of rows and columns: final int ROWS = 10; final int COLUMNS = 30; JText. Area text. Area = new JText. Area(ROWS, COLUMNS); • set. Text: to set the text of a text field or text area • append: to add text to the end of a text area • Use newline characters to separate lines: text. Area. append(account. get. Balance() + "n"); • To use for display purposes only: text. Area. set. Editable(false); // program can call set. Text and append to change it Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Text Areas • To add scroll bars to a text area: JText. Area text. Area = new JText. Area(ROWS, COLUMNS); JScroll. Pane scroll. Pane = new JScroll. Pane(text. Area); Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java 01: import java. awt. event. Action. Event; 02: import java. awt. event. Action. Listener; 03: import javax. swing. JButton; 04: import javax. swing. JFrame; 05: import javax. swing. JLabel; 06: import javax. swing. JPanel; 07: import javax. swing. JScroll. Pane; 08: import javax. swing. JText. Area; 09: import javax. swing. JText. Field; 10: 11: /** 12: A frame that shows the growth of an investment with variable interest. 13: */ 14: public class Investment. Frame extends JFrame 15: { 16: public Investment. Frame() 17: { 18: account = new Bank. Account(INITIAL_BALANCE); 19: result. Area = new JText. Area(AREA_ROWS, AREA_COLUMNS); 20: result. Area. set. Editable(false); 21: Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: // Use helper methods create. Text. Field(); create. Button(); create. Panel(); set. Size(FRAME_WIDTH, FRAME_HEIGHT); } private void create. Text. Field() { rate. Label = new JLabel("Interest Rate: "); final int FIELD_WIDTH = 10; rate. Field = new JText. Field(FIELD_WIDTH); rate. Field. set. Text("" + DEFAULT_RATE); } private void create. Button() { button = new JButton("Add Interest"); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: class Add. Interest. Listener implements Action. Listener { public void action. Performed(Action. Event event) { double rate = Double. parse. Double( rate. Field. get. Text()); double interest = account. get. Balance() * rate / 100; account. deposit(interest); result. Area. append(account. get. Balance() + "n"); } } Action. Listener listener = new Add. Interest. Listener(); button. add. Action. Listener(listener); } private void create. Panel() { panel = new JPanel(); panel. add(rate. Label); panel. add(rate. Field); panel. add(button); Continued Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Investment. Frame. java (cont. ) 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: } JScroll. Pane scroll. Pane = new JScroll. Pane(result. Area); panel. add(scroll. Pane); add(panel); } private private JLabel rate. Label; JText. Field rate. Field; JButton button; JText. Area result. Area; JPanel panel; Bank. Account account; private static final int FRAME_WIDTH = 400; private static final int FRAME_HEIGHT = 250; private static final int AREA_ROWS = 10; private static final int AREA_COLUMNS = 30; private static final double DEFAULT_RATE = 5; private static final double INITIAL_BALANCE = 1000; Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Self Check How would you modify the Investment. Frame program if you didn't want to use scroll bars? Answer: Don't construct a JScroll. Pane and add the result. Area object directly to the frame. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
- Slides: 50