Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Creating Windows

2 How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows (GUI applications)? GUI – Graphical User Interface GUI – Graphical User Interface Java has used the following since its creation: Java has used the following since its creation: AWT – Abstract Windowing Toolkit AWT – Abstract Windowing Toolkit There is an AWT package that is part of the standard Java API. There is an AWT package that is part of the standard Java API.

3 However, the AWT package had some problems. However, the AWT package had some problems. Remember, the nice thing about Java is that it works on multiple Operating Systems. Remember, the nice thing about Java is that it works on multiple Operating Systems. But some Operating Systems have different ways of interacting with their graphic interface system. But some Operating Systems have different ways of interacting with their graphic interface system.

4 For example: For example: Some Operating Systems have slider bars for their windows that behave differently than other operating systems. Some Operating Systems have slider bars for their windows that behave differently than other operating systems. Microsoft Windows usually use a mouse with 2 click buttons. Microsoft Windows usually use a mouse with 2 click buttons. Apple’s Operating Systems usually use a mouse with 1 click button. Apple’s Operating Systems usually use a mouse with 1 click button.

5 The AWT package couldn’t handle these differences across multiple platforms. The AWT package couldn’t handle these differences across multiple platforms. Why? Why? Because the AWT classes rely on an Operatig System’s peer classes. Because the AWT classes rely on an Operatig System’s peer classes. What are peer classes? Read up on them are if you are really curious What are peer classes? Read up on them are if you are really curious To solve these problems, Java introduced the following package (library of classes)…. To solve these problems, Java introduced the following package (library of classes)….

6 Swing: Swing: This package of classes doesn’t replace AWT. This package of classes doesn’t replace AWT. It provides an improved alternative for creating GUI applications. It provides an improved alternative for creating GUI applications. Very few of the Swing classes rely on an Operating Systems peer classes. Very few of the Swing classes rely on an Operating Systems peer classes. This means that the Swing components have a consistent feel and look across different Operating Systems. This means that the Swing components have a consistent feel and look across different Operating Systems. Today we used the JFrame class, which is part of the Swing package. Today we used the JFrame class, which is part of the Swing package.

7 Some Terminology: Some Terminology: A container is something that can hold multiple things, or components: A container is something that can hold multiple things, or components: Likewise, a GUI window is considered a container because it can hold multiple graphical components. Likewise, a GUI window is considered a container because it can hold multiple graphical components. Buttons Text Boxes Labels

8 In Java, a container that can be displayed as a window is known as a…. frame. In Java, a container that can be displayed as a window is known as 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 minimizing Buttons for minimizing, maximizing, and closing., maximizing, and closing.

9 A frame like this one is an object, or an INSTANCE of a class. A frame like this one is an object, or an INSTANCE of a class. We can create a class and name it something like SimpleWindow. We can create a class and name it something like SimpleWindow. public class SimpleWindow {}

10 JFrame class: JFrame class: This class contains code from the Swing package that allows us to create a frame. This class contains code from the Swing package that allows us to create a frame. We must give our SimpleWindow class access to the JFrame class. We must give our SimpleWindow 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 declaration. (We will learn more about “extends” in the future. 2. Write “extends” at the end of our class declaration. (We will learn more about “extends” in the future.

11 Ex: Ex: import javax.swing.*; //import Swing package public class SimpleWindow extends JFrame { }

12 To create the frame we now need to do the following: To create the frame we now need to do the following: Use the no-argument constructor. Use the no-argument constructor. Create an instance of the JFrame object. Create an instance of the JFrame 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 button is clicked. Tell the frame what to do when the exit button is clicked. Display the window. Display the window.

13 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); }

14 Our creation: Our creation:

15 Great. An empty window. Great. An empty window. How do I add a label to it? How do I add a label to it?

16 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 content label. 4. Add the panel to the frame’s content label. Let’s break these steps down….

17 Each frame object has a content pane. Each frame object has a content pane. To add things to our frame we have to add them to the content pane. To add things to our frame we have to add them to the content pane. Content Pane

18 How do we add things to the content pane? How do we add things to the content pane? We use a panel. We use a panel. Panel Panel A panel is also a container that holds GUI objects. A panel is also a container that holds GUI objects. However, panels cannot be displayed by themselves. They have to be added to the content pane. However, panels cannot be displayed by themselves. They have to be added to 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;

19 Now we can create a label: Now we can create a label: Define the label name in the class’ fields. Define the label name in the class’ fields. Create the label object: Create the label object: lblMessage = new JLabel(“I’m on a boat!”) lblMessage = new JLabel(“I’m on a boat!”) Here’s our label: Here’s our label: I’m on a boat!

20 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!

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

22 Code for the Main class: public class Main { public static void main (String[]args) { //Creating a CrazyWindow object SimipleWindow myWindow = new SimpleWindow(); }

23 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); }

24


Download ppt "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."

Similar presentations


Ads by Google