70
Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Embed Size (px)

Citation preview

Page 1: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Copyright © 2008-2013 Curt Hill

First Windows Program

GUIs in Eclipse

Page 2: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Preliminaries• Goal: Create a Windows program

using Eclipse• The problem to solve is to take the

number of years and convert it into the number of seconds of a persons life

• Simple GUI with:– Two buttons– One input– One output

Copyright © 2008-2013 Curt Hill

Page 3: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Disclaimer• There is a lot to know• Fortunately, there are some

shortcuts• The presentation goes through how

to do this the hard way– In most cases it will be much easier

• These will be shown at the end of the presentation

• The shortcuts will be considered in the demonstration

Copyright © 2008-2013 Curt Hill

Page 4: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Needed Objects• JFrame

– A window and container

• JButton• JTextField

– A field to type in data

• JLabel• LayoutManager

– Organizes GUI objects in a container– Handles the resizing

• ListenersCopyright © 2008-2013 Curt Hill

Page 5: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

How does it fit?• Declare and initialize controls

– Such as buttons

• Add them to a layout manager– Controls the display and sizing

• Create listeners for the buttons– Listeners are separate classes

• The listeners call a method in the main class to handle the event

Copyright © 2008-2013 Curt Hill

Page 6: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Procedure• Create an Eclipse project

– We have done this before

• Add a Java class to it– We will use JFrame as ancestral class

• Add the objects:– Two buttons– One textfield– One label

Copyright © 2008-2013 Curt Hill

Page 7: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Create Project

Copyright © 2008-2013 Curt Hill

Page 8: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Add a new Java Class 1

Copyright © 2008-2013 Curt Hill

Page 9: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Name Package and Object

Copyright © 2008-2013 Curt Hill

Page 10: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Browse for a Type

Copyright © 2008-2013 Curt Hill

Page 11: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Choose Object: javax.JFrame

Copyright © 2008-2013 Curt Hill

Page 12: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Things to be Set

Copyright © 2008-2013 Curt Hill

Page 13: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

After Finish Clicked

Copyright © 2008-2013 Curt Hill

Page 14: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

What goes in main?• Create the constructor

– Constructor is method with same name as class

• Set the size• Make the location• Make it appear

Copyright © 2008-2013 Curt Hill

Page 15: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Constructor• The constructor will look

something like this for now:public Win1(String s){ super(s); init(); }

• It will be called like this from main:Win1 w = new Win1(“Age in Seconds”);

• The call to super is to calling the JFrame constructor

Copyright © 2008-2013 Curt Hill

Page 16: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

The Rest of main• We set the size with:w.setSize(300,200);

• We place it on the desktop with:w.setLocation(100,100);

• Show the window:w.setVisible(true);

• The code will then look like the next screen

Copyright © 2008-2013 Curt Hill

Page 17: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

The First Amount of Code

Copyright © 2008-2013 Curt Hill

Page 18: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Now What?• The program is just the shell of a

Windows program• Lets run it and see what happens• Try out the buttons on title bar

– Minimize– Maximize/Restore– Kill

Copyright © 2008-2013 Curt Hill

Page 19: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

First Run

Copyright © 2008-2013 Curt Hill

Page 20: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Now What?• This program did not have the

things promised– No buttons– No text fields– No nothing

• Here is where we start adding the components

• We will initialize them in the init method that was createdLayout Manager

Copyright © 2008-2013 Curt Hill

Page 21: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Layout Manager• The default layout manager for

JFrame is BorderLayout• This has its uses but overall will

not work well for this application• Instead lets use Flow, which is the

default for Applets• We need two things

– Import – Code

Copyright © 2008-2013 Curt Hill

Page 22: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

What do we need?• Need a new import statement:import java.awt.*;– This should follow the other import at

the front

• In the init method add the following code:FlowLayout flow = new FlowLayout();setLayout(flow);

Copyright © 2008-2013 Curt Hill

Page 23: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Adding Widgets• The change of the layout manager

must occur before any other widget or control is added

• To add a simple control there is a four step process:– Declare the control– Instantiate the control with the new

keyword– Set any other values needed– Add it

Copyright © 2008-2013 Curt Hill

Page 24: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Declaration• The declaration right after the

