BIS 335 Chapter 6
Applets
- GUI program that runs in a rectangular area on a web page - provide interactivity on web pages - does not have a main method
Stand-alone
- GUI program that runs on its own without a web browser - any class that has a main() is stand alone
frame
- a container used to display a GUI-based java application - most common = JFrame object which is independent and main window of application
JCheckBox
- component has two states, selected or unselected - represented by boolean value (true if selected, false if not) - constructor: JCheckBox showTime = new JCheckBox("Show Time");
3 kinds of objects in GUIs
- components - events - listeners
panel
- container that cannot be displayed on its own but used to organize other components - most common = JPanel - must be added to another container (usually JFrame) to be displayed
FlowLayout constructors
- default: new FlowLayout() - to specify other gaps and alignment: public FlowLayout(int align, int hgap, int vgap) - values for alignment = FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER
BorderLayout
- designed to display one large, central component with up to four smaller components arranged around the edge
containers
- hold and organize other components - frames, panels, applets, dialog boxes
JTextField
- holds a single line of text - constructor: public JTextField(int columns) - columns = preferred width of text field
JTextArea
- holds multiple lines of text - constructor: public JTextArea(String contents, int rows, int columns)
GridLayout
- lays out components in grid containing rows and columns of equal sized rectangles - add components: cntr.add(comp) - constructor = new GridLayout(R,C)
Borders
- leave border around outside of container, not handled by layout manager - can be added to ANY component - use setBorder() method to apply to an object
BorderFactory.createLineBorder(top, left, bottom, right)
- leaves empty border around edges of component - parameter = integers
FLowLayout
- lines up components in a row - default layout for JPanel - uses preferred size of components
event
- object that represents some activity that we may want to respond to - ex. we want program to perform some action when the following occurs: - mouse is moved - keyboard key clicked - a timer expires
Listeners
- objects designed to respond to events generated by component objects - as event occurs, the component calls the appropriate method of the listener and passes the object that describes the event
components
- objects that represent screen elements (labels, buttons, text fields, menus, etc.) - organized in containers
JSlider
- provides way for user to select integer value from range of possible values - constructor: public JSlider(int minimum, int maximum, int value)
JButton
- push button that users clicks to trigger some action - constructor: JButton button = new JButton("Exit");
JLabel
- simplest type of component, exists just to display lines of text that can't be changed by user - constructor: JLabel message = new JLabel("Click Here", JLabel.CENTER);
ActionEvent
- when user clicks button, generates an event of type ActionEvent - event sent to listener that must be registered with button as ActionListener - to register: buttonName.addActionListener(buttonHandler)
JFrame
- where stand-alone apps are created - blank content pane, you can add things or replace entirely with a new component - can add one or more panels
2 main GUI-related packages
1. Abstract Windowing Toolkit (AWT): original java GUI package - import java.awt.*; 2. Swing: provides additional/more versatile components - import javax.swing.*;
If you want to display the "Clear" button to the user & do perform some action if the user clicks on it, you must do the following steps:
1. Create a JButton object 2. add a JButton object to a JPanel object 3. add a Listener object to the JButton, 4. write the code in actionPerformed to do what you want to do
Steps for setting up GUI
1. create container and assign layout manager 2. create components 3. add components to container 4. use container as the content pane of a window or applet
every container has:
1. default layout manager 2. instance method, setLayout(), that takes a parameter of type LayoutManager 3. add a component with the add() method
Steps to use Listener
1. put implements ActionListener on JPanel class definition 2. make component and add to panel in the constructor 3. attach a listener to component in constructor for the panel using addActionListener(this) 4. write action performed method in class for JPanel as "public void actionPerformed(ActionEvent e)"
4 main methods of JOptionPane
1. showConfirmDialog 2. showInputDialog 3. showMessageDialog 4. showOptionDialog
AWT stands for:
Abstract Windowing Toolkit
Which layout manager divides a container object into North, South, East, West and Center regions?
BorderLayout
BorderLayout positions
BorderLayout.CENTER BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.EAST BorderLayout.WEST
What is the default layout for JPanel?
FlowLayout
Font constructor
Font plainFont = new Font ("Serif", Font.BOLD, 12);
GUI
Graphical User Interface
how to create JFrame or JPanel
JFrame window = new JFrame("Title"); JPanel content = new JPanel();
What is the simplest type of component?
JLabel
I want to display a JButton to the user. As taught in class, I should add the JButton to a:
JPanel
Add components to BorderLayout
Suppose container = cntr, component = comp cntr.add(comp, BorderLayout.SOUTH);
If you want your program to pop up a JOptionPane dialog box when a user clicks a button, you put the code that pops up the window into which method?
actionPerformed
comp.setVisible(true) comp.setVisible(false)
called to hide or show component
BorderFactory.createMatteBorder(top, left, bottom, right)
can specify individual thickness for each individual edge
JOptionPane
class that provides basic built in windows so you don't have to build them yourself
BorderFactory.createEtchedBorder()
creates border that looks like groove etched around the component
BorderFactory.createTitledBorder(title)
creates border with title of type String, displayed in upper left corner
BorderFactory.createLineBorder(color, thickness)
draws a line around all four edges of a component
comp.getWidth() comp.getHeight()
functions that give current size of the component in pixels
BorderFactory.createLoweredBevelBorder()
gives 3D effect that makes it look lowered into the computer screen
layout manager
helps you layout the components you are adding to a container
What is the difference between objectname1.length & objectname2.length()?
length = length of an array called objectname1 length() = length of a String called objectname2
Specifying horizontal/vertical gaps in GridLayout
new GridLayout(R, C, H, V)
Specifying horizontal/vertical gaps in BorderLayout
panel.setLayout(new BorderLayout(5,7));
comp.setBackground(color) comp.setForeground(color)
sets background/foreground color
comp.setFont(font)
sets font used for text
pixel
smallest point addressable on a screen
Size settings
use setSize() if parent has no layout manager use setPreferredSize() if parent has layout manager
comp.setEnabled(true) comp.setEnabled(false)
used to enable and disable components