Transcript GUI 2

GUI
Swing Programs
• Four basic types of Top Level Window
– JFrame, a top level window decorated like a native
window
– JWindow, an undecorated stand-alone window (splashscreen)
– JApplet, an embeddable applet
– JDialog, a popup dialog window
• Each program type is implemented as a framework
class
Types of Layout Managers
Manager
java.awt.BorderLayout
Description
Arranges elements along the north, south,
east, west, and in the center of the container.
java.swing.BoxLayout
Arranges elements in a single row or single
column.
java.awt.CardLayout
Arranges elements like a stack of cards, with
one visible at a time.
java.awt.FlowLayout
Arranges elements left to right across the
container.
java.awt.GridBagLayout
Arranges elements in a grid of variable sized
cells (complicated).
java.awt.GridLayout
Arranges elements into a two-dimensional grid
of equally sized cells.
java.swing.OverlayLayout Arranges elements on top of each other.
Default Layout Managers
In AWT, the default layout
for applets was
FlowLayout.
Container
JApplet
JBox
JDialog
JFrame
JPanel
JWindow
Layout Manager
BorderLayout (on its content pane)
BoxLayout
BorderLayout (on its content pane)
BorderLayout (on its content pane)
FlowLayout
BorderLayout (on its content pane)
Top-level
windows use
BorderLayout
Basic Layout:
GUI Layout
• Flow layout
• Border layout
• Grid layout
• Box layout
Layout Process:
1.
2.
3.
4.
5.
6.
Create frame (Jframe) or panel (JPanel)
Layout the panel or frame
Create the components
Place the components on the container
You may create more complex GUI by placing panel(s) on the Frame
Size and display the Frame
FlowLayout Example
frame
5.
5.
5.
5.
5.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JButton("one"));
panel.add(new JButton("two"));
panel.add(new JButton("three"));
panel.add(new JButton("four"));
panel.add(new JButton("five"));
frame.getContentPane().add(panel);
frame.setSize(100,200);
frame.setvisible(true);
+
panel
=
1.
2.
3.
4 &
4 &
4 &
4 &
4 &
6.
7.
7.
BorderLayout Layouts
NORTH
EAST
WEST
SOUTH
CENTER
BorderLayout Layouts
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JButton("one"),BorderLayout.NORTH);
panel.add(new JButton("two"),BorderLayout.SOUTH);
panel.add(new JButton("three"),BorderLayout.EAST);
panel.add(new JButton("four"), BorderLayout.WEST);
panel.add(new JButton("five"), BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.setSize(250,300);
frame.setvisible(true);
Grid Layout
…
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
panel.add(new JButton("one"));
panel.add(new JButton("two"));
panel.add(new JButton("three"));
panel.add(new JButton("four"));
panel.add(new JButton("five"));
frame.add(panel);
frame.setSize(100,200);
frame.setvisible(true);
GridLayout(3,2,5,7);
Code Structure for Event Handling
class Event_Listener_Class implements some_Listener_interface{
public … method(…){
//code here says what to do if button is pressed
}
}
public class GUI{
//code to produce the GUI
//Here we will
1. Create a Event_Listener_Object
2. Register it with a component – so when the component generates
some event, the listener is informed.
}
Event Handling
When an action is performed (like a button is pressed) an Event Object is
created
AND
A method is called
This method is found inside a “Listener” class/object
In order for the correct method to be called, the litener object must be
register to the component.
Event Classes

AWT events for each type of component.
Components
Events
Description
Button, JButton
CheckBox, JCheckBox
CheckboxMenuItem,
JCheckboxMenuItem
Choice, JPopupMenu
Component, JComponent
ActionEvent
ItemEvent
User clicked button
User toggled a checkbox
ItemEvent
User toggled a checkbox
ItemEvent
User selected a choice
ComponentEvent Component was moved or resized
FocusEvent
Component acquired or lost focus
KeyEvent
User typed a key
MouseEvent
User manipulated the mouse
Container, JContainer
ContainerEvent Component added/removed from container
List, JList
ActionEvent
User double-clicked a list item
ItemEvent
User clicked a list item
Menu, JMenu
ActionEvent
User selected menu item
Scrollbar, JScrollbar
AdjustmentEvent User moved scrollbar
TextComponent, JTextComponent TextEvent User edited text
TextField, JTextField
ActionEvent
User typed Enter key
Window, JWindow
WindowEvent
User manipulated window
New Swing Event Classes

Newly defined Swing events.
Component
Events
JPopupMenu
JComponent
JList
PopupMenuEvent
User selected a choice
AncestorEvent
An event occurred in an ancestor
ListSelectionEvent
User double-clicked a list item
ListDataEvent
List's contents were changed
MenuEvent
User selected menu item
CaretEvent
Mouse clicked in text
UndoableEditEvent
An undoable edit has occurred
TableModelEvent
Items added/removed from table
TableColumnModelEvent A table column was moved
TreeModelEvent
Items added/removed from tree
TreeSelectionEvent
User selected a tree node
TreeExpansionEvent
User changed tree node
WindowEvent
User manipulated window
JMenu
JTextComponent
JTable
JTree
JWindow
Description
ButtonsTest.java
class ButtonPanel extends JPanel implements ActionListener
{
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
// constructor
public ButtonPanel() {
yellowButton = new JButton("Yellow");
blueButton = new JButton("Blue");
redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
yellowButton.addActionListener(this);
blueButton. addActionListener(this);
Generic
redButton. addActionListener(this);
}
Listener
ButtonsTest.java (Cont.)
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource(); // process event
Color color = getBackground();
if (source == yellowButton) color = Color.yellow;
Generic callback in
else if (source == blueButton) color = Color.blue;
ActionListener
else if (source == redButton) color = Color.red;
setBackground(color);
repaint();
}
}
class ButtonFrame extends JFrame
{ public ButtonFrame()
{ setTitle("ButtonTest");
setSize(300, 200);
setVisible (true);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0); }
} );
add(new ButtonPanel());
}
}
Anonymous Inner Classes
 Digression
 Nested Class – on the fly
 Need an object of a particular type exactly once, as a
parameter
 The example is an anonymous subclass of
WindowAdapter
– Overriding windowClosing()
• Many event-listener interfaces contain multiple methods.
• An adapter class implements an interface and provides a
default implementation (with an empty method body) of
each method in the interface.
• You extend an adapter class to inherit the default
implementation of every method and override only the
method(s) you need for event handling.
(C) 2010 Pearson Education, Inc. All rights
reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
• Interface MouseWheelListener enables
applications to respond to the rotation of a
mouse wheel.
• Method mouseWheelMoved receives a
MouseWheelEvent as its argument.
• Class MouseWheelEvent (a subclass of
Mouse-Event) contains methods that enable
the event handler to obtain information about
the amount of wheel rotation.
(C) 2010 Pearson Education, Inc. All rights
reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
• MouseTrakerFrame.java
(C) 2010 Pearson Education, Inc. All rights
reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
(C) 2010 Pearson Education, Inc. All
rights reserved.
• MouseDetailsFrame.java
(C) 2010 Pearson Education, Inc. All rights
reserved.
Examples
• CheckBoxFrame.java
(C) 2010 Pearson Education, Inc. All rights
reserved.
• RadioButtonFrame.java
• ComboBoxFrame.java
(C) 2010 Pearson Education, Inc. All rights
reserved.
• ListFrame.java
MultiSelectionFrame.java
(C) 2010 Pearson Education, Inc. All rights
reserved.