Swing part 1 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Swing part 1

Description:

Widget's state properties. Visible or not. Enabled / disabled ... But usually the widget will handle those. Key events respond to key pressing ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 22
Provided by: csBg
Category:

less

Transcript and Presenter's Notes

Title: Swing part 1


1
Swing part 1
  • Examples using
  • Basic widgets and features

2
How to learn new a widget
  • How does it looks?
  • The Swing tutorial has a visual index
  • What does it do?
  • What properties does it have?
  • What meaningful events does if fire?
  • What data does is supplies?
  • Data properties, like value or text

3
Hierarchy of Swing p1
Component
Container
Window
JComponent
Dialog
Frame
JPanel
JFrame
JDialog
JWindow
JApplet
4
Hierarchy of Swing p2
JComponent
JTextComponent
JComboBox
JButton
JCheckBox
JLabel
JTextField
JTextArea
JList
JScrollBar
5
The JComponent class
  • Root class for Swings widgets (except top-level)
  • Based on AWT container
  • A simple Swing widget is a composite AWT widget
  • But it doesnt mean you should exploit this!
  • Supports basic properties and events
  • Widgets state
  • Basic appearance
  • Layout properties
  • Mouse and key events

6
Widgets state properties
  • Visible or not
  • Enabled / disabled
  • Disabled widgets do not respond and are grayed
    out
  • Tool tip text
  • Important for helping the user
  • Name
  • Each widget has a name which identifies it within
    the containing widget

7
Basic appearance properties
  • Foreground and background color
  • Mostly handled by the look feel
  • Opaque or transparent
  • Font (for text)
  • Border
  • Borders can surrounds all widgets
  • Useful mainly for panels
  • To be mentioned in details later

8
Basic layout properties
  • Usually the laying out will be done by the layout
    manager of the containing widget
  • The programmer can supply tips for the layout
  • Minimum and maximum size
  • Preferred size
  • X and Y alignment
  • Most widgets (buttons, labels, toolbars...) will
    handle size automatically

9
Common events
  • All widgets support mouse and key events
  • Mouse event occurs in respond to mouse pointer
    actions
  • Motions
  • Clicks
  • But usually the widget will handle those
  • Key events respond to key pressing
  • Used for key accelerators

10
Example One Your First Swing Program
  • We will write simple program HelloWorldSwing
  • And here's the full code for HelloWorldSwing
  • import javax.swing.
  • public class HelloWorldSwing
  • / Create the GUI and show it. For thread
    safety,this method should be invoked from the
    event-dispatching thread. /

11
  • private static void createAndShowGUI()
  • //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true)
    //Create and set up the window.
  • JFrame frame new JFrame("HelloWorldSwing")
  • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
    E)
  • //Add the ubiquitous "Hello World" label.
  • JLabel label new JLabel("Hello World")
    frame.getContentPane().add(label)
  • //Display the window.
  • frame.pack()
  • frame.setVisible(true)

12
Main function
  • public static void main(String args)
  • createAndShowGUI()

Basic code of every swing program 1.Import the
pertinent packages. 2.Set up a top-level
container. 3.Display the container.
13
Thread envelope
  • javax.swing.SwingUtilities.invokeLater(new
    Runnable()
  • public void run()
  • / create and show the GUI / )

14
Example Two SwingApplication
  • Topics illustrated in this example
  • Look and Feel
  • Setting Up Buttons and Labels
  • Adding Components to Containers
  • Adding Borders Around Components
  • Handling Events

15
Example Two SwingApplication
Each time the user clicks the button (JButton),
the label (JLabel) is updated.
JButton
JLabel
16
Setting Up Buttons and Labels
  • JButton button new JButton("I'm a Swing
    button!")
  • button.setMnemonic('i')
  • button.addActionListener(/...create an action
    listener.../)
  • ...// where instance variables are declared
  • private static String labelPrefix "Number of
    button clicks "
  • private int numClicks 0
  • ...// in GUI initialization code
  • final JLabel label new JLabel(labelPrefix "0
    ")
  • ...// in the event handler for button clicks
  • label.setText(labelPrefix numClicks)

17
Look and Feel
GTK look and feel
Java look and feel
Mac OS look and feel
Windows look and feel
18
Look and Feel code
  • String lookAndFeel null
  • lookAndFeel UIManager.getCrossPlatformLookAndFe
    elClassName()
  • try UIManager.setLookAndFeel(lookAndFeel)
    catch (Exception e)

19
Adding Components to Containers
  • JPanel panel new JPanel(new GridLayout (0,1))
  • panel.add(button)
  • panel.add(label)
  • panel.setBorder(BorderFactory.createEmptyBord(
  • 30, //top
  • 30, //left
  • 10, //bottom
  • 30) //right ))

20
Handling Events
  • public class SwingApplication implements
    ActionListener
  • JButton button new JButton("I'm a Swing
    button!")
  • button.addActionListener(this)
  • public void actionPerformed(ActionEvent e)
    numClicks
  • label.setText(labelPrefix numClicks)

21
Example Three CelsiusConverter
  • JTextField tempCelsius null
  • tempCelsius new JTextField(5)
  • convertTemp.addActionListener(this)
  • tempCelsius.addActionListener(this)
  • public void actionPerformed(ActionEvent event)
    //Parse degrees Celsius as a double and convert
    to Fahrenheit.
  • int tempFahr
  • (int)((Double.parseDouble(tempCelsius.getText()))
    1.8 32)
  • fahrenheitLabel.setText(tempFahr "
    Fahrenheit")

Write a Comment
User Comments (0)
About PowerShow.com