Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 GUI

2 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

3 Types of Layout Managers ManagerDescription java.awt.BorderLayoutArranges elements along the north, south, east, west, and in the center of the container. java.swing.BoxLayoutArranges elements in a single row or single column. java.awt.CardLayoutArranges elements like a stack of cards, with one visible at a time. java.awt.FlowLayoutArranges elements left to right across the container. java.awt.GridBagLayoutArranges elements in a grid of variable sized cells (complicated). java.awt.GridLayoutArranges elements into a two-dimensional grid of equally sized cells. java.swing.OverlayLayoutArranges elements on top of each other.

4 Default Layout Managers ContainerLayout Manager JAppletBorderLayout (on its content pane) JBoxBoxLayout JDialogBorderLayout (on its content pane) JFrameBorderLayout (on its content pane) JPanelFlowLayout JWindowBorderLayout (on its content pane) In AWT, the default layout for applets was FlowLayout. Top-level windows use BorderLayout

5 GUI Layout Basic Layout: Flow layout Border layout Grid layout Box layout Layout Process: 1.Create frame (Jframe) or panel (JPanel) 2.Layout the panel or frame 3.Create the components 4.Place the components on the container 5.You may create more complex GUI by placing panel(s) on the Frame 6.Size and display the Frame

6 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

7 BorderLayout Layouts NORTH WEST SOUTH CENTER EAST

8 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); Layouts BorderLayout Layouts

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

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

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

12 Event Classes ComponentsEventsDescription Button, JButtonActionEventUser clicked button CheckBox, JCheckBoxItemEventUser toggled a checkbox CheckboxMenuItem, JCheckboxMenuItemItemEventUser toggled a checkbox Choice, JPopupMenuItemEventUser selected a choice Component, JComponentComponentEventComponent was moved or resized FocusEventComponent acquired or lost focus KeyEventUser typed a key MouseEventUser manipulated the mouse Container, JContainerContainerEventComponent added/removed from container List, JListActionEventUser double-clicked a list item ItemEventUser clicked a list item Menu, JMenuActionEventUser selected menu item Scrollbar, JScrollbarAdjustmentEventUser moved scrollbar TextComponent, JTextComponentTextEventUser edited text TextField, JTextFieldActionEventUser typed Enter key Window, JWindowWindowEventUser manipulated window AWT events for each type of component. AWT events for each type of component.

13 New Swing Event Classes ComponentEvents Description JPopupMenuPopupMenuEvent User selected a choice JComponentAncestorEvent An event occurred in an ancestor JListListSelectionEvent User double-clicked a list item ListDataEvent List's contents were changed JMenuMenuEvent User selected menu item JTextComponentCaretEvent Mouse clicked in text UndoableEditEvent An undoable edit has occurred JTableTableModelEvent Items added/removed from table TableColumnModelEvent A table column was moved JTreeTreeModelEvent Items added/removed from tree TreeSelectionEvent User selected a tree node TreeExpansionEvent User changed tree node JWindowWindowEvent User manipulated window Newly defined Swing events. Newly defined Swing events.

14 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

15 ButtonsTest.java (Cont.) public void actionPerformed(ActionEvent evt){ Object source = evt.getSource(); // process event Object 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; setBackground(color); setBackground(color); repaint(); repaint(); }} Generic callback in ActionListener

16 class ButtonFrame extends JFrame { 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()); } } } }

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

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

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

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

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

22

23

24

25

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

27

28

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

30 Examples

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

32 RadioButtonFrame.java

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

34 ListFrame.java

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


Download ppt "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."

Similar presentations


Ads by Google