Outline - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Outline

Description:

... a container. Handle automatic resizing of the container ... Set up a top level container (either JFrame or JDialog) Set up components to comprise your GUI ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: cs11
Category:

less

Transcript and Presenter's Notes

Title: Outline


1
Outline
  • Swing and AWT
  • Swing basics
  • Layout Managers
  • Stand alone GUI applications
  • Applets
  • Misc

2
AWT and Swing
  • Basic GUI elements are implemented in two
    packages java.awt, and javax.swing
  • Abstract Windowing Toolkit (AWT)
  • Set of GUI components introduced in JDK 1.0
  • Depends on native GUI
  • Swing
  • Largely supersedes AWT, but depends of AWT
    classes
  • Implemented in Java
  • Applications look exactly the same on different
    platforms
  • Not as fast as AWT
  • Pluggable look-and-feel

3
Swing
  • Stand alone applications and applets
  • Various components
  • Buttons
  • Menus
  • Dialogs
  • Trees
  • Progress bars
  • Sliders
  • Split panes
  • Tabbed panes
  • Etc. etc. etc.

4
Inheritance hierarchy JLabel
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JLabel
  • Component defines basic properties of
    components
  • Container a component that can contain other
    components

5
Containment hierarchy
  • Top level container
  • Each GUI has a container at the top of the
    containment hierarchy
  • Applications JFrame or JDialog
  • Applets JApplet
  • Intermediate-level containers
  • Contain other components and/or containers
  • E.g. JScrollPane or JSplitPane
  • Atomic components
  • E.g. JButton
  • Other
  • E.g. JTree

6
Containment hierarchy example
  • JFrame

JPanel
JButton
JButton
JLabel
7
Containment hierarchy how?
  • Create components
  • Add components to a container using one of the
    various forms of the add method
  • Example
  • JPanel buttonPanel new JPanel()
  • JButton helpButton new JButton(Help)
  • JButton quitButton new JButton(Quit)
  • JLabel titleLabel new JLabel(GUI
    Demo)buttonPanel.add(helpButton)
  • buttonPanel.add(quitButton)
  • buttonPanel.add(titleLabel)

8
Layout Managers
  • Determine the size and position of the components
    within a container
  • Handle automatic resizing of the container
  • Add components with the containers layout
    manager in mind
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout (advanced)
  • Others

9
Flow Layout
  • Places things one after another, in order added
    to the container
  • Container c getContentPane()
  • c.setLayout(new FlowLayout())
  • c.add(new JButton(OK))
  • c.add(new JButton(Open))
  • c.add(new JButton(Close))

10
Border Layout
  • Places along border, or in center
  • Container c getContentPane()
  • c.setLayout(new BorderLayout())
  • c.add("North", new Button("North"))
  • c.add("South", new Button("South"))
  • c.add("East", new Button("East"))
  • c.add("West", new Button("West"))
  • c.add("Center", new Button("Center"))

11
Grid Layout
  • Lays out in a user-specified grid
  • Container c getContentPane()
  • c.setLayout(new GridLayout(3,2))
  • c.add(new Button("1"))
  • c.add(new Button("2"))
  • c.add(new Button("3"))
  • c.add(new Button("4"))
  • c.add(new Button("5"))
  • c.add(new Button("6"))

12
Swing Applications
  • Set up a top level container (either JFrame or
    JDialog)
  • Set up components to comprise your GUI
  • Add components to containers
  • Handle events (next class)

13
Swing Applications (Example)
  • import javax.swing.
  • import java.awt.
  • public class SimpleSwing extends JFrame
  • public SimpleSwing()
  • // Get, configure, and populate the root
    container
  • Container container this.getContentPane()
  • container.setLayout(new BorderLayout())
  • container.add("North", createCoordPanel())
  • container.add("South", createButtonPanel())
  • container.add("West", createColorShapePanel())
  • public static void main(String args)
  • SimpleSwing frame new SimpleSwing()

14
Swing Applications (Example)
/ Create and populate the co-ordinate panel.
/ protected JPanel createCoordPanel()
// Create the co-ordinate panel JPanel
coordPanel new JPanel() // Populate it
with labels and text fields
coordPanel.add(new JLabel("x1", Label.LEFT))
JTextField textX1 new JTextField(2)
coordPanel.add(textX1) coordPanel.add(new
JLabel("y1", Label.LEFT)) JTextField textY1
new JTextField(2) coordPanel.add(textY1)
coordPanel.add(new JLabel("x2", Label.LEFT))
JTextField textX2 new JTextField(2)
coordPanel.add(textX2) coordPanel.add(new
JLabel("y2", Label.LEFT)) JTextField textY2
new JTextField(2) coordPanel.add(textY2)
return coordPanel

