16
Java GUI Java GUI

Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Embed Size (px)

Citation preview

Page 1: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Java GUIJava GUI

Page 2: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Graphical User Interface (GUI)

a list

a button

a text field

a label

combo box

checkbox

Page 3: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Graphical User Graphical User InterfacesInterfaces GUI’s, AWT, and SwingGUI’s, AWT, and Swing

– AWT AWT (Abstract Window Toolkit) is the (Abstract Window Toolkit) is the original Java classes used for GUI original Java classes used for GUI development.development.

– Java 2 includes an improved toolkit, Java 2 includes an improved toolkit, named named SwingSwing, to improve on AWT., to improve on AWT. Swing components are easier to use, Swing components are easier to use,

more portable, and provide more more portable, and provide more functionality than older AWT components.functionality than older AWT components.

Page 4: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Inheritance HelpsInheritance Helps

Since Swing was designed as an Since Swing was designed as an improvement of AWT, it made improvement of AWT, it made sense not to start from scratch sense not to start from scratch when designing the Swing when designing the Swing components.components.

To differentiate them, Swing To differentiate them, Swing components have names that components have names that begin with the letter “J”.begin with the letter “J”.

Page 5: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Graphical User Graphical User InterfacesInterfaces GUI’s and the Java Swing GUI’s and the Java Swing

componentscomponents– JComponentJComponent is the ancestor of many is the ancestor of many

Swing classes. Here are some Swing classes. Here are some descendants of the Jcomponent class.descendants of the Jcomponent class. JButtonJButton JPanelJPanel JLabelJLabel JTextAreaJTextArea

Page 6: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

WindowsWindows

We will use the class We will use the class JFrameJFrame to to create GUI windows.create GUI windows.– javax.swing.JFrame inherits from javax.swing.JFrame inherits from

java.awt.Framejava.awt.Frame– Every JFrame object has a “content Every JFrame object has a “content

pane” associated with it.pane” associated with it. Content panes are of type Content panes are of type

java.awt.Containerjava.awt.Container

Page 7: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

What to ImportWhat to Import When we design GUI programs, When we design GUI programs,

there are three packages we will there are three packages we will need to import:need to import:– java.awt.*java.awt.*– java.awt.event.*java.awt.event.*– javax.swing.*javax.swing.*

The java.awt.event package The java.awt.event package provides us with the capability to provides us with the capability to respond to user interface “events”, respond to user interface “events”, such as the pushing of a button.such as the pushing of a button.

Page 8: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Displaying a WindowDisplaying a Window

The most common form of a The most common form of a window displayed via Swing is a window displayed via Swing is a JFrame.JFrame.– Next, we will see several example Next, we will see several example

programs that will:programs that will: display JFramesdisplay JFrames place Swing objects in framesplace Swing objects in frames Exhibit Exhibit event-drivenevent-driven programming programming

– ““Listeners” will be used to respond to mouse Listeners” will be used to respond to mouse events.events.

Page 9: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Example GUIoneExample GUIone

Creating a JFrameCreating a JFrame– new JFrame()new JFrame()

Setting the size of our frame.Setting the size of our frame.– setSize(…);setSize(…);

Displaying the frameDisplaying the frame– setVisible(true)setVisible(true)

Page 10: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

FirstFrame.javaFirstFrame.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); }}

Page 11: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Putting content in a Putting content in a GUIGUI Frames constitute GUI real estateFrames constitute GUI real estate Organization of space: Organization of space:

– Via other containersVia other containers Each container defines and manages an area Each container defines and manages an area

within the GUIwithin the GUI– e.g. menu bars, scroll bars, tool bars, panels, etc.e.g. menu bars, scroll bars, tool bars, panels, etc.

Containers can be nestedContainers can be nested Individual components added into those Individual components added into those

Containers Containers Containers may decide on their Containers may decide on their layout (will be layout (will be

discussed later)discussed later)

Page 12: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

LabelTestLabelTest

import javax.swing.*;import javax.swing.*;import java.awt.*;import java.awt.*;

public class LabelTest extends JFramepublic class LabelTest extends JFrame{ public LabelTest(){ public LabelTest() { setTitle("Label Test");{ setTitle("Label Test"); JLabel helloLabel = new JLabel("Hello");JLabel helloLabel = new JLabel("Hello");

add( helloLabel);add( helloLabel); setSize(300, 200);setSize(300, 200); setVisible(true);setVisible(true); }} public static void main(String[] args)public static void main(String[] args) { LabelTest t = new LabelTest();{ LabelTest t = new LabelTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}}}

Page 13: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

Event HandlingEvent Handling

Perform some functionalities when the Perform some functionalities when the button is pressbutton is press

Need to have a class that implements Need to have a class that implements ActionListenerActionListener

The functionalities needed to perform will The functionalities needed to perform will be put in the actionPerformed methodbe put in the actionPerformed method

Create an instance of this type of Create an instance of this type of ActionListener ActionListener

Register the handler with the componentRegister the handler with the component

Page 14: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

ButtonTestButtonTestpublic class ButtonTest extends JFramepublic class ButtonTest extends JFrame{ public ButtonTest(){ public ButtonTest() { setTitle("Button Test");{ setTitle("Button Test"); JButton helloButton = new JButton("Hello");JButton helloButton = new JButton("Hello"); add( helloButton);add( helloButton); // create an instance of inner class ButtonHandler to use for button event handling // create an instance of inner class ButtonHandler to use for button event handling ButtonHandler handler = new ButtonHandler();ButtonHandler handler = new ButtonHandler(); //register the handler //register the handler helloButton.addActionListener( handler );helloButton.addActionListener( handler ); setSize(300, 200);setSize(300, 200); setVisible(true);setVisible(true); } } // inner class for button event handling// inner class for button event handling private class ButtonHandler implements ActionListener {private class ButtonHandler implements ActionListener {

// handle button event// handle button event public void actionPerformed( ActionEvent event )public void actionPerformed( ActionEvent event ) {{ JOptionPane.showMessageDialog( null,JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() );"You pressed: " + event.getActionCommand() ); }}

} // end private inner class ButtonHandler} // end private inner class ButtonHandler public static void main(String[] args)public static void main(String[] args) { ButtonTest t = new ButtonTest();{ ButtonTest t = new ButtonTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}}}

Page 15: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

No inner classNo inner classpublic class ButtonTest2 extends JFrame implements ActionListenerpublic class ButtonTest2 extends JFrame implements ActionListener{ { public ButtonTest2()public ButtonTest2() { { setTitle("Button Test");setTitle("Button Test"); JButton helloButton = new JButton("Hello");JButton helloButton = new JButton("Hello"); add( helloButton);add( helloButton); helloButton.addActionListener( this ); //register the handler (the frame itself)helloButton.addActionListener( this ); //register the handler (the frame itself) setSize(300, 200);setSize(300, 200); setVisible(true);setVisible(true); } } // handle button event// handle button event public void actionPerformed( ActionEvent event )public void actionPerformed( ActionEvent event ) {{ JOptionPane.showMessageDialog( null,JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() );"You pressed: " + event.getActionCommand() ); }} public static void main(String[] args)public static void main(String[] args) { { ButtonTest2 t = new ButtonTest2();ButtonTest2 t = new ButtonTest2(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}}}

Page 16: Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

ExampleExample

JTextfieldJTextfield

Get Length of square in a Get Length of square in a Jtextfield and output the areaJtextfield and output the area