class header• In our case that is this:public class Win1 extends JFrame {

• So add one label after that:JLabel lab1 = null;

Copyright © 2008-2013 Curt Hill

Page 25: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Instantiate• The instantiation will go into the init

method:void init() {lab1 = new JLabel(“Enter age in years:”);

• There is nothing else for a label so add:add(lab1);

• Lets run it

Copyright © 2008-2013 Curt Hill

Page 26: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

One More Thing• Each control or widget may need

an import• Java allows wildcards on imports• Change the generated import

from:import javax.swing.JFrame;

• Intoimport javax.swing.*;

• This also gives us JLabel, JButton etc

Copyright © 2008-2013 Curt Hill

Page 27: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Another Run

Copyright © 2008-2013 Curt Hill

Page 28: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Next• The next thing to do is add the

input box• This is the Java JTextField• Similar to the JLabel as far as

addingJTextField text = null;…text = new JTextField(5);add(text);

• The 5 is the number of columnsCopyright © 2008-2013 Curt Hill

Page 29: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Buttons• The hard one is buttons• They have the declaration,

instantiation, adding of Labels and TextAreas

• They also need an event handler• The event handler is triggered by

the Action Listener• Gets somewhat uglier at this point

Copyright © 2008-2013 Curt Hill

Page 30: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Action Listeners• The main program is a class • It starts right after the imports with a

public class declaration and proceeds to end of program– So far

• We now need to create an ActionListener

• This handles the notification that the button has been clicked

• Then calls a method in the Win1 class

Copyright © 2008-2013 Curt Hill

Page 31: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Shape• ActionListeners always have a

characteristic form• Usually about 10-12 lines• A sample is given next• Explanation follows

Copyright © 2008-2013 Curt Hill

Page 32: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Sample Listener

Copyright © 2008-2013 Curt Hill

class LClass implements ActionListener{ MyFrame adapter; LClass(MyFrame a){ adapter = a; } // end of constructor

public void actionPerformed(ActionEvent e){ // Call event handler adapter.eventHandler(); } // end of actionPerformed} // end of LClass

Page 33: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Class Name• The class name in this example

was LClass • It may be any Java name• Should not match any other name

in the program• We will use that when we add the

action listener

Copyright © 2008-2013 Curt Hill

Page 34: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Window Name• The MyFrame class in the previous

screen should be the name of the windows class that you created

• In our example this line was the main program:public class Win1 extends …

• Then we should see: Win1 adapter; LClass(Win1 a){– In the second and third line

Copyright © 2008-2013 Curt Hill

Page 35: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Event Handler• The actionPerformed method

called the eventHandler method of our main windows program

• This method name should match a public void method in the main window class

• Like the class name it does not matter what you make it, except that it matches the actual method name

Copyright © 2008-2013 Curt Hill

Page 36: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

More Details• The event handlers need:import java.awt.event.*;

• This is how the system knows what an action performed item is supposed to do

• Next is the modified Lclass

Copyright © 2008-2013 Curt Hill

Page 37: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Inserted ActionListener

Copyright © 2008-2013 Curt Hill

Page 38: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

The Button• Now we can insert the button code

into the init method• The usual code and another callbutton1 = new JButton("Calculate");button1.addActionListener( new LClass(this));add(button1);

Copyright © 2008-2013 Curt Hill

Page 39: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Button Picture

Copyright © 2008-2013 Curt Hill

Page 40: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Event Handler• The action listener class called a

method in the windows class• In our example the code was: adapter.eventHandler();

• Now we have to make that method do something

Copyright © 2008-2013 Curt Hill

Page 41: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

The Skeleton• Such a method will have the

following outer part:void eventHandler(){ …}

• What do we now want to do?• Many programs have this basic form:// Get data// Do computations// Display results

Copyright © 2008-2013 Curt Hill

Page 42: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Getting Data• The main problem is that a

textfield always has text in it– The type is String

• If we are interested in numerics then we must convert to a number

• This is done with static methods of a wrapper class

• We could use Scanner but that is more console/file based

Copyright © 2008-2013 Curt Hill

Page 43: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

TextField to Numerics• To convert an edit box value to a

usable numeric value, we use the wrapper classes– Integer– Double

• Thus code like this:int i = Integer.parseInt(edit1.getText());double d = Double.parseDouble(edit1.getText());

• This causes the value to be obtained from the edit box named edit1 and converted into an int or double

Copyright © 2008-2013 Curt Hill

Page 44: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Back to a String• To display a result in either a

TextField or Label requires the setText method

• This may only take a String• There is no String constructor that

takes a numeric• There is however the valueOf method• Thus:lab1.setText( String.valueOf(d));

Copyright © 2008-2013 Curt Hill

Page 45: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Another Conversion• You may also concatenate a

number to a string painlessly:int a = b*c/2;String s = “Answer: “+a;

• However, you may not assign a number to a string directly without valueOfs = a; // Illegals = String.valueOf(a); // OK

Copyright © 2008-2013 Curt Hill

Page 46: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Static Methods• parseInt, parseDouble, and valueOf

are all static methods• A static method does not need an

instance of the type• We usually use the class name as

if it were an instance:Integer.parseInt(x);Double.parseDouble(y);String.valueOf(u);

Copyright © 2008-2013 Curt Hill

Page 47: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Casts• Why do we not cast these

conversions?• Casts only work on primitive types• A String is not a primitive so it

must use a static method to convert values into it

• Similarly the wrapper methods also use static methods to make a double out of a string

Copyright © 2008-2013 Curt Hill

Page 48: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Get Data• The data is in the text field named text

• The method call:text.getText() will get it

• Then we convert into an integer with:Integer.parseInt(…)

• We use:int years = Integer.parseInt( text.getText());

Copyright © 2008-2013 Curt Hill

Page 49: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Do Computations• The computation is merely a bunch

of multiplications to convert years into seconds

• Multiply by– Days in year– Hours in day– Minutes in an hour– Seconds in a minutedouble sec = years*365.25*24*60*60;

Copyright © 2008-2013 Curt Hill

Page 50: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Display Results• System.out.println is mostly used

for console programs• In a GUI we put the result in a

Label or TextField• In this example the label is named

answer• We use the setText method:

answer.setText(“”+sec);

Copyright © 2008-2013 Curt Hill

Page 51: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Event Handler and Run

Copyright © 2008-2013 Curt Hill

Page 52: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Are we done?• This program is now 61 lines• It still needs some work:

– An exit button– An about button

• However, most of that can wait until we get this part under control

Copyright © 2008-2013 Curt Hill

Page 53: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

New Button• Exit button is simple, add a new:

– Button– Listening class– Event handler

• The event handler method should execute:System.exit(0);

• This enlarges the program to 82 lines• The About needs a dialog which will

wait for a future presentation

Copyright © 2008-2013 Curt Hill

Page 54: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

With Exit Button

Copyright © 2008-2013 Curt Hill

Page 55: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Shell programming• Eighty or more lines is likely more

than we want to type for each program

• What many programmers do is modify a previous program into their next one

• Lots of this program could be recycled

• How would that work?

Copyright © 2008-2013 Curt Hill

Page 56: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

The Windows Shell• Attached to the web site is a shell

program• It is the do-nothing windows

program• It contains instructions on how to

modify it inside comments• The standard Windows shell comes

with one jTextField, several jLabels and three jButtons

Copyright © 2008-2013 Curt Hill

Page 57: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Process• The process is fully described in

the shell itself• Create new project and class• Copy the shell in over everything

except the package statement• Use Replace to change the

WinShell name with the one you created above

• Then customize the program as needed

Copyright © 2008-2013 Curt Hill

Page 58: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Customization• The instructions to customize the

program are coded with /*--

• These can be searched for and modifications made

• If a new button is needed then the instructions to do that are coded with a /*++

Copyright © 2008-2013 Curt Hill

Page 59: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

WinShell• Lets walk through the process a

second time with WinShell • This one will be quicker• We will close all previous projects • First create a new project and add

a class• Paste in WinShell• Modify• Run

Copyright © 2008-2013 Curt Hill

Page 60: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Started

Copyright © 2008-2013 Curt Hill

Page 61: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Delete Program

Copyright © 2008-2013 Curt Hill

Page 62: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Paste in WinShell

Copyright © 2008-2013 Curt Hill

Page 63: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Start Replace

Copyright © 2008-2013 Curt Hill

Page 64: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Replace Dialog

Copyright © 2008-2013 Curt Hill

Page 65: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Change About

Copyright © 2008-2013 Curt Hill

Page 66: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Captions

Copyright © 2008-2013 Curt Hill

Page 67: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Main method

Copyright © 2008-2013 Curt Hill

Page 68: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Event Handler

Copyright © 2008-2013 Curt Hill

Page 69: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Run

Copyright © 2008-2013 Curt Hill

Page 70: Copyright © 2008-2013 Curt Hill First Windows Program GUIs in Eclipse

Lastly• We will next do the demo• The windows shell from the web

page will be used• There is quite a few things that

need to be changed• Not nearly as many as if typed in

new

Copyright © 2008-2013 Curt Hill