24
Creating Windows Creating Windows

Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Embed Size (px)

Citation preview

Page 1: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Creating Creating WindowsWindows

Page 2: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

How can we use Java to create programs How can we use Java to create programs that use windows (GUI applications)?that use windows (GUI applications)?

GUI – Graphical User InterfaceGUI – Graphical User Interface

Java has used the following since its Java has used the following since its creation:creation: AWT – Abstract Windowing ToolkitAWT – Abstract Windowing Toolkit There is an AWT package that is part of the There is an AWT package that is part of the

standard Java API.standard Java API.

Page 3: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

However, the AWT package had However, the AWT package had some problems.some problems.

Remember, the nice thing about Java Remember, the nice thing about Java is that it works on multiple is that it works on multiple Operating Systems.Operating Systems.

But some Operating Systems have But some Operating Systems have different ways of interacting with different ways of interacting with their graphic interface system.their graphic interface system.

Page 4: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

For example:For example: Some Operating Systems have slider Some Operating Systems have slider

bars for their windows that behave bars for their windows that behave differently than other operating differently than other operating systems.systems.

Microsoft Windows usually use a mouse Microsoft Windows usually use a mouse with 2 click buttons.with 2 click buttons.

Apple’s Operating Systems usually use Apple’s Operating Systems usually use a mouse with 1 click button.a mouse with 1 click button.

Page 5: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

The AWT package couldn’t handle The AWT package couldn’t handle these differences across multiple these differences across multiple platforms.platforms.

Why? Why? Because the AWT classes rely on an Because the AWT classes rely on an

Operatig System’s peer classes.Operatig System’s peer classes. What are peer classes? Read up on them What are peer classes? Read up on them

are if you are really curiousare if you are really curious

To solve these problems, Java To solve these problems, Java introduced the following package introduced the following package (library of classes)….(library of classes)….

Page 6: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Swing:Swing:

This package of classes doesn’t replace AWT.This package of classes doesn’t replace AWT. It provides an improved alternative for It provides an improved alternative for

creating GUI applications.creating GUI applications. Very few of the Swing classes rely on an Very few of the Swing classes rely on an

Operating Systems peer classes.Operating Systems peer classes. This means that the Swing components have a This means that the Swing components have a

consistent feel and look across different consistent feel and look across different Operating Systems.Operating Systems.

Today we used the JFrame class, which is Today we used the JFrame class, which is part of the Swing package.part of the Swing package.

Page 7: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Some Terminology:Some Terminology: A A containercontainer is something that can hold is something that can hold

multiple things, or components:multiple things, or components:

Likewise, a GUI window is considered a Likewise, a GUI window is considered a containercontainer because it can hold because it can hold multiple graphical components. multiple graphical components.

Buttons

Text Boxes

Labels

Page 8: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

In Java, a container that can be In Java, a container that can be displayed as a window is known as displayed as a window is known as a…. frame.a…. frame.

Frame:Frame: A basic window that has:A basic window that has:

A Title Bar.A Title Bar. A Border.A Border. Buttons for minimizingButtons for minimizing , maximizing, and closing., maximizing, and closing.

Page 9: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

A frame like this one is an object, or an A frame like this one is an object, or an INSTANCE of a class.INSTANCE of a class.

We can create a class and name it something We can create a class and name it something like SimpleWindow.like SimpleWindow.

public class SimpleWindow{

}}

Page 10: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

JFrame class:JFrame class:

This class contains code from the Swing This class contains code from the Swing package that allows us to create a frame.package that allows us to create a frame.

We must give our SimpleWindow class We must give our SimpleWindow class access to the JFrame class.access to the JFrame class.

To do that we must :To do that we must : 1. Import the Swing package.1. Import the Swing package. 2. Write “extends” at the end of our class 2. Write “extends” at the end of our class

declaration. (We will learn more about declaration. (We will learn more about “extends” in the future.“extends” in the future.

Page 11: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Ex:Ex:

import javax.swing.*; //import Swing package

public class SimpleWindow extends JFrame{

}

Page 12: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

To create the frame we now need to To create the frame we now need to do the following:do the following: Use the no-argument constructor.Use the no-argument constructor. Create an instance of the JFrame Create an instance of the JFrame

object.object. Set the window title.Set the window title. Set the size of the window.Set the size of the window. Tell the frame what to do when the exit Tell the frame what to do when the exit

button is clicked.button is clicked. Display the window.Display the window.

Page 13: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Ex:Ex:

public class SimpleWindow extends JFrame{ //No-Arg Constructor public SimpleWindow( ) { //Create an instance of the window in memory. JFrame window = new JFrame();

//Set the title window.setTitle(“I’m a Simple Window!");

//Set the size of the window – this is in pixels window.setSize(350, 250);

//Specify what happens when the close button is clicked. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window. window.setVisible(true); }}

Page 14: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Our creation:Our creation:

Page 15: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Great. An empty Great. An empty window.window.

How do I add a label to How do I add a label to it?it?

Page 16: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

1. Create a panel object.1. Create a panel object. 2. Create a label object.2. Create a label object. 3. Add the label to the panel.3. Add the label to the panel. 4. Add the panel to the frame’s 4. Add the panel to the frame’s

content label.content label.

Let’s break these steps Let’s break these steps down….down….

Page 17: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Each frame object has a content Each frame object has a content pane.pane.

To add things to our frame we have To add things to our frame we have to add them to the content pane.to add them to the content pane.

Content Pane

Page 18: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

How do we add things to the content How do we add things to the content pane?pane? We use a panel.We use a panel.

PanelPanel A panel is also a container that holds GUI A panel is also a container that holds GUI

objects.objects. However, panels cannot be displayed by However, panels cannot be displayed by

themselves. They have to be added to themselves. They have to be added to the content pane.the content pane.

To create a panel:To create a panel: Define the panel name in the class’ fields.Define the panel name in the class’ fields. Create the panel object: Create the panel object:

panel = new JPanel;panel = new JPanel;

Page 19: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Now we can create a label:Now we can create a label: Define the label name in the class’ Define the label name in the class’

fields.fields. Create the label object:Create the label object:

lblMessage = new JLabel(“I’m on a lblMessage = new JLabel(“I’m on a boat!”)boat!”)

Here’s our label:Here’s our label:I’m on a boat!

Page 20: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Add the label to the panel.Add the label to the panel. Ex: Ex:

panel.add(lblMessage);panel.add(lblMessage);

Add the panel to the content pane.Add the panel to the content pane. Ex: Ex:

add(panel);add(panel);

I’m on a boat!

I’m on a boat!

Page 21: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Here is our completed window:Here is our completed window:

Page 22: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Code for the Main class:

public class Main {

public static void main (String[]args) {

//Creating a CrazyWindow object SimipleWindow myWindow = new SimpleWindow();

}

}

Page 23: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows

Code for the SimpleWindow class:

import javax.swing.*;

public class SimpleWindow extends JFrame{ private JPanel panel; private JLabel lblMessage;

//Constructor public SimpleWindow( ) { //Set the window title. setTitle("I'm a Simple Window!");

//Set the size of the window setSize(350, 250);

//Specify what happens when the close button is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create a JPanel object and let the panel field reference it panel = new JPanel();

//Create another label to display craziness lblMessage = new JLabel("I'm on a boat!");

//Add the labels to the panel //panel.add(lblMessage);

//Add the panel to the frame's content pane add(panel);

//Display the window setVisible(true); }}

Page 24: Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows