Java AWT Tutorial
2 Parts Creating the graphical interface Defining the behavior
Used this site [Link] As well as Deitel & Deitel Small Java How To Program 6th edition
AWT - Building a graphical interface
2 ways
Write the code yourself Use a graphical interface builder
We will write the code ourselves, you should know what kind of code the automatic tools build for you
AWT - Building a graphical interface
Terminology
AWT Abstract Windows Toolkit GUI Graphical User Interface
AWT - Building a graphical interface
Creating the window
The first thing you need is the window your GUI application will be in In Java AWT top level windows are represented by the Frame class
AWT - Building a graphical interface
The Frame class
Most common way is to use the single argument constructor The argument is a String that becomes the windows title The window is initially invisible, you must call the Frames show() method which it inherits from the window class
AWT - Building a graphical interface
Adding a Frame and using the show() method
package [Link]; import [Link].*; public class MyApp1 { public static void main (String arg[]){ Frame myFrame = new Frame("example Frame for cosc210"); [Link](); // necessary for the frame to be visible } }
Used the mouse to drag the Window to a larger size
AWT - Building a graphical interface
Adding a Frame and using the show() method
package [Link]; import [Link].*; public class MyApp1 { public static void main (String arg[]){ Frame myFrame = new Frame("example Frame for cosc210"); [Link](); [Link](1000,100); } }
AWT - Building a graphical interface
Adding a component
Each piece inside a window is a component
Text box, menu, scroll bar
some components act as containers for other components Example the component Window we have already used by creating a Frame
Because Frame extends Window every Frame is a Window but every Window is not necessarily a Frame
AWT - Building a graphical interface
Adding a component Example the component Window we have already used by creating a Frame
Notice: Window extends Container Container is the generic component that Defines how all containers work Container is abstract you have to choose An implemented container to use containers
AWT - Building a graphical interface
Adding a component (a static component)
We will start with a Label
package [Link]; import [Link].*; public class MyApp1 { public static void main (String arg[]){ Frame myFrame = new Frame("example Frame for cosc210"); [Link](); [Link](300,300); Label myLabel1 = new Label("cosc210 pass"); [Link](myLabel1); } }
AWT - Building a graphical interface
Adding another component (a dynamic component)
We will add a Choice Added the following code in our main method
Choice choices = new Choice(); [Link]("you get an A"); [Link]("you get an B"); [Link]("you get an C"); [Link](choices);
Notice the label has been covered up
AWT - Building a graphical interface
LayoutManager class
There are many types of LayoutManagers
Each is designed to facilitate positioning components
We will use the BorderLayout manager
This is the most commonly used LayoutManager for AWT
BorderLayout divides the window into 5 areas (North, South, East, West, Center)
Use the containers add method with a second parameter like the following:
[Link] [Link] [Link] [Link] [Link]
AWT - Building a graphical interface
LayoutManager class
Added the following code in our main method
package [Link]; import [Link].*; public class MyApp1 { public static void main (String arg[]){ Frame myFrame = new Frame("example Frame for cosc210"); [Link](); [Link](300,300); BorderLayout myBL = new BorderLayout(); [Link](myBL); Label myLabel1 = new Label("cosc210 pass"); [Link](myLabel1,[Link]); //[Link](myLabel1); Choice choices = new Choice(); [Link]("you get an A"); [Link]("you get an B"); [Link]("you get an C"); //[Link](choices); [Link](choices,[Link]); } }