15
Swing Applications (Example)
  • / Creates the panel containing the "Draw"
    button /
  • protected JPanel createButtonPanel()
  • // Create the button panel
  • JPanel buttonPanel new JPanel()
  • // Create the draw button
  • JButton drawButton new JButton("Draw")
  • // Add the button to the panel
  • buttonPanel.add(drawButton)
  • return buttonPanel

16
Swing Applications (Example)

/ Creates the color/shape panel /
protected JPanel createColorShapePanel()
// Create and configure the panel JPanel
colorShapePanel new JPanel()
colorShapePanel.setLayout(new BorderLayout())
// Create and populate the two ComboBoxes
String colors "Red", "Yellow", "Green",
"Blue", "Black", "White" JComboBox
colorList new JComboBox(colors)
colorList.setSelectedIndex(0) String
shapes "Circle", "Rectangle", "Line"
JComboBox shapeList new JComboBox(shapes)
shapeList.setSelectedIndex(0) // Add them
to the panel colorShapePanel.add("North",
colorList) colorShapePanel.add("South",
shapeList) return colorShapePanel
17
Swing Applications (Example)
  • This example wont interact with the user
  • Handling interaction-next class

18
Swing Applets
  • Top level container is JApplet
  • An interface to allow small Java programs run in
    a browser
  • init() called when an applet is first loaded
  • start() whenever the page the applet is on is
    viewed or revisited
  • stop() whenever the user leaves the page
  • destroy() called after stop() when browser is
    shut down
  • No need to define a main method

19
Swing Applets (Example)
  • import java.awt.
  • import javax.swing.
  • public class MyApplet extends JApplet
  • public void init()
  • Container pane getContentPane()
  • JLabel label new JLabel("Hello Swing
    applet world!")
  • label.setHorizontalAlignment(JLabel.CENTER
    )
  • appletPane.add(label, BorderLayout.CENTER)

20
Viewing Applets
  • Requires some special HTML code
  • HTML - generic language of web browsers
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height100gt
  • lt/appletgt
  • lt/htmlgt
  • Save in file MyAppletTest.html
  • Use Appletviewer to view
  • appletviewer MyAppletTest.html

To put in an existing page, only these two middle
lines are needed
21
Misc Components Color and Font
  • Color
  • jcomponent.setBackground(Color r)
  • jcomponent.setForeground(Color r)
  • Color.yellow, Color.red, etc.
  • Font
  • Font f new Font(Serif, Font.BOLD, 14)
  • jcomponent.setFont(f)

22
Misc Custom Painting
  • The Graphics Class
  • Controls how information is drawn
  • Every container or component has a graphics
    context (instance of Graphics) representing its
    on-screen real estate
  • To update screen, paintComponent is called and
    passed the Graphics object overriding this in a
    JComponent subclass lets you create custom widgets

23
Misc Custom Painting (contd)
  • Some methods of the Graphics class
  • setColor(Color r)
  • setFont(Font f)
  • drawString(String s, int x, int y)
  • drawLine(int x1, int y1, int x2, int y2)
  • drawRect(int x, int y, int width, int height)
  • fillRect(int x, int y, int width, int height)
  • drawPolygon(Polygon p)
  • etc.

24
Misc Custom Painting (example)
  • import javax.swing.
  • import java.awt.
  • public class Welcome extends JApplet
  • public void paintComponent(Graphics g)
  • Polygon p1 new Polygon()
  • p1.addPoint(100, 200)
  • p1.addPoint(200, 300)
  • p1.addPoint(13, 243)
  • g.drawPolygon(p1) // draws outline
  • g.fillPolygon(p1) // fills in
  • g.drawString(Welcome to Java Programmin)
  • showStatus(My first Java Program)

25
Summary
  • Swing and AWT are designed as a large
    hierarchical library of components that can be
    put together to build a GUI
  • Components are created and added to a container.
    Their location and size within a container are
    managed by the containers layout manager
  • Applets define an interface for viewing Java
    programs in a web browser

26
Next Class
  • More GUI Programming
  • Event handling
  • MP4 due soon, MP5 is out soon
Write a Comment
User Comments (0)
About PowerShow.com