Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check.

Similar presentations


Presentation on theme: "More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check."— Presentation transcript:

1 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

2 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.

3 Microwave Example A silly implementation of a Microwave

4 Layout You’ve used FlowLayout: –FlowLayout is the default –Just lays the controls out in a line, and wraps when necessary, based on 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 don’t 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 Frame’s 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 UI’s.

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.getSelectedCheckbox() // note that returns a Checkbox reference

19 Choice Boxes Choice boxes are appropriate for when there are more then a few choices and only one is appropriate Do not use radio buttons for more than three or four choices. Use Choice boxes. Choice boxes can multi-select, but it is significantly 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 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


Download ppt "More Advanced AWT Last week, you learned about Applets, Lists, and Buttons AWT provides several other “widgets” and tools: –Labels –Choice boxes –Check."

Similar presentations


Ads by Google