1 / 74

Chapter 11 Creating User Interfaces

Chapter 11 Creating User Interfaces. What is JavaBean? JComponent JButton ImageIcon JLabel JTextField JTextArea JComboBox JList. JCheckBox JRadioButton Menus Creating Multiple Windows JScrollBar JScrollPane. JComponent Properties. toolTipText font background foreground

saber
Download Presentation

Chapter 11 Creating User Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11Creating User Interfaces • What is JavaBean? • JComponent • JButton • ImageIcon • JLabel • JTextField • JTextArea • JComboBox • JList • JCheckBox • JRadioButton • Menus • Creating Multiple Windows • JScrollBar • JScrollPane

  2. JComponent Properties • toolTipText • font • background • foreground • doubleBuffered • border • preferredSize • minimumSize • maximumSize

  3. 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)

  4. JButton Properties • text • icon • mnemonic • horizontalAlignment • verticalAlignment • horizontalTextPosition • verticalTextPosition

  5. Responding to JButton Events public void actionPerformed(ActionEvent e) { // Get the button label String actionCommand = e.getActionCommand(); // Make sure the event source is a button if (e.getSource() instanceof JButton) // Make sure it is the right button if ("My Button".equals(actionCommand) System.out.println ("Button pressed!"); }

  6. JButton ButtonDemo Run

  7. 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 horizontalAlignment) JLabel(String text) JLabel(Icon icon) JLabel(Icon icon, int horizontalAlignment)

  8. JLabel Properties • text • icon • horizontalAlignment • verticalAlignment

  9. Example 11.2: Using Labels This example gives a program that uses a label as an area for displaying images. There are fifty-two images in image files named L1.gif, L2.gif, ..., L52.gif stored in the image directory under c:\book. You can use two buttons, Prior and Next, to browse the images. LabelDemo Run

  10. JTextField A text field is an input area where the usercan type in characters. Text fields are usefulin that they enable the user to enter in variable data (such as a name or a description).

  11. JTextField Constructors • JTextField(int columns) Creates an empty text field with the specified number of columns. • JTextField(String text) Creates a text field initialized with the specified text. • JTextField(String text, int columns) Creates a text field initialized with thespecified text and the column size.

  12. JTextField Properties • text • horizontalAlignment • editable • columns

  13. JTextField Methods • getText() Returns the string from the text field. • setText(String text) Puts the given string in the text field. • setEditable(boolean editable) Enables or disables the text field to be edited. By default, editable is true. • setColumns(int) Sets the number of columns in this text field.The length of the text field is changeable.

  14. Example 11.3: Using Text Fields This example gives a program that enters two numbers in two text fields and displays their sum in the third text field when you press the Add button. TextFieldDemo Run

  15. JTextArea 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 JTextArea, which enables the user to enter multiple lines of text.

  16. JTextArea Constructors • JTextArea(int rows, int columns) Creates a text area with the specified number of rows and columns. • JTextArea(String s, int rows, int columns) Creates a text area with the initial text andthe number of rows and columns specified.

  17. JTextArea Properties • text • editable • columns • lineWrap • wrapStyleWord • rows • lineCount • tabSize

  18. Example 11.4 Using Text Areas • This example gives a program that displays an image in a label, a title in a label, and a text in a text area.

  19. Example 11.4, cont. TextAreaDemo Run

  20. JComboBox 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: JComboBox()

  21. JComboBox Methods To add an item to a JComboBox jcbo, use jcbo.addItem(Object item) To get an item from JComboBox jcbo, use jcbo.getItem()

  22. Using theitemStateChanged Handler public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem(); } When a choice is checked or unchecked, itemStateChanged() for ItemEvent is invoked as well as the actionPerformed() handler for ActionEvent.

  23. Example 11.5: Using Combo Boxes This example lets users view an image and a description of a country's flag by selecting the country from a combo box. Figure 11.8 shows a sample run of the program. ComboBoxDemo Run

  24. 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.

  25. JList Constructors • JList() Creates an empty list. • JList(Object[] stringItems) Creates a new list initialized with items.

  26. JList Properties • selectedIndexd • selectedIndices • selectedValue • selectedValues • selectionMode • visibleRowCount

  27. Example 11.6: Using Lists This example gives a program that lets users select countries in a list and display the flags of the selected countries in the labels. ListDemo Run

  28. JCheckBox A check box is a component that enables the user to toggle a choice on or off, like a light switch.

  29. JCheckBox Constructors • JCheckBox() • JCheckBox(String text) • JCheckBox(String text, boolean selected) • JCheckBox(Icon icon) • JCheckBox(String text, Icon icon) • JCheckBox(String text, Icon icon, boolean selected)

  30. JCheckBox Properties JCheckBox has all the properties in JButton. Additionally, JButton has the following property: selected

  31. Example 11.7: Using Check Boxes This example gives a program that can dynamically change the font of a message to be displayed on a panel. The message can be displayed in bold and italic at the same time, or can be displayed in the center of the panel. CheckBoxDemo Run

  32. JRadioButton Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time.

  33. JRadioButton Radio buttons are variations of check boxes. They are often used in the group, where only one button is checked at a time.

  34. JRadioButton Constructors • JRadioButton() • JRadioButton(String text) • JRadioButton(String text, boolean selected) • JRadioButton(Icon icon) • JRadioButton(String text, Icon icon) • JRadioButton(String text, Icon icon, boolean selected)

  35. JRadioButton Properties JRadioButton has all the properties in JButton. Additionally, JButton has the following property: selected

  36. Grouping Radio Buttons ButtonGroup btg = new ButtonGroup(); btg.add(jrb1); btg.add(jrb2);

  37. Example 11.8: Using Radio Buttons This example shows a program that simulates traffic lights. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time. No light is on when the program starts. RadioButtonDemo Run

  38. 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.

  39. Static Method for Creating Borders • createTitledBorder(String title) • createLoweredBevelBorder() • createRaisedBevelBorder() • createLineBorder(Color color) • createLineBorder(Color color, int thickness) • createEtchedBorder() • createEtchedBorder(Color highlight, Color shadow, boolean selected) • createEmptyBorder() • createMatteBorder(int top, int left, int bottom, int right, Icon tileIcon) • createCompoundBorder(Border outsideBorder, Border insideBorder)

  40. Example 11.9: Using Borders This example gives a program that creates and displays various types of borders. You can select a border with a title or without a title. For a border without a title, you can choose a border style from Lowered Bevel, Raised Bevel, Etched, Line, Matte, or Empty. For a border with a title, you can specify the title position and justification. You can also embed another border into a titled border.

  41. Example 11.9, cont. BorderDemo Run

  42. JOptionPane Dialogs • A dialog is normally used as a temporary window to receive additional information from the user, or to provide notification that some event has occurred.

  43. Message Dialogs A message dialog box simply displays a message to alert the user and waits for the user to click the OK button to close the dialog.

  44. Message Types The messageType is one of the following constants: JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.PLAIN_MESSAGE JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE

  45. Message Types, cont.

  46. Confirmation Dialogs A message dialog box displays a message and waits for the user to click the OK button to dismiss the dialog. The message dialog does not return any value. A confirmation dialog asks a question and requires the user to respond with an appropriate button. The confirmation dialog returns a value that corresponds to a selected button.

  47. Input Dialogs An input dialog box is used to receive input from the user. The input can be entered from a text field or selected from a combo box or a list. Selectable values can be specified in an array, and a particular value can be designated as the initial selected value.

  48. Option Dialogs An option dialog allows you to create custom buttons.

  49. Example 11.10: Creating Standard Dialogs • Objective: This example demonstrates using standard dialogs. The program prompts the user to select the annual interest rate from a list in an input dialog, the number of years from a combo box in an input dialog, and the loan amount from an input dialog, and displays the loan payment schedule in a text area inside a JScrollPane in a messasge dialog.

  50. Example 11.10: Creating Standard Dialogs, cont. JOptionPaneDemo Run

More Related