Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Software Engineering
PersonGui (Mark 1) Case Study
2
Contents The PersonGui (Mark1) The The Person Gui (Mark1) ToolBar
PersonGui Constructor Adding Buttons Making Buttons Handling Events
3
The Person Gui (Mark1): Use Cases
user Define Age Define Height Display Attributes
4
The Person Gui (Mark1): Class Diagram
<<interface>> ActionListener 1 JPanel PersonGui class has caused the JPanel (which it extends) to be linked to a JToolBar JTextArea 1 PersonGui 1 JToolBar 1 JTextField 1 1 JFrame 1 1 Person private Double height private Double age void setAge (String) void setHeight (String) String getAge () String getHeight () String Attributes_Description ()
5
Person private Double height private Double age void setAge (String)
public void setAge (String stringAge) { age = new Double(stringAge); } public void setHeight (String stringHeight) { height = new Double(stringHeight); } Person private Double height private Double age void setAge (String) void setHeight (String) String getAge () String getHeight () String Attributes_Description () public String getAge () { return age.toString(); } public String getHeight () { return height.toString(); } public String Attributes_Discription () { return "This Person has height " + getHeight() + " and has age " + getAge() + "\n"; }
6
The Person Gui (Mark1) JFrame JTextArea JToolBar JTextField
7
PersonGui Constructor: JToolBar
(Full code available from course web site) public PersonGui() { super(new BorderLayout()); JToolBar toolBar = new JToolBar("Still draggable"); addButtons(toolBar); toolBar.setFloatable(false); toolBar.setRollover(true); textArea = new JTextArea(5, 30); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); setPreferredSize(new Dimension(750, 430)); add(toolBar, BorderLayout.PAGE_START); add(scrollPane, BorderLayout.CENTER); }
8
PersonGui Constructor
super(new BorderLayout()); Causes the enclosed code to act on the super-class that extends PersonGui. In general for a class extension can invoke methods of parent (super) class via super.method(x,y,z)
9
Here's what happens if we don't call super.
public PersonGui() { //super(new BorderLayout()); new BorderLayout(); //Create the toolbar. BorderLayout now attached to toolbar, not JPanel
10
PersonGui Constructor
Internal method call JToolBar toolBar = new JToolBar("Still draggable"); addButtons(toolBar); toolBar.setFloatable(false); toolBar.setRollover(true); If the rollover state is true then border of the toolbar buttons will be drawn only when the mouse pointer hovers over them.
11
toolBar.setFloatable(true);
12
toolBar.setRollover(true);
13
addButtons method Internal method call causes actions on text field
protected void addButtons(JToolBar toolBar) { JButton button = null; // add first button button = makeNavigationButton(PRINT_ATTRIBUTES, "Prints a String containing ageString and heightString", "Print Attributes"); toolBar.add(button); // add second button button = makeNavigationButton(AGE, "Sets the value of ageString", "Define Age"); toolBar.addSeparator(); textField.setColumns(10); textField.addActionListener(this); textField.setActionCommand(TEXT_ENTERED); toolBar.add(textField); } Internal method call causes actions on text field to be notified to 'this' (i.e PersonGui class)
14
makeNavigationButton method
protected JButton makeNavigationButton(String actionCommand, String toolTipText, String altText) { //Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); button.setText(altText); return button; } causes actions on button to be notified to 'this'
15
button.setActionCommand(AGE);
button press causes an ActionEvent object to be created, with an action command string set to AGE button.setToolTipText("Sets the value of ageString"); when mouse hovers over button this text will appear
16
button.addActionListener(this);
creates an ActionListener that is attached to the PersonGui class
17
Handling Events: Implementing interface
public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); String description = null; if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Attributes_Discription(); description = text + newline } else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText(); dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); } else if (HEIGHT.equals(cmd)) { // third button clicked } else if (TEXT_ENTERED.equals(cmd)) { // text field } displayResult(description);
18
actionPerformed(ActionEvent e)
button:JButton PersonGui addActionListener(this) left_mouse_click e:Action Event <<create>> notify(e ) String cmd = e.getActionCommand();
19
actionPerformed(ActionEvent e)
else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText(); dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); } :PersonGui textField: dude:Person getText() ageString setAge(ageString); setText("")
20
PersonGui textField: dude:Person
else if (HEIGHT.equals(cmd)) { // third button clicked heightString = textField.getText(); dude.setHeight(heightString); description = "The height of the person is: " + heightString + newline; textField.setText(""); } PersonGui textField: dude:Person getText() heightString setHeight(heightString); setText("")
21
PersonGui dude:Person
if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Attributes_Discription(); description = text + newline; } PersonGui dude:Person Attributes_Discription( ) text
22
displayResult method Makes sure next append will
protected void displayResult(String actionDescription) { textArea.append(actionDescription + newline); textArea.setCaretPosition(textArea.getDocument().getLength()); } Adds text after caret current position Makes sure next append will occur at the end of document.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.