Download GUI 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java GUI
Graphical User Interface (GUI)
a text field
a label
checkbox
a button
combo box
a list
Graphical User Interfaces

GUI’s, 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.
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”.
Graphical User Interfaces

GUI’s 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

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
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.
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.
Example GUIone

Creating a JFrame
– new JFrame()

Setting the size of our frame.
– setSize(…);

Displaying the frame
– setVisible(true)
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);
}
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)
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_CLOSE);
}
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
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 )
{
JOptionPane.showMessageDialog( null,
"You pressed: " + event.getActionCommand() );
}
} // end private inner class ButtonHandler
public static void main(String[] args)
{ ButtonTest t = new ButtonTest();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
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.getActionCommand() );
}
}
public static void main(String[] args)
{
ButtonTest2 t = new ButtonTest2();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Example


JTextfield
Get Length of square in a Jtextfield
and output the area