GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated...
-
Upload
henry-slack -
Category
Documents
-
view
216 -
download
0
Transcript of GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated...
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 (splash-
screen)– JApplet, an embeddable applet– JDialog, a popup dialog window
• Each program type is implemented as a framework class
Types of Layout Managers
Manager Descriptionjava.awt.BorderLayout 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
Container Layout ManagerJApplet BorderLayout (on its content pane)JBox BoxLayoutJDialog BorderLayout (on its content pane)JFrame BorderLayout (on its content pane)JPanel FlowLayoutJWindow BorderLayout (on its content pane)
In AWT, the default layout for applets was
FlowLayout.
Top-level windows use BorderLayout
GUI LayoutBasic Layout:
• Flow layout• Border layout• Grid layout• Box layout
Layout Process:
1. Create frame (Jframe) or panel (JPanel)2. Layout the panel or frame3. Create the components4. Place the components on the container5. You may create more complex GUI by placing panel(s) on the Frame6. Size and display the Frame
1. JFrame frame = new JFrame();
2. JPanel panel = new JPanel();
3. panel.setLayout(new FlowLayout());
4 & 5. panel.add(new JButton("one"));
4 & 5. panel.add(new JButton("two"));
4 & 5. panel.add(new JButton("three"));
4 & 5. panel.add(new JButton("four"));
4 & 5. panel.add(new JButton("five"));
6. frame.getContentPane().add(panel);
7. frame.setSize(100,200);
7. frame.setvisible(true);
+=
frame
panel
FlowLayout Example
BorderLayout Layouts
NORTH
WEST
SOUTH
CENTER
EAST
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);
BorderLayout Layouts Layouts
…
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);
Grid Layout
GridLayout(3,2,5,7);
Code Structure for Event Handling
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.}
class Event_Listener_Class implements some_Listener_interface{ public … method(…){ //code here says what to do if button is pressed }}
Event Handling
When an action is performed (like a button is pressed) an Event Object is createdAND 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
Components Events DescriptionButton, JButton ActionEvent User clicked buttonCheckBox, JCheckBox ItemEvent User toggled a checkboxCheckboxMenuItem, JCheckboxMenuItem ItemEvent User toggled a checkboxChoice, JPopupMenu ItemEvent User selected a choiceComponent, JComponent ComponentEvent Component was moved or resized FocusEvent Component acquired or lost focus KeyEvent User typed a key MouseEvent User manipulated the mouseContainer, JContainer ContainerEvent Component added/removed from containerList, JList ActionEvent User double-clicked a list item ItemEvent User clicked a list itemMenu, JMenu ActionEvent User selected menu itemScrollbar, JScrollbar AdjustmentEvent User moved scrollbarTextComponent, JTextComponent TextEvent User edited textTextField, JTextField ActionEvent User typed Enter keyWindow, JWindow WindowEvent User manipulated window
AWT events for each type of component.AWT events for each type of component.
New Swing Event Classes
Component Events DescriptionJPopupMenu PopupMenuEvent User selected a choiceJComponent AncestorEvent An event occurred in an ancestorJList ListSelectionEvent User double-clicked a list item ListDataEvent List's contents were changedJMenu MenuEvent User selected menu itemJTextComponent CaretEvent Mouse clicked in text UndoableEditEvent An undoable edit has occurredJTable TableModelEvent Items added/removed from table TableColumnModelEvent A table column was movedJTree TreeModelEvent Items added/removed from tree TreeSelectionEvent User selected a tree node TreeExpansionEvent User changed tree nodeJWindow WindowEvent User manipulated window
Newly defined Swing eventsNewly defined Swing events..
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); redButton. addActionListener(this); }
Generic Listener
ButtonsTest.java (Cont.)
public void public void actionPerformedactionPerformed((ActionEventActionEvent evt){ evt){ Object source = evt.getSource(); // process eventObject source = evt.getSource(); // process event Color color = getBackground();Color color = getBackground(); if (source == yellowButton) color = Color.yellow;if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue;else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red;else if (source == redButton) color = Color.red; setBackgroundsetBackground(color);(color); repaint();repaint(); } } }}
Generic callback in ActionListener
class ButtonFrame extends JFrameclass ButtonFrame extends JFrame{ public ButtonFrame(){ public ButtonFrame() { setTitle("ButtonTest");{ setTitle("ButtonTest"); setSize(300, 200);setSize(300, 200); setVisible (true);setVisible (true); addWindowListener(new WindowAdapter()addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ public void windowClosing(WindowEvent e) { System.exit(0); }{ System.exit(0); } } );} ); add(new ButtonPanel());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()
(C) 2010 Pearson Education, Inc. All rights reserved.
• 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.
• 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.