Java GUI - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Java GUI

Description:

Every JFrame object has a 'content pane' associated with it. ... When we design GUI programs, there are three packages we will need to import: java.awt. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 16
Provided by: peopl83
Category:
Tags: gui | java | threepane

less

Transcript and Presenter's Notes

Title: Java GUI


1
Java GUI

2
Graphical User Interface (GUI)
a text field
a label
a button
checkbox
combo box
a list
3
Graphical User Interfaces
  • GUIs, AWT, and Swing
  • AWT (Abstract Window Toolkit) is the original
    Java classes used for GUI development.
  • Java 2 includes an improved toolkit, named Swing,
    to improve on AWT.
  • Swing components are easier to use, more
    portable, and provide more functionality than
    older AWT components.

4
Inheritance Helps
  • Since Swing was designed as an improvement of
    AWT, it made sense not to start from scratch when
    designing the Swing components.
  • To differentiate them, Swing components have
    names that begin with the letter J.

5
Graphical User Interfaces
  • GUIs and the Java Swing components
  • JComponent is the ancestor of many Swing classes.
    Here are some descendants of the Jcomponent
    class.
  • JButton
  • JPanel
  • JLabel
  • JTextArea

6
Windows
  • We will use the class JFrame to create GUI
    windows.
  • javax.swing.JFrame inherits from java.awt.Frame
  • Every JFrame object has a content pane
    associated with it.
  • Content panes are of type java.awt.Container

7
What to Import
  • When we design GUI programs, there are three
    packages we will need to import
  • java.awt.
  • java.awt.event.
  • javax.swing.
  • The java.awt.event package provides us with the
    capability to respond to user interface events,
    such as the pushing of a button.

8
Displaying a Window
  • The most common form of a window displayed via
    Swing is a JFrame.
  • Next, we will see several example programs that
    will
  • display JFrames
  • place Swing objects in frames
  • Exhibit event-driven programming
  • Listeners will be used to respond to mouse
    events.

9
Example GUIone
  • Creating a JFrame
  • new JFrame()
  • Setting the size of our frame.
  • setSize()
  • Displaying the frame
  • setVisible(true)

10
FirstFrame.java
import javax.swing. class FirstFrame extends
JFrame public FirstFrame()
super("FirstFrame") setSize(300, 200)
setVisible(true) public static
void main(String args) JFrame frame new
FirstFrame() frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE)
11
Putting content in a GUI
  • Frames constitute GUI real estate
  • Organization of space
  • Via other containers
  • Each container defines and manages an area within
    the GUI
  • e.g. menu bars, scroll bars, tool bars, panels,
    etc.
  • Containers can be nested
  • Individual components added into those Containers
  • Containers may decide on their layout (will be
    discussed later)

12
LabelTest
  • import javax.swing.
  • import java.awt.
  • public class LabelTest extends JFrame
  • public LabelTest()
  • setTitle("Label Test")
  • JLabel helloLabel new JLabel("Hello")
  • add( helloLabel)
  • setSize(300, 200)
  • setVisible(true)
  • public static void main(String args)
  • LabelTest t new LabelTest()
  • t.setDefaultCloseOperation(JFrame.EXIT_ON_CL
    OSE)

13
Event Handling
  • Perform some functionalities when the button is
    press
  • Need to have a class that implements
    ActionListener
  • The functionalities needed to perform will be put
    in the actionPerformed method
  • Create an instance of this type of ActionListener
  • Register the handler with the component

14
ButtonTest
  • public class ButtonTest extends JFrame
  • public ButtonTest()
  • setTitle("Button Test")
  • JButton helloButton new JButton("Hello")
  • add( helloButton)
  • // create an instance of inner class
    ButtonHandler to use for button event handling
  • ButtonHandler handler new
    ButtonHandler()
  • //register the handler
  • helloButton.addActionListener( handler )
  • setSize(300, 200)
  • setVisible(true)
  • // inner class for button event handling
  • private class ButtonHandler implements
    ActionListener
  • // handle button event
  • public void actionPerformed( ActionEvent
    event )

15
No inner class
  • public class ButtonTest2 extends JFrame
    implements ActionListener
  • public ButtonTest2()
  • setTitle("Button Test")
  • JButton helloButton new JButton("Hello")
  • add( helloButton)
  • helloButton.addActionListener( this )
    //register the handler (the frame itself)
  • setSize(300, 200)
  • setVisible(true)
  • // handle button event
  • public void actionPerformed( ActionEvent
    event )
  • JOptionPane.showMessageDialog( null,
  • "You pressed " event.getActionComma
    nd() )
Write a Comment
User Comments (0)
About PowerShow.com