Download presentation
Presentation is loading. Please wait.
1
March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Rest of semesterRest of semester –More on strings (Chapter 10) –More on object design (Chapters 6 and 9) Notes on Lab 19Notes on Lab 19 Program (method) designProgram (method) design Graphic bordersGraphic borders
2
March 2005 2R. Smith - University of St Thomas - Minnesota Notes on Lab 19 @param comment for actionPerformed()@param comment for actionPerformed() –Treated this as minor Wrong answer in calculationWrong answer in calculation –Minor unless ‘way’ off Object declaration errors (a few)Object declaration errors (a few) –Wrong object name –Misformed constructor function Declaring it ‘void’ for exampleDeclaring it ‘void’ for example
3
March 2005 3R. Smith - University of St Thomas - Minnesota Detailed Design of your program Long programs are hard to debugLong programs are hard to debug If you keep the program short, it’s easierIf you keep the program short, it’s easier Q: How do you keep programs short?Q: How do you keep programs short? A: Break it into separate methods.A: Break it into separate methods. –A method to create a labeled text field –A method to create a radio button group
4
March 2005 4R. Smith - University of St Thomas - Minnesota Labeled Text Field Example public JTextBox makeTextPanel (JPanel panel, String s)public JTextBox makeTextPanel (JPanel panel, String s) 1. Create a text box1. Create a text box 2. Create a label with the String2. Create a label with the String 3. Put label in the panel3. Put label in the panel 4. Put text box in the panel4. Put text box in the panel 5. Return the JTextBox reference5. Return the JTextBox reference
5
March 2005 5R. Smith - University of St Thomas - Minnesota The challenge Identify what “answers” you need backIdentify what “answers” you need back That decides most of the arrangementThat decides most of the arrangement Input Parameters:Input Parameters: The data you need to make the answersThe data you need to make the answers Answers you need back:Answers you need back: –The panel (always) –Objects that retrieve answers
6
March 2005 6R. Smith - University of St Thomas - Minnesota Radio Button Group example Problem: method only returns one “thing”Problem: method only returns one “thing” –Radio buttons have many ‘things’ to look for Solution:Solution: –Create the button group from an array of strings –Return an array of ‘buttons”
7
March 2005 7R. Smith - University of St Thomas - Minnesota Button Group Method public JRadioButton[] makeButtonSet (JPanel panel, String[] str)public JRadioButton[] makeButtonSet (JPanel panel, String[] str) –1. create a JRadioButton array the same size as the str array –2. Create a button group object –3. Loop for each string in str a. Create a JRadioButton object a. Create a JRadioButton object b. Save it in the array b. Save it in the array c. Add the button to the panel c. Add the button to the panel d. Add the button to the group object d. Add the button to the group object –4. Return the button array to call:to call: radioList = makeButtonSet (buttonPanel, {"Oak", "Mahogany", "Cherry"});radioList = makeButtonSet (buttonPanel, {"Oak", "Mahogany", "Cherry"});
8
March 2005 8R. Smith - University of St Thomas - Minnesota Another Approach to Radio Buttons A customized ‘button group’ objectA customized ‘button group’ object The basic object is a panelThe basic object is a panel –The constructor returns an object usable as a panel –The constructor adds the button functions The button’s “answer” is returned through a methodThe button’s “answer” is returned through a method –Option: return the text of the button –Option: return the button’s number (index) Define numeric constants for the different buttonsDefine numeric constants for the different buttons
9
March 2005 9R. Smith - University of St Thomas - Minnesota Borders It’s helpful to put borders around GUI partsIt’s helpful to put borders around GUI parts Indicates which parts go togetherIndicates which parts go together “Title” border – gives a title to related items“Title” border – gives a title to related items –For example, a collection of radio buttons
10
March 2005 10R. Smith - University of St Thomas - Minnesota Types of borders Empty border – just some spaceEmpty border – just some space Line border – a simple lineLine border – a simple line Etched border – border with 3D visual effectEtched border – border with 3D visual effect Title border – etched border with titleTitle border – etched border with title Matte border, lowered bevel border, raised bevel border, etc. – other stylesMatte border, lowered bevel border, raised bevel border, etc. – other styles Compound border – uses 2 bordersCompound border – uses 2 borders –Inside is one style, outside is the other
11
March 2005 11R. Smith - University of St Thomas - Minnesota Creating a Border Apply the border to a panel:Apply the border to a panel: –panel.setBorder(border); Use BorderFactory methods to create bordersUse BorderFactory methods to create borders –The Factory is NOT an object constructor –It is a set of static methods in the BorderFactory class Examples:Examples: –Panel.setBorder(BorderFactory.createTitledBorder(“Hello”)); –Panel.setBorder(BorderFactory.createLineBorder( Color.Red, 1); - red border, 1 pixel wideColor.Red, 1); - red border, 1 pixel wide
12
March 2005 12R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.