Slides - Rose
Download
Report
Transcript Slides - Rose
CSSE221: Software Dev. Honors
Day 8
Announcements
HW3 due now, solutions will be posted.
Questions on Arrays?
Max # of elements in an array:
Today is Constitution Day.
Integer.MAX_INT, but your machine probably doesn’t have enough
space to hold this much data! And the JVM will certainly not claim all
your memory even if you did.
Newer machines (with 64-bit addressing) will have more… but I don’t
know how Java is responding (perhaps Java 7 will have long int array
indices)
Speaker in Kahn Room in Union, 10th hour.
No baby yet…
Capsule Deliverables: sec 1
Please email the quiz, key, and summary to me by 7:30
am on the day you are presenting.
Send .doc files
(If you get them to me by 3pm on a weekday before
you present, I’ll still print them)
Lessons from week 1:
Nice research and summarization!
Proofread your stuff. Everyone else will see it.
1-page quizzes are good (or staple)
Summaries don’t have to be verbose, just dense with info.
This week: Fifteen assignment
Monday:
Tuesday:
Fifteen specification
GUIs using Java’s Swing library
Intro to UML as a design tool
Start Fifteen
EventListeners: responding to user input
Time to work on project
Thursday:
Anonymous classes (capsule)
Function objects and comparators (capsule)
“Fifteen”
Arrays (especially 2D)
Creating GUIs using Swing
Responding to mouse clicks
Fifteem Teams
Repos is csse221-200810-<usr1>-<usr2>. If solo, use your personal repos.
•
•
•
•
•
•
•
•
•
beltonj1-fishmad
bennetrn-leveyjr
bennindp-speyerea
crockeea-morrisps
devorejd-priestjs
lundgrpb-skilessa
johnsoad-smithny
sullivja-wentztj
mcginnda
behlinmc-bennetdj
clutecc-dovalojd
gatesds
heinma1-hulettbh
johnsoac-rubinza
kruthar-richarme
millerbe-eckertwm
mosttw-reillytm
nibertjw-stokesej
Discuss how to pair-program
The Java Swing Library
JFrames, JTextBoxes, JButtons, JScrollPanes…
what’s available?
What components will I need…
…now, for Fifteen?
…later, for a term project?
Browse the Visual Index to the Swing Components in
Sun’s Java Tutorial.
Fundamentals of Software
Development 1
6
JFrame
A simple window that contains the other GUI
components
Background Color
Make sure to set visible
getContentPane().setBackground(Color);
setVisible(true);
Setting the size
setSize(width, height);
Benefits of Extending JFrame or
JComponents like JPanel
When extending the JFrame class we can still
instantiate it as JFrame.
“Controlling” the JFrame is easier to
comprehend when we do not need the calling
object stated every time.
i.e. setSize(w,h); instead of
jFrameObject.setSize(w,h);
JLabel
Displays text to user
User cannot edit text
Construction: JLabel message = new
JLabel(String text);
Methods:
String text = message.getText();
message.setText(String text);
JTextField
Lets user enter or modify a single line of text
Construction:
Getting the Text:
JTextField text = new JTextField(int
WidthInCharacters);
String input = text.getText();
Setting text in a Text Field:
text.setText(String output);
JTextArea
Lets user enter or modify multiple lines of text.
Declaration: private JTextArea text;
Constructors:
text = new JTextArea(int rows, int columns);
text = new JTextArea(String output);
text = new JTextArea(String output, int rows, int columns);
Useful methods:
String input = text.getText();
text.setText(String output);
setEditable(Boolean editable);
boolean editable = text.isEditable();
JButton
Used to let user execute action
Construction: JButton button = new
JButton(String text);
button.setBackground(Color color);
JPanel
Simple container that groups objects
Meant to subdivide
Creation Syntax:
JPanel panelName = new JPanel();
Useful attributes and methods
.setLayout(new Layout());
.add(component);
.setBackground(Color color);
Layout Managers
Frames are organized by layout managers.
Organizes the locations of the components
within the frame
Not only frames use layout managers, but Panels
are also organized by layout managers.
Allows for panels within panels.
Layout: Flow Layout
Places the components from the left side to the
right side.
Wraps around the right side.
.setLayout(new FlowLayout());
No special add syntax.
Layout: Border Layout
North, South, East, West, Center
setLayout(new BorderLayout());
.add(component, BorderLayout.NORTH);
Layout: Grid Layout
Similar to flow in the left to right manner, but the grid
that you define limits the size of any one component
You can leave out a definite definition of how many
rows or columns are in the grid.
setLayout(new GridLayout(r,c));
Adds in components from left to right, top to bottom.
Cannot specify which block.
PaintComponent
Used with JPanels, and all JComponents, to
paint the components on the screen.
Called automatically, no need to invoke it.
Use repaint(); to force the paintComponent()
method to execute.
Start with super.paintComponent(g); where g is
your Graphics object
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackgroundColor(Color.red);
g.drawOval(10,40,20,20);
}
Alternative designs
I want a component to appear. Question:
Should I paint it?
Add a new component?
Press me!
Answer: It depends…
On how much control you want over its appearance
On how you want it to respond to events
It’s usually a tradeoff.
Listeners
If you want to respond to events, you need to implement a
Listener interface.
We’ll look at these in detail tomorrow
Demo together