More Advanced AWT - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

More Advanced AWT

Description:

Border Layout creates five areas on the screen, 'North', 'South' ... Create a 'CheckboxGroup' and pass it to each Checkbox in ... ways to create a thread ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 29
Provided by: Lhe9
Category:
Tags: awt | advanced | create | more

less

Transcript and Presenter's Notes

Title: More Advanced AWT


1
More Advanced AWT
  • You have seen ex about Applets, Lists, and
    Buttons
  • AWT provides several other widgets and tools
  • Labels
  • Choice boxes
  • Check boxes
  • Radio buttons
  • Text boxes
  • Frames
  • Layout Managers

2
Note
  • None of the code on these slides is meant to be
    copied pasted, its for example only. (Showing
    the full code would take too much space).
  • See the Microwave.java. All of these things are
    in there.

3
Microwave Example
  • A silly implementation of a Microwave

4
Layout
  • Youve used FlowLayout
  • FlowLayout is the default
  • Just lays the controls out in a line,and wraps
    when necessary, basedon window shape

5
Grid Layout
  • GridLayout creates a grid and puts each component
    in the grid, left to right, top to bottom

6
Grid Layout Cont.
  • Layout managers manage the layout for canvases,
    like Frame or Applet
  • setLayout(new GridLayout(rows, cols))
  • Note the widgets grow to fit the cells by
    default there are many options to control
    formatting
  • Entering a blank space in the grid
  • Give it an empty Label
  • component.add(new Label())
  • Add components like usual
  • this.add(Button)

7
Border Layout
  • Border Layout creates five areas on the screen,
    North, South, East, West, and Center,
    and puts components in those areas.
  • This screen shot put things in North, Center,
    and South you can experiment with East and
    West
  • Checkbox North
  • Choice Center
  • Radio buttons South

8
Border Layout Cont.
  • setLayout(new BorderLayout())
  • add() like usual
  • Widgets dont grow to fill available space

9
Frames
  • Frames are separate dialog boxes, complete with
    closing button, maximize, minimize, etc.

10
Creating/Using a Frame
  • frame new Frame()
  • frame.add(some_widget) // adds a widget
  • frame.setVisible(true) //shows the frame
  • frame.setLayout(new BorderLayout())
  • // uses the BorderLayout manager
  • frame.setSize(400,125) //width, height

11
Frame Events
  • The widgets so far do not need event listeners
    (though they all can have listeners attached to
    them), but a Frame needs a WindowListener
    attached to it.
  • Without a listener, the window will never close!
  • frame.addWindowListener(new minimalFrameListener()
    )
  • // see next slide for minimalFrameListener
  • // change name at the bottom to your Frames
    name

12
Minimal Frame Listener
  • public class minimalFrameListener implements
  • WindowListener
  • public void windowDeactivated(WindowEvent e)
  • public void windowClosed(WindowEvent e)
  • public void windowDeiconified(WindowEvent e)
  • public void windowOpened(WindowEvent e)
  • public void windowIconified(WindowEvent e)
  • public void windowActivated(WindowEvent e)
  • public void windowClosing(WindowEvent e)
    name.setVisible(false)

13
Panels
  • These layouts are only rarely enough
  • Panels can hold components and organized by the
    layout managers
  • Panels can even hold other panels!
  • Nest panels in panels to create relatively
    complicated UIs.

14
Panel Code
  • panel new Panel()
  • Example
  • This is actually a panel that contains a label
    and a choice, and was added to the Center of
    the border layout
  • panel.add(new Label(Set Heat Level))
  • panel.add(powerSetting)
  • this.add(Center, panel)

15
Nesting Panels
  • Layouts too complicated for the simple managers
    can usually be created by creating several panels
    and composing them together
  • Each panel can have its own Layout manager

16
More Widgets
  • The toolkit you are using, Abstract Window
    Toolkit (AWT), has several more widgets
  • Widgets are the things you interact with, like
    buttons, choice boxes, or radio buttons

17
Checkboxes
  • Checkboxes are appropriate for Boolean questions
    Yes or no.
  • checkbox new Checkbox(Output result to
    stdout?)
  • Inside parameter is the label on the Checkbox
  • Getting/Setting the state
  • Checkbox.setState(true) // checks the checkbox
  • Checkbox.getState() // returns true or false
  • if checkbox.getState()
  • System.out.println(It was checked.)

