Chapter 11 Creating User Interfaces F What is



















![JList Constructors F JList() Creates an empty list. F JList(Object[] string. Items) Creates a JList Constructors F JList() Creates an empty list. F JList(Object[] string. Items) Creates a](https://slidetodoc.com/presentation_image_h2/7eb67cb2c3b2b695efd6d7b5ccf54c92/image-20.jpg)
































- Slides: 52
Chapter 11 Creating User Interfaces F What is Java. Bean? JComponent F JButton F Image. Icon F JLabel F JText. Field F JText. Area F JCombo. Box F JList F F JCheck. Box F JRadio. Button F F Menus Creating Multiple Windows JScroll. Bar JScroll. Pane 1
JComponent Properties F tool. Tip. Text F font F background F foreground F double. Buffered F border F preferred. Size F minimum. Size F maximum. Size 2
JButton A button is a component that triggers an action event when clicked. The following are JButton non-default constructors: JButton(String text) JButton(String text, Icon icon) JButton(Icon icon) Example 11. 1: Using Buttons Button. Demo Run 3
JButton Properties F text F icon F mnemonic F horizontal. Alignment F vertical. Alignment F horizontal. Text. Position F vertical. Text. Position 4
Responding to JButton Events public void action. Performed(Action. Event e) { // Get the button label String action. Command = e. get. Action. Command(); // Make sure the event source is a button if (e. get. Source() instanceof JButton) // Make sure it is the right button if ("My Button". equals(action. Command) System. out. println ("Button pressed!"); } 5
JLabel A label is a display area for a short text, an image, or both. The non-default constructors for labels are as follows: JLabel(String text, int horizontal. Alignment) JLabel(String text) JLabel(Icon icon, int horizontal. Alignment) Example 11. 2: Using Labels Label. Demo Run 6
JLabel Properties F text F icon F horizontal. Alignment F vertical. Alignment 7
JText. Field A text field is an input area where the user can type in characters. Text fields are useful in that they enable the user to enter in variable data (such as a name or a description). Example 11. 3: Using Text Fields Text. Field. Demo Run 8
JText. Field Constructors F JText. Field(int columns) Creates an empty text field with the specified number of columns. F JText. Field(String text) Creates a text field initialized with the specified text. F JText. Field(String text, int columns) Creates a text field initialized with the specified text and the column size. 9
JText. Field Properties F text F horizontal. Alignment F editable F columns 10
JText. Field Methods F get. Text() Returns the string from the text field. F set. Text(String text) Puts the given string in the text field. F set. Editable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. F set. Columns(int) Sets the number of columns in this text field. The length of the text field is changeable. 11
JText. Area If you want to let the user enter multiple lines of text, you cannot use text fields unless you create several of them. The solution is to use JText. Area, which enables the user to enter multiple lines of text. 12
JText. Area Constructors F JText. Area(int rows, int columns) Creates a text area with the specified number of rows and columns. F JText. Area(String s, int rows, int columns) Creates a text area with the initial text and the number of rows and columns specified. 13
JText. Area Properties F text F editable F columns F line. Wrap F wrap. Style. Word F rows F line. Count F tab. Size 14
Example 11. 4 Using Text Areas F This example gives a program that displays an image in a label, a title in a label, and a text in a text area. Text. Area. Demo Run 15
JCombo. Box A combo box is a simple list of items from which the user can choose. It performs basically the same function as a list, but can get only one value. To create a choice, use its default constructor: JCombo. Box() Example 11. 5: Using Combo Boxes Combo. Box. Demo Run 16
JCombo. Box Methods To add an item to a JCombo. Box jcbo, use jcbo. add. Item(Object item) To get an item from JCombo. Box jcbo, use jcbo. get. Item() 17
Using the item. State. Changed Handler When a choice is checked or unchecked, item. State. Changed() for Item. Event is invoked as well as the action. Performed() handler for Action. Event. public void item. State. Changed(Item. Event e) { // Make sure the source is a combo box if (e. get. Source() instanceof JCombo. Box) String s = (String)e. get. Item(); } 18
JList A list is a component that performs basically the same function as a combo box, but it enables the user to choose a single value or multiple values. Example 11. 6: Using Lists List. Demo Run 19
JList Constructors F JList() Creates an empty list. F JList(Object[] string. Items) Creates a new list initialized with items. 20
JList Properties F selected. Indexd F selected. Indices F selected. Values F selection. Mode F visible. Row. Count 21
JCheck. Box A check box is a component that enables the user to toggle a choice on or off, like a light switch. Example 11. 7: Using Check Boxes Check. Box. Demo Run 22
JCheck. Box Constructors F JCheck. Box() F JCheck. Box(String text, boolean selected) F JCheck. Box(Icon icon) F JCheck. Box(String text, Icon icon, boolean selected) 23
JCheck. Box Properties JCheck. Box has all the properties in JButton. Additionally, JButton has the following property: selected 24
JRadio. Button Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time. Example 11. 8: Using Radio Buttons Radio. Button. Demo Run 25
JRadio. Button Constructors F JRadio. Button() F JRadio. Button(String text, boolean selected) F JRadio. Button(Icon icon) F JRadio. Button(String text, Icon icon, boolean selected) 26
JRadio. Button Properties JRadio. Button has all the properties in JButton. Additionally, JButton has the following property: selected 27
Grouping Radio Buttons Button. Group btg = new Button. Group(); btg. add(jrb 1); btg. add(jrb 2); 28
Borders You can set a border on any object of the JComponent class, but often it is useful to set a titled border on a JPanel that groups a set of related user interface components. Example 11. 9: Using Borders Border. Demo Run 29
Static Method for Creating Borders Fcreate. Titled. Border(String title) Fcreate. Lowered. Bevel. Border() Fcreate. Raised. Bevel. Border() Fcreate. Line. Border(Color color, int thickness) Fcreate. Etched. Border(Color highlight, Color shadow, boolean selected) Fcreate. Empty. Border() Fcreate. Matte. Border(int top, int left, int bottom, int right, Icon tile. Icon) Fcreate. Compound. Border(Border outside. Border, Border inside. Border) 30
Message Dialogs FA dialog is normally used as a temporary window to receive additional information from the user, or to provide notification that some event has occurred. 31
Creating Message Dialogs Use static method in JOption. Pane class. show. Message. Dialog(Component parent. Component, Object message, String title, int message. Type) show. Message. Dialog(Component parent. Component, Object message, String title, int message. Type, Icon icon) 32
Example 11. 10: Using Message Dialogs F Objective: Display student exam scores. The program prompts the user to enter the user’s last name and the password in a dialog box. Upon receiving the correct user name and password, the program displays the student’s full name and the exam score. Dialog. Demo Run 33
Menus F Java provides several classes—JMenu. Bar, JMenu. Item, JCheck. Box. Menu. Item, and JRadio. Button. Menu. Item —to implement menus in a frame. F A JFrame or JApplet can hold a menu bar to which the pull-down menus are attached. Menus consist of menu items that the user can select (or toggle on or off). Menu bars can be viewed as a structure to support menus. 34
Menu Demo 35
The JMenu. Bar Class A menu bar holds menus; the menu bar can only be added to a frame. Following is the code to create and add a JMenu. Bar to a frame: JFrame f = new JFrame(); f. set. Size(300, 200); f. set. Visible(true); JMenu. Bar mb = new JMenu. Bar(); f. set. JMenu. Bar(mb); 36
The Menu Class You attach menus onto a JMenu. Bar. The following code creates two menus, File and Help, and adds them to the JMenu. Bar mb: JMenu file. Menu = new JMenu("File", false); JMenu help. Menu = new JMenu("Help", true); mb. add(file. Menu); mb. add(help. Menu); 37
The JMenu. Item Class You add menu items on a menu. The following code adds menu items and item separators in menu file. Menu: file. Menu. add(new JMenu. Item("new")); JMenu. Item("open")); JMenu. Item("-")); JMenu. Item("print")); JMenu. Item("exit")); JMenu. Item("-")); 38
Submenus You can add submenus into menu items. The following code adds the submenus “Unix, ” “NT, ” and “Win 95” into the menu item “Software. ” JMenu software. Help. Sub. Menu = new JMenu("Software"); JMenu hardware. Help. Sub. Menu = new JMenu("Hardware"); help. Menu. add(software. Help. Sub. Menu); help. Menu. add(hardware. Help. Sub. Menu); software. Help. Sub. Menu. add(new JMenu. Item("Unix")); software. Help. Sub. Menu. add(new JMenu. Item("NT")); software. Help. Sub. Menu. add(new JMenu. Item("Win 95")); 39
Submenu Demo 40
Example 11. 11: Using Menus F Objective: Create a user interface that performs arithmetic. The interface contains labels and text fields for Number 1, Number 2, and Result. The Result box displays the result of the arithmetic operation between Number 1 and Number 2. Menu. Demo Run 41
Creating Multiple Windows The following slides show step-by-step how to create an additional window from an application or applet. 42
Creating Additional Windows, Step 1: Create a subclass of JFrame (called a Sub. Frame) that tells the new window what to do. For example, all the GUI application programs extend JFrame and are subclasses of JFrame. 43
Creating Additional Windows, Step 2: Create an instance of Sub. Frame in the application or applet. Example: Sub. Frame sub. Frame = new Sub. Frame("Sub. Frame Title"); 44
Creating Additional Windows, Step 3: Create a JButton for activating the sub. Frame. add(new JButton("Activate Sub. Frame")); 45
Creating Additional Windows, Step 4: Override the action. Performed() method as follows: public action. Performed(Action. Event e) { String action. Command = e. get. Action. Command(); if (e. target instanceof Button) { if ("Activate Sub. Frame". equals(action. Command)) { sub. Frame. set. Visible(true); } } } 46
Example 11. 12 Creating Multiple Windows F This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram. " When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area. Multiple. Windows. Demo Run Histogram 47
JScroll. Bar FA scroll bar is a control that enables the user to select from a range of values. The scrollbar appears in two styles: horizontal and vertical. Example 11. 13: Using Scrollbars Scroll. Bar. Demo Run 48
Scroll Bar Properties 49
JScroll. Pane FA scroll pane is a component that supports automatically scrolling without coding. Example 11. 14: Using Scroll Panes Scroll. Pane. Demo Run 50
Scroll Pane Structures 51
JTabbed. Pane FA tabbed pane provides a set of mutually exclusive tabs for accessing multiple components. Example 11. 15: Using Tabbed Panes Tabbed. Pane. Demo Run 52