Java Swing in 2 hours - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Java Swing in 2 hours

Description:

a 'Customized' Component. ButtonExample2.java. Some more Widgets. JPanel ... Often, you will be able to customise an existing Swing component to do a job for ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 37
Provided by: yg67
Category:

less

Transcript and Presenter's Notes

Title: Java Swing in 2 hours


1
Java Swing (in 2 hours)
  • Part 1

2
Some numbers
  • 14 packages related to UI
  • 451 classes at java 1.4 (and counting!)
  • 2 hours
  • 15 sec / class

3
What well cover this session
  • What is a GUI
  • What is Swing ( some history)
  • Some basic Swing concepts
  • How to create GUIs (that dont do anything)
  • Code samples
  • Note about code samples I might use many classes
    in the same file. This is usually NOT the good
    way to do it.

4
What is a GUI?
5
What is Swing
  • Timeline
  • 1930- Artie Shaw, Benny Goodman, Glen Miller
    and many others In the mood, etc.
  • Java 1.0 AWT (1996)
  • Java 1.2 Swing (1998)
  • Swing keeps growing, AWT is mostly deprecated.

6
Swing vs. AWT
  • AWT abstract windows toolkit (cross platform)
  • AWT earliest version of Java GUI
  • AWT components are heavyweight
  • Most Swing components are 'lightweight'
  • Do not mix AWT and Swing
  • Use Swing

7
Some Basic Swing Concepts(a REALLY shallow
overview)
  • Every program has at least one Top-Level
    container (JFrame, JWindow, JApplet)
  • The Top-Level container contains JComponents
    (JLabel, JButton..). Some components are used as
    Containers for other components (JPanel).
  • (J)Component has methods, and can handle events.

8
GUI Elements
  • Component ? items displayed (widgets)
  • Container ? region containing widgets
  • Layout ? arrangement of container
  • Event ? interactions for GUI

9
Smallest Swing Program
  • SimplestSwing.java
  • SaferSimplestSwing.java
  • BetterSimplestSwing.java

10
Whats in a JFrame?
  • Title
  • MenuBar
  • ContentPane
  • Most of the action is at the ContentPane
  • use getContentPane() and add elements to it.

11
JPanel
  • Is a container for other elements.
  • Usually (for now)
  • Create a JFrame
  • Create a JPanel. Add widgets to the JPanel.
  • Add the JPanel to the JFrame content pane.
  • ButtonExample.java

12
a Customized Component
  • ButtonExample2.java

13
Some more Widgets
  • JPanel
  • JButton, JLabel
  • TextField
  • TextArea
  • getText(), setText()
  • SomeWidgets.java

14
Some more Widgets
  • TextArea grows
  • Use JScrollPane
  • panel.add(new JScrollPane(textarea))
  • You can wrap almost any component in a
    JScrollPane..

15
Layout Managers
  • Who decides on the components placement and size?
  • A Layout Manager
  • JPanels default FlowLayout
  • Other layouts
  • BorderLayout (ContentPanes default)
  • GridLayout
  • BoxLayout, GridBagLayout, SpringLayout

16
Setting a Layout
  • JPanel panel new JPanel(new BorderLayout())
  • or
  • JPanel panel new JPanel()
  • panel.setLayout(new GridLayout(4,3,5,5))

17
Flow Layout
  • Components are added one after the other, and
    flow to the next line if needed.
  • It wont resize the container

18
BorderLayout
  • Container is splitted into 5 sections NORTH,
    SOUTH, EAST, WEST, CENTER
  • The central component is stretched.

19
BorderLayout, cont.
  • There can be only one component in each section.
  • This is usually used to add a toolbar at the
    size of the screen, and a main working area at
    the center.

20
BorderLayoutExample
  • BorderLayoutExample.java

21
GridLayout
  • GridLayout is a simple table with a fixed number
    of cells, with all columns and rows equally
    sized.

22
Combinations
  • You can ofcourse combine layouts --
  • JPanel with BorderLayout that contain
    twoJPanels, one of them with GridLayout and the
    other with FlowLayout, etc.

23
Play with it
  • Simple exercise for home create a calculator
    screen

24
Some more widgets
  • JCheckBox , JRadioButton
  • like JButton, inherit from JAbstractButton
  • Learn to use them.

25
Others you might want to look at
  • JSplitPane
  • JTabbedPane
  • JMenuBar
  • JMenu
  • JMenuItem
  • JToolTip
  • JComboBox
  • JList
  • JSlider
  • JSpinner

26
ImageIcons
  • The easiest way to add graphics and images.
  • ImageIcons can appear on a JLabel or a JButton,
    with or without additional text.
  • ImageIconsExample.java
  • BetterImageIconsExample.java

27
Drawing your own graphics
  • Every component has a paint() method thats
    responsible for drawing it.
  • In principle, you can just override it and do
    your own painting.
  • BUT there are many pitfalls its hard to get it
    right. We wont even try..

28
Icons to the rescue
  • ImageIcon implements the Icon interface.
  • JButton, JLabel can take any Icon.
  • We can create our own custom Icons, and draw on
    them.

29
The Icon Interface
30
  • This is called whenever the container of the icon
    paints itself.
  • c the containing component.
  • x, y the location on the containing component.
  • g a Graphics object, that we can draw on.

31
Graphics
  • Graphics abstract class, supporting (among
    others)
  • drawLine(int x1, int y1, int x2, int y2)
  • drawOval(int x, int y, int width, int height)
  • fillOval(int x, int y, int width, int height)
  • drawArc(int x, int y, int width, int height,
    int startAngle,
  • int arcAngle)
  • setColor(Color c)
  • setFont(Font f)
  • drawString(String s, int x, int y)

32
Custom Icon example
  • CustomIconExample.java

33
Remember this about Containers
  • The structure of containers is your design
    decision and should always be thought through in
    advance
  • particularly for managing components
  • nesting containers
  • Failure to do so usually either results in a
    messy interface, messy code or both.

34
Remember this about Components
  • There are many components that make your job much
    easier.
  • Often, you will be able to customise an existing
    Swing component to do a job for you, instead of
    having to start from scratch

35
How to Learn Swing
  • Dont even try.
  • (to know all of it)
  • Learn general framework principles and design
    styles.
  • Then use the API reference, and Swing Tutorials
    to discover detailed usage of each component.

36
Where to get more info
  • Books (but you can do without it)
  • Internet Ill put some good links on the course
    web site (under links).
  • TRY TO DO STUFF!
Write a Comment
User Comments (0)
About PowerShow.com