A Quick Java Swing Tutorial Introduction, Lecture notes of Java Programming

Swing Tutorial. 1. Introduction. • Swing – A set of GUI classes. • Part of the Java's standard library. • Much better than the previous library: AWT.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

unknown user
unknown user 🇬🇧

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
9/19/2018
1
A Quick Java
Swing Tutorial
1
Introduction
Swing – A set of GUI classes
Part of the Java's standard library
Much better than the previous library: AWT
Abstract Window Toolkit
Highlights
A rich set of widgets
Widget: Any GUI element (also called: components)
Contents and shape are separated (MVC support)
Fine-grained control over the behavior and look and feel
Platform independent
Isolates the programmer from the operating system's GUI
2
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download A Quick Java Swing Tutorial Introduction and more Lecture notes Java Programming in PDF only on Docsity!

A Quick Java

Swing Tutorial

1

Introduction

  • Swing – A set of GUI classes
    • Part of the Java's standard library
    • Much better than the previous library: AWT
      • Abstract Window Toolkit
  • Highlights
    • A rich set of widgets
      • Widget: Any GUI element (also called: components)
    • Contents and shape are separated (MVC support)
    • Fine-grained control over the behavior and look and feel
    • Platform independent
      • Isolates the programmer from the operating system's GUI 2

Swing Components

  • Containers
    • Contain and manage other components.
    • Top Level/Internal
    • Examples: JFrame (Top Level), JScrollPane, JPanel.
  • Basic controls
    • Atomic components
    • Used for showing ouput and/or getting some input
    • Inherits JComponent
    • Examples: JButton, JLabel, JTextArea, JTable, Jlist
  • Usually every Swing class extends the corresponding AWT class
    • For backward-compatibility reasons 3

My First Swing Program

import javax.swing.*; import java.awt.BorderLayout; public class First { public static void main(String[] args) { JFrame frame = new JFrame("My First Frame"); // operation to do when the window is closed. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(new JLabel("I Love Swing"), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } } 4

Top Level Containers: JFrame

  • javax.swing.JFrame:
    • Top-level window with a title and a border.
    • Usually used as a program's main window 7

More on JFrame

  • Made of several layers
  • Widgets are added to the Content Pane layer.
    • Use getContentPane() to obtain it
  • Other layers are used for customizing the window's appearence 8

Internal Containers

  • Not Top level containers
  • Can contain other non-top level components
  • Examples:

–JScrollPane: Provides a scrollable view of its

components

–JSplitPane: Separates two components

–JTabbedPane: User chooses which

component to see

9

Containers - Layout

  • Each container has a layout manager
    • Determines the size, location of contained widgets.
  • Setting the current layout of a container:

void setLayout(LayoutManager lm)

  • LayoutManager implementing classes:

–BorderLayout

–BoxLayout

–FlowLayout

–GridLayout

10

Swing Components

13

First Swing Program Revisited

import javax.swing.*; import java.awt.BorderLayout; public class First { public static void main(String[] args) { JFrame frame = new JFrame("My First Frame"); // operation to do when the window is closed. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(new JLabel("I Love Swing"), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } } 14 Create a frame Create a text label Add the label to the content pane Choose the border layout Specify CENTER as the layout position

Interface

  • Interface – similar to Abstract class
  • No methods are implemented
  • Syntax: public interface MyInterface{ public String name=“hello”; public void sayHello(); }

Implementing an Interface

  • Before using an interface, it needs to be implemented
  • Why do we need interfaces?
    • A contract that guarantees that listed methods will be implemented
    • Helps to ‘generalize’ code
  • Syntax: public class MyInterfaceImpl implements MyInterface{ public void sayHello(){…} }