Title: GUI
1GUI
- component buttons, menus, etc.
- event handling system
- containers to add controls to
- layout manager
- graphics operations
- controls press, scroll, choose between
2procedure
- declare controls
- add controls to a container
- add an event handler
3component
- javap java.awt.Component
- location
- size
- color
- etc.
- java foundation classes - more controls
4container
- holds scrollbar, textfields, butons, etc
- must have their size set before they will show up
on the screen - setSize(500,100)
5container
- Window basic free floating window that can be
moved - Panel nested in some larger container
- Frame subclass of Window with title and menubar
6public class myFrame( static Frame fnew
Frame(myFrame) public static void main(
String a) f.setSize(300,100) f.show()
7Layout in a Container
- setLayout( new FlowLayout() )
- FlowLayout components added left to right,
keeping them centered, starting a new line when
necessary - you may specify LEFT or RIGHT justified instead
of centered - default for applet
8Border Layout
- five components may be added
- they are positioned at north, east, south, west
and center - default for Frame
9Grid Layout
- setLayout( new GridLayout(2,3))
- this makes two rows of three components
- all components are the same size
- GridBagLayout is a much more complicated version
of GridLayout
10mixing layouts
- create different containers
- set different layout managers in the different
containers - put the different containers into another
container where you want them
11controls
- Checkbox on or off control
- CheckboxGroup group of checkboxs
- Choice pop-up list
- Label string, left aligned
- List select from several alternatives
- TextField can enter a single line of chars
- TextArea several lines long
12Checkbox
Checkbox cbnew Checkbox(small) add(cb) public
void init() cb.addItemListener(new
ItemListener() public void itemStateChanged(Ite
mEvent ie) System.out.println(ie.paramString())
)
13Menu
- Frame f new Frame(Editor)
- MenuBar mbnew MenuBar()
- Menu file new Menu(file, true)
- //true is for tearoff menu
14public void init() file.add(new
MenuItem(new)) file.add(new
MenuItem(open)) file.add(new
MenuItem(save)) file.add(new
MenuItem(-)) file.add(new MenuItem(exit))
mb.add(file) f.setSize(500,100) f.setMenuBar(
mb) f.show()
15file.addActionListener( new ActionListener() pu
blic void actionPerformed(ActionEvent
e) System.out.println(field is
e.getActionCommand() ) )
16convert applet to application
- make the top level class extend Frame, not Applet
- set the layout manager to FlowLayout
- add a main routine
- if the applet read parameters, have the main read
command line arguments - main should instantiate an object of the class
17applet to application
- the constructor should call start() and init()
- add a menu with an Exit item or an exit button