Java Programming Lecture 13

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 10 months ago)

109 views

Java Programming
Lecture 13
Alice E.Fischer
March 8,2012
Alice E.Fischer ()
Java Programming - L13...1/15
March 8,2012 1/15
Outline
1
The Second Swing Program:Smile
Swing Components
Using Swing Components
2
Summary and Homework
Alice E.Fischer ()
Java Programming - L13...2/15
March 8,2012 2/15
The Second Swing Program:Smile
The Second Swing Program:Smile
JFrames,JPanels,and the graphics window
Widgets:JButton,JLabel,JTextbox
The placement of widgets in panels.
Layouts
Shapes:Rectangle,Oval
Alice E.Fischer ()
Java Programming - L13...3/15
March 8,2012 3/15
The Second Swing Program:Smile
Swing Components
Application vs.Applet
An application has a main function.
An applet does not { it has an init() function instead.
An application runs in an IDE or in a command window.
An applet is run by running an.html le in a browser.Some IDEs
make it easy to test applets,others don't.
In a graphics application,the main function must create a new object
that extends JFrame..
Smile S = new Smile();
It is helpful to set the window closing operation,also:
S.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Alice E.Fischer ()
Java Programming - L13...4/15
March 8,2012 4/15
The Second Swing Program:Smile
Swing Components
Frames and Panels
The main Swing window is a JFrame.It wraps,organizes,and controls a
set of panels within it.
You can put JPanels inside the JFrame to hold and organize other widgets.
One frame may have many JPanels.They are arranged according to a
layout.JPanels also have layouts,which are used to organize the smaller
panels within.
For a JFrame,the default layout is BorderLayout.
Alice E.Fischer ()
Java Programming - L13...5/15
March 8,2012 5/15
The Second Swing Program:Smile
Swing Components
Layouts
Elements are placed into a JFrame or JPanel according to its current
layout.The simplest layouts are:
BorderLayout:Panels can go on any edge or in the center.Positions
are called North,South,East,West,and Center,and can be written
two ways:BorderLayout.NORTH or"North".
FlowLayout:Add elements left to right until the width is lled,then
start another line.
GridLayout( r,c):A matrix with r rows and c columns,which is lled
left to right and top to bottom.
There are many other layouts,with tabs,cards,and much more.
Alice E.Fischer ()
Java Programming - L13...6/15
March 8,2012 6/15
The Second Swing Program:Smile
Swing Components
Widgets
A widget is an element,large or small,complex or simple,that you might
put into a graphics window.The basic Swing widgets are:
JPanel:A container to organize other widgets.
JButton:Click it to cause some action.
JTextField:a box into which the user can enter input.
JLabel:The label for a text eld.
JTextArea:A big output box able to hold several lines of data.
JCheckBox:a box for entering a boolean o/on input.
JRadioButton:one of a set of mutually-exclusive o/on elements.
These are organized into ButtonGroups.
Alice E.Fischer ()
Java Programming - L13...7/15
March 8,2012 7/15
The Second Swing Program:Smile
Using Swing Components
To Create a JFrame
In your main() function:
1
Declare and create the app window:
Smile s = new Smile();
2
Set the app to terminate when the window is closed:
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Alice E.Fischer ()
Java Programming - L13...8/15
March 8,2012 8/15
The Second Swing Program:Smile
Using Swing Components
To Customize and Use your JFrame
In your JFrame-class constructor:
1
Give the window a title:
super("Smile");
2
Control its position and size:
setBounds( x,y,width,height);
3
Grab the graphics area:
Container pane = getContentPane();
4
Change the layout if you don't want a BorderLayout.
5
Add components to the graphics area:
pane.add(p1,"North");
6
Make the window visible:
setVisible(true);
Alice E.Fischer ()
Java Programming - L13...9/15
March 8,2012 9/15
The Second Swing Program:Smile
Using Swing Components
To Use a Panel
1
Declare and create it:
private JPanel p1 = newJpanel();
2
Give it a layout:
p1.setLayout( new GridLayout(1,2));
3
Add widgets to it:
p1.add(red);p1.add(rLabel);
4
Put your panel into the JFrame object named pane:
pane.add(p1,"North") or
pane.add(p1,BorderLayout.NORTH)
Alice E.Fischer ()
Java Programming - L13...10/15
March 8,2012 10/15
The Second Swing Program:Smile
Using Swing Components
To Use a Text Field
Text elds can be used for input and output.
1
Declare it,name it,and supply an initial value for the display.The
second parameter is the minimum width of the box.private
JTextField red = new JTextField("0",5);
2
Create a label for it:
private JLabel rLabel = new JLabel("red");
3
Add both to a panel so that one is next to or above the other:
p1.add(red);
p1.add(rLabel);
Alice E.Fischer ()
Java Programming - L13...11/15
March 8,2012 11/15
The Second Swing Program:Smile
Using Swing Components
To Make a Clickable Button
1
Declare it,name it,and supply a label for the display:
private JButton submit = new JButton ("submit")
2
Add it to a panel:
p5.add(submit);
3
Register the button so that Swing will listen for button events:
submit.addActionListener(this);
4
Dene a handler for these events:
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == submit){...}
}
Alice E.Fischer ()
Java Programming - L13...12/15
March 8,2012 12/15
The Second Swing Program:Smile
Using Swing Components
A Button Handler
1
Declare that your class implements the ActionListener interface:
class Smile extends JFrame implements ActionListener {
2
Dene the method:
public void actionPerformed(ActionEvent event) {
3
Grab the event information:
Object source = event.getSource();
4
Use if...else to identify which button was clicked:
if (source == submit) {...}
5
Download information from relevant TextFields and use that info to
modify your model:
rd = new Integer (red.getText()).intValue();
6
Do other appropriate actions for that button and repaint the window:
repaint();
Alice E.Fischer ()
Java Programming - L13...13/15
March 8,2012 13/15
Summary and Homework
Java Summary
We have discussed the following packages,classes,methods,and concepts
today:
The concept of derivation and extends,super
Swing components
Containers and Layouts
Text elds and labels for them.
Registering buttons
Writing button handlers
Alice E.Fischer ()
Java Programming - L13...14/15
March 8,2012 14/15
Summary and Homework
P7.Swinging Users.Due Thursday,March 22
Re-implement part of P5 using a Swing interface.
Dene three classes:Main,User,and Admin.User is the same as P5,
Main is almost the same.
Derive an Admin class from JFrame.
Your MODEL is the same ArrayList,current user,current ID.
Encryption,les,and password validation are same as P5.
For your CONTROLLER,create buttons and handlers for NewUser
and Quit.(We will add the other parts later.)
Use text elds (laid out vertically) to enter the full name,logon name,
password and repeated password.
Be sure the main thread writes the output le and terminates when
you close the window.
Hand in code,some screen shots,and your output le.
Alice E.Fischer ()
Java Programming - L13...15/15
March 8,2012 15/15