Java Swing and GUI
Anonymous Classes
Enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. *Use them if you need to use a local class only once
KeyEvent Class
generated by a component object when a key is pressed, released, or typed getKeyChar(): returns the character associated with the key in this event getKeyCode(): returns the integer key code associated with the key in this event. getKeyLocation(): returns the location of the key that originated this key event
Key events
generated when the user presses a keyboard key (KeyListener interface)
Flow Layout
Puts as many components as possible on a row, at their preferred size The JPanel class uses flow layout by default. When a component can't fit on a row, it's put on the next row.
3 different ways to declare a class
Regular class, named inner class, anonymous inner class
Container
a special type of component that is used to hold and organize other components can establish a containment hierarchy Ex: JFrame
Key Listener Interface
keyTyped(KeyEvent): called just after the user types a Unicode character into the listened-to component. keyPressed(KeyEvent):called just after the user presses a key while the listened-to component has the focus keyReleased(KeyEvent): called just after the user releases a key while the listened-to component has the focus
Listeners
an object that "wait" for an event to occur and responds in way when it does
Component
an object that defines a screen element used to display info or allow the user to interact with the program
Events
an object that represents some occurrence in which we may be interested; ofter correspond to mouse button press, keyboard key press, etc.
Regular class
declared and named in its own file
Named inner class
declared and named inside of another class
Anonymous inner class
declared, but not named, inside of another class
Layout Managers
determines how the components in the container are arranged visually Ex: FlowLayout, BorderLayout, GridLayout, BoxLayout
Mouse events
occur when the user interacts with another component via the mouse (MouseListener interface class)
Mouse motion events
occur while the mouse is in motion (MouseMotionListener interface class)
Name other components
text fields, check boxes, radio buttons, sliders, combo boxes, timers
AWT
the original Java GUI package
Swing
Provides components that are more versatile than those of AWT
Adapter Classes
Each listener interface has a corresponding abstract class that we call an adapter
What are the 3 kinds of objected need to create a GUI?
Components, Events, and Listeners
Focus
For a key press to affect a component, the component must have the keyboard focus.
What does GUI stand for?
Graphical User Interfaces
Border Layout
Has 5 areas to which components can be added: North, South, East, West, and Center
What are some examples of heavyweight and lightweight containers?
Heavyweight- a frame, and applet; it's managed by the underlying operating system. Lightweight- a panel; it's managed by the Java program; more complex
JavaFX
It's designed to provide a lightweight, hardware-accelerated Java UI platform for enterprise business applications. Intended to replace Swing
Box Layout
Organizes components either vertically or horizontally,in one row or one column Components are organized in the order in which they are added to the container. There are no gaps b/w the components.
Grid Layout
Presents a container's components in a rectangular grid of rows and columns One component is placed in each cell, and all cells are the same size. # of rows and columns is established by using parameters to the constructor when created As components are added, they fill the grid from left to right, top to bottom
3 GUI Packages
The Abstract Window Toolkit (AWT) Swing JavaFX