DOC PREVIEW
MSU CSE 470 - maawt
Course Cse 470-
Pages 21

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

More Advanced AWTNoteMicrowave ExampleLayoutGrid LayoutGrid Layout Cont.Border LayoutBorder Layout Cont.FramesCreating/Using a FrameFrame EventsMinimal Frame ListenerPanelsPanel CodeNesting PanelsMore WidgetsCheckboxesRadio ButtonsChoice BoxesTextFieldMultiThreadingMore 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 ManagersNote•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.Microwave Example•A silly implementation of a MicrowaveLayout•You’ve used FlowLayout:–FlowLayout is the default–Just lays the controls out in a line,and wraps when necessary, basedon window shapeGrid Layout•GridLayout creates a grid and puts each component in the grid, left to right, top to bottomGrid 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)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: SouthBorder Layout Cont.•setLayout(new BorderLayout())•add() like usual•Widgets don’t grow to fill available spaceFrames•Frames are separate dialog boxes, complete with closing button, maximize, minimize, etc.Creating/Using a Frameframe = new Frame();frame.add(some_widget); // adds a widgetframe.setVisible(true); //shows the frameframe.setLayout(new BorderLayout()); // uses the BorderLayout managerframe.setSize(400,125); //width, heightFrame 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 nameMinimal Frame Listenerpublic 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); } }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.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 layoutpanel.add(new Label(“Set Heat Level”));panel.add(powerSetting);this.add(“Center”, panel)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 managerMore 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 buttonsCheckboxes•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 checkboxCheckbox.getState(); // returns true or falseif checkbox.getState() { System.out.println(“It was checked.”);}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 constructorwavelengthGroup = new CheckboxGroup(); // no argumentsmicrowave = new Checkbox(“Microwave”, wavelengthGroup, true);// last argument controls initial statusinfrared = new Checkbox(“Infrared”, wavelengthGroup, false);•Getting the selected Checkbox:Checkbox selected = wavelengthGroup.getSelectedCheckbox() // note that returns a Checkbox referenceChoice 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 programmaticallySystem.out.println(powerSetting.getSelectedItem()); // prints the item textTextField•Freeform text entrytext = new TextField(“initial contents”, width);System.out.println(text.getText());Text.setText(“Something.”);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


View Full Document

MSU CSE 470 - maawt

Course: Cse 470-
Pages: 21
Download maawt
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view maawt and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view maawt 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?