You are on page 1of 3

Object Oriented Programming Techniques (CMM005) Laboratory #9 !

"# Layout
$ %ims This exercise introduces the use of layout manager objects to control the layout of GUI components inside a Container. & Outcomes In completing this exercise, you should be able to: Use layout managers to control the layout of GUI components inside a Container. ' The Ca(cu(ator %))(ication In this exercise, we will write a simple calculator application that performs integer arithmetic calculation. We will focus on the GUI layout. If you are interested, then you can try to implement the underlying e ent handler!s" that performs the calculation but this is totally optional. '*$ The !"# The calculator has the following GUI layout:

#ere are the re$uirements of the GUI: The menu bar has a single %Help& menu. The %Help& menu has a single %About& menu item, which displays a dialogue box with the program ersion number and author name. The digit buttons are all of the same si'e. The %AC& !i.e. %All Clear&" button, %BackSpace& button and %=& are of the same si'e, but maybe slightly bigger than the digit buttons. #ere is a partial object tree of the GUI. (otice that all boxes are objects. This diagram is incomplete as we ha e not put in the buttons:
Object Oriented Programming Techniques (CMM005) Laboratory ! %& 'ui (0"0)(0"" Page "#$

f:CalcFrame

pane:Container

:BorderLayout

menu_bar:JMenuBar

calc_panel:JPanel

:?Layout

help_menu:JMenu

display_field:JTextField button_panel:JPanel :?Layout

about_menuitem:JMenuItem

Put!in!the!missin"!ob#ects
(otice that there are two unspecified layout manager ob)ects !with the question mar*". They control the layout of calc_panel and button_panel respecti ely. Complete the above object tree before you attempt any programming. What do you thin* is a suitable layout manager for each of these+ #ints: ,ou may notice that the buttons are not of the same si'e. -ll the digit buttons and the arithmetic buttons !i.e. + - * /" are of the same si'e but the three buttons ! AC, BS, =" are slightly bigger. That means you cannot use a single GridLayout for button_panel to layout all the buttons. #ow can you sol e this layout problem+ Complete the GUI ob)ect tree by filling in the missing ob)ects. #ints: Consider ha ing other JPanel under button_panel. .or each of these JPanel, use an appropriate layout manager. '*$*$ The +CalcFrame, C(ass - partially completed GUI is a ailable as Calc ra!e"#a$a. /ownload Calc ra!e"#a$a from Campus 0oodle. Complete the GUI by filling in the missing GUI components. Compile and see if your Calc ra!e GUI wor*s as expected. 1uestions:
Object Oriented Programming Techniques (CMM005) Laboratory ! %& 'ui (0"0)(0"" Page (#$

Can you run the Calc ra!e class+ Why+ If no, how can you test your GUI+ What else is2are needed+

'*&

-.ent /and(ing '*&*$ The +About, 0ia(ogue The %About& menu item simply displays a dialogue box that shows the program ersion number and author, etc. Write a simple e ent handler that ser es this purpose. #int: To display a dialogue box, you may use the method:
J%ptionPane"&'o()e&&a*e+ialo*,-.-/.

'*&*& /and(ing the 0igit 1uttons The digit buttons !345" should update the display area by appending a digit to the end of the current text. Try to write an e ent handler for the digit buttons !only6". #ints: ,ou can create a class, e.g. +i*itButtonHandler, that implements the #a$a"a(t"e$ent"ActionLi&tener interface. 7egister a +i*itButtonHandler e ent handler ob)ect to the digit buttons only. (ot all buttons6 The e ent handler ob)ect needs to access the J0e1t ield ob)ect in order to update the text. ,ou ha e two design options here: In option89, you can pass the J0e1t ield ob)ect reference to the constructor of the +i*itButtonHandler and *eep it as a pri ate ariable inside the +i*itButtonHandler ob)ect. In this case, the e ent handler has to access the J0e1t ield ob)ect directly to set2get the calculator display. In option8:, you can treat the frame as an abstract ob)ect which has defined methods to set2get the calculator display. In this case, the e ent handler does not need to *now the internal implementation of the frame but to use the methods of the frame abstract ob)ect. This is es+ecially use,ul -hen the e.ent handler needs to access a lot o, /01 com+onents in the ,rame& To append a Strin* to the J0e1t ield !e.g. the string %2& when the button 9 is pressed", you need to use Strin* *et0e1t,/ to get the current text, append the extra %;tring& to the returned string and then use &et0e1t,Strin*/ on the J0e1t ield ob)ect. This is clumsy but seems that J0e1t ield does not ha e append,Strin*/ as in J0e1tArea. When the current display text is % 3&, you may not want to append the extra string !e.g. %2&" but to replace it.
Object Oriented Programming Techniques (CMM005) Laboratory ! %& 'ui (0"0)(0"" Page $#$

You might also like