More Advanced AWT

21
More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: – Labels Choice boxes Check boxes Radio buttons Text boxes – Frames Layout Managers

description

More Advanced AWT. Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: Labels Choice boxes Check boxes Radio buttons Text boxes Frames Layout Managers. Note. - PowerPoint PPT Presentation

Transcript of More Advanced AWT

Page 1: More Advanced AWT

More Advanced AWT

• Last week, you learned about Applets, Lists, and Buttons

• AWT provides several other “widgets” and tools:– Labels– Choice boxes– Check boxes– Radio buttons– Text boxes– Frames– Layout Managers

Page 2: More Advanced AWT

Note

• None of the code on these slides is meant to be copied & pasted, it’s for example only. (Showing the full code would take too much space).

• See the Microwave.java. All of these things are in there.

Page 3: More Advanced AWT

Microwave Example

• A silly implementation of a Microwave

Page 4: More Advanced AWT

Layout

• You’ve used FlowLayout:– FlowLayout is the default– Just lays the controls out in a line,

and wraps when necessary, basedon window shape

Page 5: More Advanced AWT

Grid Layout

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

Page 6: More Advanced AWT

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)

Page 7: More Advanced AWT

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

Page 8: More Advanced AWT

Border Layout Cont.

• setLayout(new BorderLayout())• add() like usual

• Widgets don’t grow to fill available space

Page 9: More Advanced AWT

Frames

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

Page 10: More Advanced AWT

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

Page 11: More Advanced AWT

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 Frame’s name

Page 12: More Advanced AWT

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);

}

}

Page 13: More Advanced AWT

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 UI’s.

Page 14: More Advanced AWT

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)

Page 15: More Advanced AWT

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

Page 16: More Advanced AWT

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

Page 17: More Advanced AWT

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.”);

}

Page 18: More Advanced AWT

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.getSelectedCheckbox()

// note that returns a Checkbox reference

Page 19: More Advanced AWT

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

Page 20: More Advanced AWT

TextField

• Freeform text entry

text = new TextField(“initial contents”, width);

System.out.println(text.getText());

Text.setText(“Something.”);

Page 21: More Advanced AWT

MultiThreading

• Microwave sample has a countdown timer in a separate thread

• To create a thread:– create a class that extends Thread, and override “public

void run()”– Instantiate that class– Call “start()” method

• See Microwave.java for example (too long to add here)

• Also see Timer in Counter thread