se1021-5-3-GuiLayoutAndActionListener

Download Report

Transcript se1021-5-3-GuiLayoutAndActionListener

SE-1021
Software Engineering II

Week 5, Class 3

Completing the Calculator GUI layout
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
1
How can we create this
calculator layout?
112344
+
5
-
*
/
1
2
3
4
5
6
7
8
0
Modify the code below
to put the operators
above the buttons…
/// in the constructor…
setLayout(new BorderLayout());
JPanel panel = createButtonPanel();
add(panel,BorderLayout.CENTER);
public JPanel createOperationPanel() {
9
JPanel panel = new JPanel();
panel.add(new JButton("+"));
panel.add(new JButton("-"));
panel.add(new JButton("*"));
panel.add(new JButton("/"));
return panel;
SE-2811
2
Dr. Josiah Yoder
}
Slide style: Dr. Hornick
Get to know your classmates


Turn to a neighbor, and find out who traveled
farther over Christmas break
When you have determined who traveled
farther, that person should raise his hand
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
3
What do we need to do to get
our GUI to respond?
In your groups of 2…
 Write a list of as many things that you can
think of as possible to complete the GUI
application.
 Favor quantity over quality
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
4
What is an inner class?

An inner class is



Declared inside of another class
Usually declared private (needed by only one
class)
Has access to all variables of the top-level class




Even instance variables!
And so … _____________________________
Even private variables!
And so … ______________________________
Dean & Dean, 2nd Ed. Pp 775-776 (Sec. 17.10)
http://docs.oracle.com/javase/tutorial/java/javaOO/n
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
5
Anonymous Inner Class
This is a class that is DECLARED at the same
place it is INSTANTIATED in the program
Syntax:
new InterfaceName () {
ClassBody
}
Use: Use it where you would use the
constructor of the class
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
6
Lambda Expression
A lambda expression is an anonymous inner
class with shorter syntax, for interfaces with just
one method.
Example:
e -> myMethod(e)
Use: Use it where you would use a constructor
or an anonymous inner class.
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
7
From the Horse’s Mouth*
“Lambda expressions let you express
instances of single-method classes more
compactly.”
http://docs.oracle.com/javase/tutorial/java/java
OO/lambdaexpressions.html (emphasis added)
* “straight from the Horse’s mouth” is an idiom
saying to get something from an authoritative
source. It refers to looking into a horse’s mouth
to determine its age.
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
8
Answer this question
Example lambda expression:
e->System.out.println(e.getActionCommand())
Can a lambda expression use instance
variables? How about instance methods?
Think alone 1 minutes, discuss in pairs 1
minute
SE-2811
Dr. Josiah Yoder
Slide style: Dr. Hornick
9