Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.

Similar presentations


Presentation on theme: "Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons."— Presentation transcript:

1 Creating User Interfaces Chapter 13 CSCI 1302

2 CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons –Icons –Alignments –Text Positions Check Boxes Radio Buttons

3 CSCI 1302 – Creating User Interfaces3 Outline Labels Text Fields Text Areas Combo Boxes Lists Scroll Bars Sliders Creating Multiple Windows

4 CSCI 1302 – Creating User Interfaces4 Introduction GUI components make up the user interfaces that you see and use Java provides many components that provide a wide variety of functionality

5 CSCI 1302 – Creating User Interfaces5 Common Features All but a few components are subclasses of JComponent They share a lot of the same features and methods, making it easy to learn and use these items See Figure 13.2 for a list of common features See TestSwingCommonFeatures.java

6 CSCI 1302 – Creating User Interfaces6 Buttons Triggers an action event when clicked Includes regular buttons, toggle buttons, check box buttons, and radio buttons Common button features are represented in the parent class javax.swing.AbstractButton Regular buttons are JButton s

7 CSCI 1302 – Creating User Interfaces7 Icons Small, fixed-size pictures used to decorate components Use the ImageIcon class to create icons from image files: Icon i = new ImageIcon(“photo.gif”); Three button icon states: default, pressed, and rollover See TestButtonIcons.java

8 CSCI 1302 – Creating User Interfaces8 Alignments Horizontal alignment specifies horizontally placement on the button Use setHorizontalAlignment(int) with the constants LEADING, LEFT, CENTER, RIGHT, TRAILING Vertical alignment specifies vertical placement on the button Use setVerticalAlignment(int) with the constants TOP, CENTER, BOTTOM See Figures 13.7 and 13.8

9 CSCI 1302 – Creating User Interfaces9 Text Positions Specifies text position relative to the icon Use setHorizontalTextPosition(int) with the constants LEADING, LEFT, CENTER, RIGHT, TRAILING Use setVerticalTextPosition(int) with the constants TOP, CENTER, BOTTOM See note about SwingConstants See Figures 13.9 and 13.10 and ButtonDemo.java

10 CSCI 1302 – Creating User Interfaces10 Check Boxes Toggle button is a two-state button like a light switch Often used to toggle choices on or off Many options available through constructors, see Figure 13.14 Fires ItemEvent s Use isSelected() to query selected state See CheckBoxDemo.java

11 CSCI 1302 – Creating User Interfaces11 Radio Buttons Enable the user to choose a single choice out of a group of choices Must use a ButtonGroup to create groups of individual radio buttons Fires ItemEvent, then an ActionEvent Use isSelected() to query selected state See RadioButtonDemo.java

12 CSCI 1302 – Creating User Interfaces12 Labels Display area for short text, images, or both See Figure 13.18 for constructors and methods Already used in TVFrame.java

13 CSCI 1302 – Creating User Interfaces13 Text Fields Can be used to enter or display a string Like other components, has many properties that can be set Can be editable or not See TextFieldDemo.java

14 CSCI 1302 – Creating User Interfaces14 Text Areas Used to enter multiple lines of text as opposed to using multiple text fields See Figure 13.22 for constructors and methods Does not scroll automatically, but can be contained in a JScrollPane See TextAreaDemo.java

15 CSCI 1302 – Creating User Interfaces15 Combo Boxes Known as choice or drop-down lists A list of items from which the user can choose Limit’s range of choices, helps input validation See Figure 13.25 for constructors and methods Can generate ActionEvent and ItemEvent See ComboBoxDemo.java

16 CSCI 1302 – Creating User Interfaces16 Lists Performs same functions as a combo box, but enables the user to select both single and multiple values See Figure 13.27 for constructors and methods Three selection modes: Single, single- interval, and multiple-interval Do not scroll automatically Fires ListSelectionEvent

17 CSCI 1302 – Creating User Interfaces17 Scroll Bars

18 CSCI 1302 – Creating User Interfaces18 Scroll Bars orientation – describes if the scroll bar is displayed horizontally or vertically maximum – maximum value minimum – minimum value visibleAmount (extent) – Relative width of the scroll bar’s bubble value – current value of scroll bar blockIncrement – used in block areas unitIncrement – used in unit areas

19 CSCI 1302 – Creating User Interfaces19 Scroll Bars When value is changed, fires an AdjustmentEvent to all registered listeners Objects notified of changes to the scroll bar’s value must implement the adjustmentValueChanged method in the AdjustmentListener interface See ScrollBarDemo.java

20 CSCI 1302 – Creating User Interfaces20 Sliders Similar to scrollbar but more versatile See Figure 13.33 for constructors and methods User can graphically select a value by sliding a knob within a bounded interval Fires a ChangeEvent which is passed to registered listeners who must implement stateChanged in ChangeListener interface See SliderBarDemo.java

21 CSCI 1302 – Creating User Interfaces21 Creating Multiple Windows Subwindows can be opened during the lifetime of a “main” window Can create an instance of a new window and make it visible See MultipleWindowsDemo.java


Download ppt "Creating User Interfaces Chapter 13 CSCI 1302. CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons."

Similar presentations


Ads by Google