Docsity

Java GUI Components and Event Handling: Swing vs AWT, Slides of Network security

Partial preview of the text

Download Java GUI Components and Event Handling: Swing vs AWT and more Slides Network security in PDF only on Docsity!

Objectives

  • use an image viewer application to introduce Java's GUI features

11. Introducing

Java's GUI Features

Topics

    1. GUI Principles
    1. Background
    1. Three Steps to GUIs
    1. An Applicaton Window
    1. Adding Menus
    1. Event Handling
    1. ImageViewer as a Listener

continued Docsity.com

1. GUI Principles

listeners (waiting code)

events (info. objects) components

layout manager

1. GUI Principles

  • GUIs are built from components
    • buttons, menus, sliders, etc.
  • The positioning of GUI components in a

window is done with layout managers

  • User actions are converted into events
    • button presses, menu selections, etc.
  • Events are processed by listeners.

2.1. Swing and AWT

  • In older Java programs, the AWT (Abstract Window Toolkit) GUI components are used - Swing has replaced AWT GUIs - never mix Swing and AWT components in a single program
  • Some parts of AWT are still used
    • e.g. its layout managers (see later)

continued Docsity.com

use Swing

  • AWT supports heavyweight GUI components
    • each Java GUI component is actually a simple layer hiding the OSes GUI component - the OS component is called a peer component
    • e.g. on Windows, a Java button in AWT is implemented using a Windows' button

continued Docsity.com

2.2. Disadvantages of AWT

  • Java does not have complete control over the AWT GUI components - some AWT GUIs do not work well because of problems with the underlying OS (e.g. file choosing in Windows)
  • The "look and feel" of Java GUIs in AWT vary between OSes.

Some Java"Look and Feel"s

Using the Substance L&F add-on library (https://substance.dev.java.net/)

4. An Application Window

title

content pane

window controls

Implemented by subclassing JFrame

Coding an Application

public class ImageViewer extends JFrame { public ImageViewer() { super("ImageViewer 0.1");

// create the GUI Container c = getContentPane(); JLabel label = new JLabel ("I am a label. I can display some text."); c.add(label); :

JLabel is a GUI component

Version 0.

Sizing the Application

  • Replace setSize() by pack():

5. Adding Menus

  • JMenuBar
    • contains the menus
  • JMenu
    • contains the menu items
  • JMenuItem
    • individual items in a menu