click below
click below
Normal Size Small Size show me how
Chapter 14
Introduction to Sing Components
Term | Definition |
---|---|
UI(User Interface) Components | Buttons, text fields, and other components with which the user can interact. Also called controls or widgets |
Swing Components | UI elements such as dialog boxes and buttons; you can usually recognize their names because they begin with J |
Java Foundation Classes(JFC) | A more general set of UI programming capabilities. JFC includes Swing component classes and selected classes from the java.awt package |
Lightweight Components | Almost all Swing components are said to be lightweight components because they are written completely in Java and do not have to rely on the local operating system code |
Heavyweight Components | Some Swing components, such as JFrames, are known as heavyweight components because they do require interaction with the local operating system |
Container | A type of component that holds other components so you can treat a group of them as a single entity |
1. Method Inherited by the JFrame Class | 1. Method[void setTitle(String)]..Purpose[Sets a JFrame's title using the String argument]... 2. Method[void setSize(int, int)]..Purpose[Sets a JFrame's size in pixels with the width and height as arguments]... |
2. Method Inherited by the JFrame Class | 3. Method[void setSize(Dimension)]..Purpose[Sets a JFrame's size using Dimension class object: the Dimension(int, int) constructor creates an object that represents both a width and height]... |
3. Method Inherited by the JFrame Class | 4. Method[String getTitle()]..Purpose[Returns a JFrame's title]... 5. Method[void setResizble(boolean)]..Purpose[Sets the JFrame to be resizable by passing true to the method, or sets the JFrame not to be resizable by passing false to the method].... |
4. Method Inherited by the JFrame Class | 6. Method[boolean isResizable()]..Purpose[Returns true or false to indicate whether JFrame is resizable]... 7. Method[void setVisible(boolean)]..Purpose[Sets JFrame to be visible using boolean argument true and invisible using boolean argument false]... |
5. Method Inherited by the JFrame Class | 8. Method[void setBounds(int, int, int, int)]..Purpose[Overrides the default behavior for the JFrame to be positioned in the upper-left corner or computer screen. |
6. Method Inherited by the JFrame Class | First two arguments are horizontal and vertical positions or JFrame's upper-left corner on desktop. Final two arguments set width and height.] |
Pixels | The picture elements, or tiny dots of light, that make up the image on your computer monitor |
Look and Feel | The default appearance and behavior of any user interface. You can request that Java's look and feel provide the decorations for the frame |
JLabel | A built-in Java Swing class that holds text you can display |
add() Method | You can add JLabel and other object to a JFrame object using this method. Ex: JLabel greeting = new Label("Good day");... aFrame.add(greeting);... |
remove() Method | Counterpart to the add() method. Removes object from a JFrame. Ex: aFrame.remove(greeting); |
setText() Method | Can be used to change the text in a JLabel by using this Component class method with the JLabel object and passing a String to it. Ex: greeting.setText("Howdy"); |
getText() Method | Used to retrieve the text in a JLabel(or other Component) by using this method, which returns the currently stored String |
Font Class | A Java provided class from which you can create an object that holds typeface and size information. |
setFont() Method | Requires a Font object argument. To construct a Font object, you need three arguments: typeface, style, and point size. |
Typeface Argument | The typeface argument to the Font constructor is a String representing a font. Common fonts have names such as Arial, Century, Monospaced, and Times New Roman. |
Style Argument | Applies an attribute to displayed text and is one of three values: Font.PLAIN, Font.BOLD, or Font.ITALIC |
Point Size Argument | An integer that represents about 1/72 of an inch. Printed text is commonly 12 points; a headline might be 30 points |
1. Giving a JLabel Object New Font | To give a JLabel object a new font, you can create a FOnt object, as in the following... Font headlineFont = new Font("Monospaced", Font.Bold, 36);. The Typeface name is a String, so you must enclose it in double quotation marks. |
2. Giving a JLabel Object New Font | You can use the setFont() method to assign the Font to a JLabel with a statement such as... greeting.setFont(headlineFont); |
Layout Manager | To place multiple components at specified positions in a container so they do not hide each other, you must explicitly use a layout manager - a class that controls component positioning. Normal behavior of a JFrame is to use layout format BorderLayout |
BorderLayout | Created by using BorderLayout class. Divides a container into regions. When you do not specify a region in which to place a component, all the components are placed in the same region, and they obscure each other |
Flow Layout Manager | Places components in a row, and when a row is filled, components automatically spill into the next row |
FlowLayout Class Three Constants | Specify how components are positioned in each row of their container. These constants are FlowLayout.LEFT, FlowLayout.RIGHT, and FlowLayout.CENTER. Ex: FlowLayout flow = new FlowLayout(FlowLayout.RIGHT); |
setLayout() Method | Sets a JFrame's layout. Ex: aFrame.setLayout(flow);. A more compact syntax that uses an anonymous FlowLayout object is... aFrame.setLayout(new FlowLayout()); |
JTextField | A component into which a user can type a single line of text data. Text data comprises any characters you can enter from the keyboard, including numbers and punctuation |
Instantiating a JTextField Object | To provide a JTextField that allows enough room for a user to enter approximately 10 characters, you can code the following.. JTextField response = new JTextField(10);... To add the JTextField response to JFrame frame, you write.. frame.add(response); |
Editable JTextFields | A JTextField is editable when it has the capability of accepting keystrokes. A JTextField is editable by default |
setEditable() Method | If you do not want the user to be able to enter data in a JTextField, you can send a Boolean value to this method to change the JTextField's editable status |
JButton | A Component the user can click with a mouse to make a selection |
Tool Tips | Popup windows that can help a user understand the purpose of components in an applications; the tool tip appears when a user hovers the mouse pointer over the component |
setToolTipText() Method | Used to define the text to be displayed in a tool tip by passing an appropriate String to it. Ex: button.setToolTipText("Click this button"); |
Event | Occurs when a user takes action on a component, such as clicking the mouse on a JButton object |
Event-Driven Program | A program in which the user might initiate any number of events in any order |
Source of Event | Within an event-driven program, a component on which an event is generated is the source of the event |
Listener | An object that is interested in an event |
addActionListener() Method | Used to tell your class to expectnActionEvents |
Preparing Your Class to Accept Event Messages | You prepare your class to accept button button press events by importing the java.awt.event package into your program and adding the phrase implements ActionListener to the class header |
java.awt.event Package | Includes event classes with names such as ActionEvent, ComponentEvent, and TextEvent. |
ActionListener | Is an interface - a class containing a set of specifications for methods that you can use. Implementing ActionListener provides you with standard event method specifications that allow your listener to work with ActionEvent |
ActionEvents | The types of events that occur when a user clicks a button |
actionPerformed(ActionEvent e) Method | When a class =, such as a JFrame , has a registered as a listener with a Component such as a JButton, and a user clicks the JButton, the actionPerformed() method executes. The ActionLIstener interface contains this method |
1. Responding to Events | You use the following header, in which e represents any name you choose for the Event(the JButton click) that intiated the notification of the ActionListener(which is the JFrame)... public void actionPerformed(ActionEvent e). |
2. Responding to Events | The body of the method contains any statements that you want to execute when the action occurs |
setEnabled() Method | Used to make a component available or unavailable by passing true or false to it, respectively |
1. Alphabetical List of Some Event Listeners | 1. Listener[ActionListener]..TypeOfEvents[Action events]..Ex[Button clicks]... 2. Listener[AdjustmentListener]..TypeOfEvents[Adjustment events]..Ex[Scroll bar moves]... |
2. Alphabetical List of Some Event Listeners | 3. Listener[ChangeListener]..TypeOfEvents[Change events]..Ex[Slider is repositioned]... 4. Listener[FocusListener]..TypeOfEvents[Keyboard focus events]..Ex[Text field grains or loses focus]... |
3. Alphabetical List of Some Event Listeners | 5. Listener[ItemListener]..TypeOfEvents[Item events]..Ex[Check box changes status]... 6. Listener[KeyListener]..TypeOfEvents[Keyboard events]..Ex[Text is entered]... |
4. Alphabetical List of Some Event Listeners | 7. Listener[MouseListener]..TypeOfEvents[Mouse events]..Ex[Mouse clicks]... 8. Listener[MouseMotionListener]..TypeOfEvents[Mouse movement events]..Ex[Mouse rolls]... |
5. Alphabetical List of Some Event Listeners | 9. Listener[WindowListener]..TypeOfEvents[Window events]..Ex[Window closes] |
1. Some Swing Components and Their Associated Listener-Registering Methods | 1. Components[JButton, JCheckBox, JComboBox, JTextField, and JRadioButton]..AssociatedListenerRegisteringMethods[addActionListener()]... |
2. Some Swing Components and Their Associated Listener-Registering Methods | 2. Components[JScrollBar]..AssociatedListenerRegisteringMethods[addAdjustmentListener()]... |
3. Some Swing Components and Their Associated Listener-Registering Methods | 3. Components[All Swing components]..AssociatedListenerRegisteringMethods[addFocusListener(), addKeyListener(), addMouseListener(), and addMouseMotionListener()]... |
4. Some Swing Components and Their Associated Listener-Registering Methods | 4. Components[JButton, JCheckBox, JComboBox, and JRadioButton]..AssociatedListenerRegisteringMethods[addItemListener()]... |
5. Some Swing Components and Their Associated Listener-Registering Methods | 5. Components[All JWindow and JFrame components]..AssociatedListenerRegisteringMethods[addWindowListener()]... 6. Components[JSlider and JCheckBox]..AssociatedListenerRegisteringMethods[addChangeListener())] |
Event Handler | A method that executes because it is called automatically when an appropriate event occurs |
1. Selected Methods that Respond to Events | 1. Listener[ActionListener]..Method[actionPerformed(ActionEvent)]... 2. Listener[AdjustmentListener]..Method[adjustmentValueChanged(AdjustmentsEvent)]... |
2. Selected Methods that Respond to Events | 3. Listener[FocusListener]..Method[focusGained(FocusEvent) and focusLost(FocusEvent)]... 4. Listener[ItemListener]..Method[itemStatedChanged(ItemEvent)] |
JCheckBox | Consists of a label positioned beside a square; you can click the square to display or remove a check mark |
ButtonGroup Class | When you create a ButtonGroup, you can group several components, such as JCheckBoxes, so a user can select only one at a time |
JComboBox | A component that combines two features: a display area showing a default option and list box that contains additional, alternate options |
Generic Programming | A feature of modern languages that allows multiple data types to be used safely with methods. |