18
Radio Buttons
  • Radio Buttons are checkboxes bundled into a
    group, Appropriate when only one choice of many
    is valid at a time
  • Create a CheckboxGroup and pass it to each
    Checkbox in the constructor
  • wavelengthGroup new CheckboxGroup() // no
    arguments
  • microwave new Checkbox(Microwave,
    wavelengthGroup, true)
  • // last argument controls initial status
  • infrared new Checkbox(Infrared,
    wavelengthGroup, false)
  • Getting the selected Checkbox
  • Checkbox selected wavelengthGroup.getSelectedChe
    ckbox()
  • // note that returns a Checkbox reference

19
Choice Boxes
  • Choice boxes are appropriate for when thereare
    more then a few choices and only one
    isappropriate
  • Do not use radio buttons for more than threeor
    four choices. Use Choice boxes.
  • Choice boxes can multi-select, but it
    issignificantly more complicated to program.
  • powerSetting new Choice()
  • powerSetting.add(Carbonize)
  • powerSetting.add(Sear)
  • powerSetting.select(Sear) //selects a choice
    programmatically
  • System.out.println(powerSetting.getSelectedItem())
  • // prints the item text

20
TextField
  • Freeform text entry
  • text new TextField(initial contents, width)
  • System.out.println(text.getText())
  • Text.setText(Something.)

21
MultiThreading
  • Microwave sample has a countdown timer in a
    separate thread
  • 2 ways to create a thread
  • Subclass Thread
  • Implement Runnable and pass it to Thread
    constructor.
  • In either case end up with a Thread object
  • Call start() to start.
  • Run() is method that does the work.
  • Once run() exits, thread is dead
  • Cant restart thread, you have to create a new
    one.

22
What is a Thread?
  • The state of a computation is determined by
  • The current value of the program counter (and
    other registers).
  • The current value of the stack pointer (and
    variables stored on the stack).
  • The current values of all variables in memory.
  • A thread is a distinct PC and stack
  • Objects are shared (stored on the heap)
  • Local variables (references to the heap) are
    owned by an individual thread.
  • Many processors, one shared memory.
  • Think of the Objects as being in a common pool.
  • Multiple CPUs allow several methods to be run at
    the same time.
  • Threads run methods
  • A single thread may operate on several objects,
    one method at a time.
  • A single object may be operated upon by several
    threads, all at once!

23
Class Thread
  • Thread is a concrete class that is used to
    provide an API for operating upon actual threads.
  • Programmers are expected to define subclasses of
    Thread and to override the run method.
  • run defines the main routine for that thread.
  • Creating an object of type Thread does not
    launch the thread.
  • Each Thread object must have its start method
    invoked before it will begin executing.

24
Code example
  • class ContinuousHello extends Thread
  • public void run()
  • while (true)
  • System.out.print("Yo")
  • class ThreadExampleOne
  • public static void main(String args)
  • Thread t new ContinuousHello()
  • System.out.println("OK, I've made two
    threads")
  • t.start()

25
Thread Termination
  • When a thread has finished executing its run
    method, the thread terminates.
  • Threads can also be terminated using the
    Thread.terminate method, but this is considered
    somewhat anti-social.
  • The JVM terminates when the last non-system
    thread terminates (or when System.exit() is
    invoked).
  • You can wait on another thread with Thread.join()

26
Thread Scheduling
  • The Java language specification does not dictate
    how threads are scheduled.
  • Most JVM implementations are using a pre-emptive
    scheduler.
  • Threads do have priorities.
  • A lower priority thread will never run as long as
    a higher priority thread is ready.
  • priority can be accessed with getPriority and
    setPriority

27
Why Use Threads if you only have one processor?
  • Spreading the work of a single program over
    multiple threads should, in general, slow down
    the overall execution rate.
  • Threads are helpful, however, if the application
    must interact with unpredictably slow things
  • humans
  • computers across a network
  • While one thread is blocked waiting for input
    from the external device (or person), other
    threads can continue operating.

28
Garbage Collection and Threads
  • Although there are concurrent garbage
    collectors, it is generally best to assume that
    your JDK does not have one.
  • All thread processing must be stopped while
    garbage collection is in progress.
Write a Comment
User Comments (0)
About PowerShow.com