GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated...

35
GUI

Transcript of GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated...

Page 1: 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.

GUI

Page 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.

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

Page 3: 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.

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.

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

BorderLayout Layouts

NORTH

WEST

SOUTH

CENTER

EAST

Page 8: 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.

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

Page 9: 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.

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);

Page 10: 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.

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 }}

Page 11: 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.

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.

Page 12: 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.

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.

Page 13: 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.

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..

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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()); } }} }

Page 17: 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.

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()

Page 18: 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.

(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.

Page 19: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 20: 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.

(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.

Page 21: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 22: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 23: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 24: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 25: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 26: 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.

• MouseTrakerFrame.java

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 27: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 28: 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.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 29: 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.

• MouseDetailsFrame.java

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 30: 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.

Examples

Page 31: 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.

• CheckBoxFrame.java

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 32: 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.

• RadioButtonFrame.java

Page 33: 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.

• ComboBoxFrame.java

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 34: 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.

• ListFrame.java

Page 35: 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.

MultiSelectionFrame.java

(C) 2010 Pearson Education, Inc. All rights reserved.