120 likes | 131 Views
This overview provides a summary of the AWT, including its motivation, typical frame setup, layout managers, component class hierarchy, user events, and graphics contexts. Java-AWT language.
E N D
A Brief Overview of the Abstract Windowing Toolkit (AWT) • Motivation. • Typical Frame setup. • BorderLayout, FlowLayout, GridLayout. • Component class hierarchy • User events • Graphics contexts CSE 341, S. Tanimoto Java-AWT-
Motivation 1. A portable graphics model and window system (Unix, Microsoft Windows, Macintosh). 2. Support interaction in a general way (Mouse events, keyboard events). 3. Provide high-level building blocks (widgets like buttons and canvases in JDK1.0 and 1.1, and more complex interface components such as TreeView in JDK 1.2 -- in “Swing”). 4. Permit integration with browsers (Netscape Navigator, Microsoft Internet Explorer).. CSE 341, S. Tanimoto Java-AWT-
Typical Frame Setup // RecipeViewer.java Steve Tanimoto, 12 April 1999. import java.applet.*; import java.awt.*; import java.awt.event.*; public class RecipeViewer extends Frame implements WindowListener, ItemListener { TextArea recipeTextArea; Choice recipeSelector; public static void main( String [] args) { RecipeViewer rv = new RecipeViewer(); rv.init(); rv.show(); } CSE 341, S. Tanimoto Java-AWT-
RecipeViewer.java (Continued) public void init() { setSize(300, 200); addWindowListener(this); // for the close box. setLayout( new BorderLayout() ); add(new Label("The Recipe Viewer"),BorderLayout.NORTH); recipeTextArea = new TextArea(100, 20); add(recipeTextArea, BorderLayout.CENTER); recipeSelector = new Choice(); recipeSelector.addItem("Select recipe"); recipeSelector.addItem("Limoncello"); recipeSelector.addItem("Salsa"); add(recipeSelector, BorderLayout.SOUTH); recipeSelector.addItemListener(this); // for the Choice. } CSE 341, S. Tanimoto Java-AWT-
RecipeViewer.java (Continued) public void itemStateChanged(ItemEvent e) { String item = recipeSelector.getSelectedItem(); if (item.equals("Limoncello")) { recipeTextArea.setText( "To make limoncello, soak the zest of 15 lemons\n" + "for 30 days in 1 liter of 100 proof vodka.\n" + "Mix in 1 liter of cool sugar syrup made by cooking\n" + "1 liter sugar in 1 liter water for 5 min.\n" + "Add 1 more liter vodka, soak for 30 more days, and\n" + "strain out into clean storage bottles."); } else if (item.equals("Salsa")) { recipeTextArea.setText( "To make salsa, wash 3 lbs ripe tomatoes and dice.\n" + "Add 3 minced jalapeno peppers, 1 bunch fresh cilantro\n" + "also minced, 2 minced peeled onions, juice of 2 lemons,\n" + "and 1 teaspoon salt."); } else recipeTextArea.setText("Unknown choice"); } CSE 341, S. Tanimoto Java-AWT-
RecipeViewer.java (Continued) public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } CSE 341, S. Tanimoto Java-AWT-
BorderLayout NORTH WEST EAST CENTER SOUTH CSE 341, S. Tanimoto Java-AWT-
FlowLayout setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10)) CSE 341, S. Tanimoto Java-AWT-
GridLayout setLayout(new GridLayout(3, 2, 5, 5)); CSE 341, S. Tanimoto Java-AWT-
Component Class Hierarchy AWTEvent Container Panel Applet Button Font Window Frame Label FontMetrics TextComponent TextField Dialog Object Color List TextArea FileDialog Choice Graphics Checkbox Component CheckboxGroup MenuComponent LayoutManager CSE 341, S. Tanimoto Java-AWT-
User Events ActionEvent ContainerEvent AdjustmentEvent FocusEvent MouseEvent EventObject AWTEvent ComponentEvent InputEvent KeyEvent ItemEvent PaintEvent TextEvent WindowEvent CSE 341, S. Tanimoto Java-AWT-
Graphics Contexts void paint (Graphics g) { int x=100; int y=50; int w=100; int h=50; int x2=300; int y2=150; g.drawString(“Hello”, x, y); // Draws text g.setFont(new Font(“Helvetica”, Font.BOLD 20)); g.drawString(“Hello again”, x, y2); g.setColor(Color.red); g.drawLine(x1, y1, x2, y2); g.drawRect(x, y, w, h); g.fillRect(x, y, w, h); g.drawOval(x, y, w, h); } CSE 341, S. Tanimoto Java-AWT-