1 / 85

The Java Abstract Windowing Toolkit

The Java Abstract Windowing Toolkit. Layout and Layout Managers Components Panel Menus FileDialog Other Capabilities of the AWT. Graphical User Interfaces. Many programs have graphical user interface (GUI) Windows Icons Menus Pointing device Advantages of GUI programs

burson
Download Presentation

The Java Abstract Windowing Toolkit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Java Abstract Windowing Toolkit Layout and Layout Managers Components Panel Menus FileDialog Other Capabilities of the AWT

  2. Graphical User Interfaces • Many programs have graphical user interface (GUI) • Windows • Icons • Menus • Pointing device • Advantages of GUI programs • Looks up-to-date • Desktop metaphor • Most users are familiar with GUI programs Sometimes called the “W.I.M.P.” interface Programming and Problem Solving With Java

  3. Graphical User Interfaces • Development of GUI programs • Used to be very difficult and expensive • Today, visual development tools (like Visual Basic) make GUI development much easier • Just drag components onto the screen with mouse • Still expensive to develop cross-platform GUI apps • Porting tools help a little • Java changes things • Java GUI programs are source and object compatible across platforms • Can develop Java GUI programs with or without visual development tools Programming and Problem Solving With Java

  4. Layout and Layout Managers • Two ways to design graphical user interface in Java • Visual development tool: drag and drop componets on the screen (like Visual Basic) • JBuilder • VisualAge for Java • Visual Café • Visual J++ • Use Java’s layout managers Programming and Problem Solving With Java

  5. Layout manager Automaticallyplacescomponents (buttons, text, ...) on the screen Adjusts components if user resizes the window Layout and Layout Managers This is the BorderLayout layout manager Programming and Problem Solving With Java

  6. Layout and Layout Managers • Java’s AWT has several layout managers predefined • FlowLayout • BorderLayout • GridLayout • GridBagLayout • others... • Also possible to define additional layout managers • Different layout managers place components on the screen in different ways Will look at these three in detail Programming and Problem Solving With Java

  7. Layout and Layout Managers • FlowLayout • Simplest layout manager • Puts components on window like word processor puts words on a page • Left-to-right, top-to-bottom Programming and Problem Solving With Java

  8. Layout and Layout Managers • Flowlayout layout manager adjusts componets when window size changes • Again, just like a word processor Programming and Problem Solving With Java

  9. Layout and Layout Managers • Frame • Window that knows how to resize itself • Moves around on the screen when dragged • Can minimize, maximize Programming and Problem Solving With Java

  10. Layout and Layout Managers • How to use Flowlayout • Import AWT package • import java.awt.*; • Define a new class as a subclass of Frame • class FlowLayoutExample extends Frame • { • ... • } • Constructor steps • // Make title for frame • super("FlowLayout Example"); • // Initialize layout manager • setLayout(new FlowLayout(FlowLayout.CENTER)); • // Put components on frame • add(new Button("Hello")); • add(new Button("There")); • add(new Button("How are you?")); • // Set size of frame and show it • setSize(200, 100); • show(); Programming and Problem Solving With Java

  11. Layout and Layout Managers • // Demonstrates how to use the FlowLayout layout manager. • import java.awt.*; • class FlowLayoutExample extends Frame • { • // Constructor • public FlowLayoutExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("FlowLayout Example"); • // Choose the layout manager and initialize it • setLayout(new FlowLayout(FlowLayout.CENTER)); • // Put components on the frame • add(new Button("Hello")); • add(new Button("There")); • add(new Button("How are you?")); • // Set the size for the frame and display it • setSize(200, 100); • show(); • } • } • public class DemonstrateFlowLayout • { • static public void main(String[] args) • { • new FlowLayoutExample(); • } • } The whole program Programming and Problem Solving With Java

  12. Layout and Layout Managers • Resizing frame rearranges components • Flowlayout layout manager arranges components like a word processor arranges words Programming and Problem Solving With Java

  13. Layout and Layout Managers • Borderlayout • Divides frame into five areas: North, South, East, West, Center • Can put five components on frame Programming and Problem Solving With Java

  14. Layout and Layout Managers • Resizing Borderlayout frame • Height of North andSouth are fixed • Width of East andWest are fixed • Other dimensionsgrow to fill frame Programming and Problem Solving With Java

  15. Layout and Layout Managers • BorderLayout Example • class BorderLayoutExample extends Frame • { • // Constructor • public BorderLayoutExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("BorderLayout Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Put components on the frame • add(new Button("North"), BorderLayout.NORTH); • add(new Button("South"), BorderLayout.SOUTH); • add(new Button("West"), BorderLayout.WEST); • add(new Button("East"), BorderLayout.EAST); • add(new Button("Center"), BorderLayout.CENTER); • // Set the size for the frame and display it • pack(); • show(); • } • } Must specify location pack() makes frame small as possible Programming and Problem Solving With Java

  16. Layout and Layout Managers • GridLayout • Puts components into a matrix from left to right and from top to bottom • Specify size of matrix in instantiation of GridLayout object • Example: make a matrix with three rows and four columns • new GridLayout(3, 4) Programming and Problem Solving With Java

  17. Layout and Layout Managers • Resizing GridLayout frame Programming and Problem Solving With Java

  18. Layout and Layout Managers • GridLayout example • class GridLayoutExample extends Frame • { • // Constructor • public GridLayoutExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("GridLayout Example"); • // Choose the layout manager and initialize it • setLayout(new GridLayout(3, 4)); • // Put components on the frame • add(new Button("1")); • add(new Button("2")); • add(new Button("3")); • add(new Button("4")); • add(new Button("5")); • add(new Button("6")); • add(new Button("7")); • add(new Button("8")); • add(new Button("9")); • add(new Button("0")); • // Set the size for the frame and display it • pack(); • show(); • } • } Programming and Problem Solving With Java

  19. Components • Component: controls that appear on frame • Buttons • Labels • Lists • Text fields • Others • Uses of components • Display information (labels) • Collect information (text fields, lists) • Control program (buttons, menus) Programming and Problem Solving With Java

  20. Components: Label • Label component: put text on frame • class LabelExample extends Frame • { • // Constructor • public LabelExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Label Example"); • // Choose the layout manager and initialize it • setLayout(new FlowLayout(FlowLayout.LEFT)); • // Put components on the frame • add(new Label("First name: ")); • add(new TextField(10)); • add(new Label("Last name: ")); • add(new TextField(15)); • add(new Label("Major: ")); • add(new TextField("Computer Science")); • // Set the size for the frame and display it • setSize(250, 150); • show(); • } • } Programming and Problem Solving With Java

  21. Components: Button • Button component: give user control • Put on frame same as any other component • Must also tell program what to do when button pressed • Event-driven programming • Program responds to events (such as button pushes) • Older style: program has complete control • To connect event (button push) to action • Define listener object, link to button • Listener object responds when button pushed • Same listener object can handle several events • Listener object for button pushes • Must be from class that implements ActionListener • Must have actionPerformed() method Programming and Problem Solving With Java

  22. Components: Button • Example with one button • Will have Listener object be the same as the frame • (With larger programs, better to separate Listener from the frame) • import java.awt.*; • import java.awt.event.*; • class ButtonExample extends Frame implements ActionListener • { • ... • } • Define the button, assign to variable • Button myButton = new Button("Press Me!"); • Need the variable, so can link the button to the listener in the constructor (next slide) Necessary for buttons Programming and Problem Solving With Java

  23. Components: Button • Example with one button (continued) • Constructor puts button on frame, links to listener (which is this object) • // Constructor • public ButtonExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Button Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Put components on the frame • add(myButton, BorderLayout.CENTER); • // Register the button with its listener object • myButton.addActionListener(this); • // Set the size for the frame and display it • setSize(200, 100); • show(); • } Link button to listener Programming and Problem Solving With Java

  24. Components: Button • Example with one button (continued) • Write actionPerformed() method in this class • actionPerformed() responds when button pushed • // actionPerformed: Display "Button pressed" in the command when the • // window user pressed the button. • public void actionPerformed(ActionEvent event) • { • System.out.println("Button pressed"); • } • actionPerformed() method can do anything when button pushed • Open or close file • Display a message • Quit the program • ... Programming and Problem Solving With Java

  25. Components: Button • class ButtonExample extends Frame implements ActionListener • { • Button myButton = new Button("Press Me!"); • // Constructor • public ButtonExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Button Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Put components on the frame • add(myButton, BorderLayout.CENTER); • // Register the button with its listener object • myButton.addActionListener(this); • // Set the size for the frame and display it • setSize(200, 100); • show(); • } • // actionPerformed: Display "Button pressed" in the command • // window when the user presses the button • public void actionPerformed(ActionEvent event) • { • System.out.println("Button pressed"); • } • } Button pressed Button pressed Button pressed Programming and Problem Solving With Java

  26. Components: More than 1 Button • When more than one button • Each button has its own listener, or • One listener can handle several buttons • If one listener & several buttons • Listener must be able to tell which button was pressed • Listener can use getSource() method -- returns component that caused the event • // actionPerformed: Distinguish between yesButton and noButton, and • // display a corresponding message in the command • // window for each • public void actionPerformed(ActionEvent event) • { • if (event.getSource() == yesButton) • { • System.out.println("You pressed yes"); • } • else • { • System.out.println("You pressed no"); • } • } Programming and Problem Solving With Java

  27. Components: More than 1 Button • // Demonstrates one way to distinguish between two buttons in an • // actionPerformed() method • import java.awt.*; • import java.awt.event.*; • class TwoButtonsExample extends Frame implements ActionListener • { • Button yesButton = new Button("Yes"); • Button noButton = new Button("No"); • Label myLabel = new Label(); • // Constructor • public TwoButtonsExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Two Button Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Put components on the frame • add(yesButton, BorderLayout.WEST); • add(noButton, BorderLayout.EAST); • add(myLabel, BorderLayout.SOUTH); • // Register the buttons with the listener object • yesButton.addActionListener(this); • noButton.addActionListener(this); • // Set the size for the frame and display it • setSize(200, 100); • show(); • } Programming and Problem Solving With Java

  28. Components: More than 1 Button • // actionPerformed: Distinguish between yesButton and noButton, • // and display a corresponding message for each • public void actionPerformed(ActionEvent event) • { • if (event.getSource() == yesButton) • { • myLabel.setText("You pressed yes"); • } • else • { • myLabel.setText("You pressed no"); • } • } • } • public class DemonstrateTwoButtons • { • static public void main(String[] args) • { • new TwoButtonsExample(); • } • } Programming and Problem Solving With Java

  29. Components: Checkbox • Checkbox good for getting yes/no responses • Often used for option dialogs • Example from JBuilder • Each checkbox can be independent of others, or can be linked to others Programming and Problem Solving With Java

  30. Components: Checkbox • // Demonstration of how to use check box components. • import java.awt.*; • import java.awt.event.*; • class CheckBoxExample extends Frame implements ActionListener • { • Checkbox airCondCheckBox = new Checkbox("A/C"); • Checkbox cruiseCheckBox = new Checkbox("Cruise"); • Button costButton = new Button("Compute Cost"); • Label costLabel = new Label(); • // Constructor • CheckBoxExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Car Cost"); • // Choose the layout manager and initialize it • setLayout(new GridLayout(4, 1)); • // Put components on the frame • add(airCondCheckBox); • add(cruiseCheckBox); • add(costButton); • add(costLabel); • // Register the buttons with the listener object • costButton.addActionListener(this); • // Set the size for the frame and display it • setSize(150, 100); • show(); • } Programming and Problem Solving With Java

  31. Components: Checkbox • // actionPerformed: Compute the cost of the automobile based • // on the options the user has selected • public void actionPerformed(ActionEvent event) • { • double cost = 15000.0; • if (cruiseCheckBox.getState()) • { • cost = cost + 1000.0; • } • if (airCondCheckBox.getState()) • { • cost = cost + 1500.0; • } • costLabel.setText("Cost: " + cost); • } • } • public class DemonstrateCheckBox • { • static public void main(String[] args) • { • new CheckBoxExample(); • } • } Programming and Problem Solving With Java

  32. Components: Checkbox • Checkbox groups • Can make a group of checkboxes mutually exclusive • If user clicks one, the othersgo blank • Often called “radio buttons”in other developmentenvironments • To put checkbox object in checkbox group • Make a CheckBoxGroup object • Use CheckboxGroup object in Checkbox constructor Programming and Problem Solving With Java

  33. Components: Checkbox • Example • Have CheckBoxGroup paymentGroup • Put cash CheckBox component into that group • Checkbox cashCheckBox = new Checkbox("Cash", false, paymentGroup); • Checkbox constructor arguments • Descriptive text string • true if that checkbox should be checked by default • CheckBoxGroup object Programming and Problem Solving With Java

  34. Components: Checkbox • // Demonstration of how to use a check box group • // for mutually exclusive check box components • import java.awt.*; • import java.awt.event.*; • class CheckBoxExample extends Frame implements ActionListener • { • TextField amountTextField = new TextField(10); • CheckboxGroup paymentGroup = new CheckboxGroup(); • Checkbox cashCheckBox = new Checkbox("Cash", false, paymentGroup); • Checkbox checkCheckBox = new Checkbox("Check", false, paymentGroup); • Checkbox creditCardCheckBox = new Checkbox("Credit card", false, paymentGroup); • Button makePaymentButton = new Button("Make payment"); Programming and Problem Solving With Java

  35. Components: Checkbox • // Constructor • public CheckBoxExample() • { • // Call the constructor for the Frame class with the title • // for the window • super("Payment"); • // Choose the layout manager and initialize it • setLayout(new GridLayout(5, 2)); • // Put components on the frame • add(new Label("Amount")); • add(amountTextField); • add(new Label("Payment type")); • add(cashCheckBox); • add(new Label()); • add(checkCheckBox); • add(new Label()); • add(creditCardCheckBox); • add(makePaymentButton); • // Register the button with the listener object • makePaymentButton.addActionListener(this); • // Set the size for the frame and display it • setSize(250, 200); • show(); • } Programming and Problem Solving With Java

  36. Components: Checkbox • // actionPerformed: Compute the cost of the automobile based • // on the options the user has selected • public void actionPerformed(ActionEvent event) • { • if (cashCheckBox.getState()) • { • System.out.println("Cash payment"); • } • if (checkCheckBox.getState()) • { • System.out.println("Check payment"); • } • if (creditCardCheckBox.getState()) • { • System.out.println("Credit card payment"); • } • } • } • public class DemonstrateCheckBoxGroup • { • static public void main(String[] args) • { • new CheckBoxExample(); • } • } Programming and Problem Solving With Java

  37. Components: Choice • Allows user to pick from a list that’s normally hidden • User presses arrow button to see options • User then clicks on desired option Programming and Problem Solving With Java

  38. Components: Choice • // Demonstration of how to use a choice component • import java.awt.*; • import java.awt.event.*; • class ChoiceExample extends Frame implements ActionListener • { • Button submitButton = new Button("Submit"); • Choice browserChoice = new Choice(); • Label resultLabel = new Label(); • // Constructor • public ChoiceExample() • { • // Call the constructor for the Frame class with window title • super("Choice Example"); • // Choose the layout manager and initialize it • setLayout(new GridLayout(1, 3)); • // Add choices to the choice component • browserChoice.add("Netscape"); • browserChoice.add("Internet Explorer"); • browserChoice.add("HotJava"); • // Put components on the frame • add(browserChoice); • add(submitButton); • add(resultLabel); • // Register the button with the listener object • submitButton.addActionListener(this); • // Set the size for the frame and display it • pack(); • show(); • } Programming and Problem Solving With Java

  39. Components: Choice • // actionPerformed: Display the selected browser name from • // the choice component • public void actionPerformed(ActionEvent event) • { • resultLabel.setText( • browserChoice.getSelectedItem()); • } • } • public class DemonstrateChoice • { • static public void main(String[] args) • { • new ChoiceExample(); • } • } Programming and Problem Solving With Java

  40. Components: TextField • Allows user to type one line of text • Allows user to enter anything • Retrieve text from the field with getText() method Programming and Problem Solving With Java

  41. Components: TextField • // Demonstrates the use of text field components • import java.awt.*; • import java.awt.event.*; • class TextFieldExample extends Frame implements ActionListener • { • TextField firstNameText = new TextField(15); • TextField lastNameText = new TextField(20); • TextField majorText = new TextField("CompSci"); • Button submitButton = new Button("Submit"); • Label resultLabel = new Label(); • // Constructor • public TextFieldExample() • { • // Call the constructor for the Frame class with window title • super("TextField Example"); • // Choose the layout manager and initialize it • setLayout(new GridLayout(4, 2)); Programming and Problem Solving With Java

  42. Components: TextField • // Put components on the frame • add(new Label("First name: ")); • add(firstNameText); • add(new Label("Last name: ")); • add(lastNameText); • add(new Label("Major: ")); • add(majorText); • add(submitButton); • add(resultLabel); • // Register the button with the listener object • submitButton.addActionListener(this); • // Set the size for the frame and display it • pack(); • show(); • } • // actionPerformed: Display the first name, last name, and name • // of major from the three text field areas • public void actionPerformed(ActionEvent event) • { • resultLabel.setText(firstNameText.getText() • + " " + lastNameText.getText() • + " " + majorText.getText()); • } • } • public class DemonstrateTextField • { • static public void main(String[] args) • { • new TextFieldExample(); • } • } Programming and Problem Solving With Java

  43. Components: TextArea • Like TextField, but allows user to type more than one line Programming and Problem Solving With Java

  44. Components: TextArea • // Demonstration of a TextArea component • import java.awt.*; • import java.awt.event.*; • class TextAreaExample extends Frame implements ActionListener • { • TextArea nameAndAddressTextArea = new TextArea(); • Button submitButton = new Button("Submit"); • // Constructor • public TextAreaExample() • { • // Call the constructor for the Frame class with window title • super("TextArea Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Put components on the frame • add(new Label("Name and address"), BorderLayout.WEST); • add(nameAndAddressTextArea, BorderLayout.CENTER); • add(submitButton, BorderLayout.SOUTH); Programming and Problem Solving With Java

  45. Components: TextArea • // Register the button with the listener object • submitButton.addActionListener(this); • // Set the size for the frame and display it • pack(); • show(); • } • // actionPerformed: Display the contents of the TextArea • // component in the command window • public void actionPerformed(ActionEvent event) • { • System.out.println(nameAndAddressTextArea.getText()); • } • } • public class DemonstrateTextArea • { • static public void main(String[] args) • { • new TextAreaExample(); • } • } Programming and Problem Solving With Java

  46. Components: TextArea • TextArea methods Programming and Problem Solving With Java

  47. Components: TextArea • Example of using TextArea methods Programming and Problem Solving With Java

  48. Components: TextArea • // Demonstration of the append(), insert(), and • // replaceRange() methods of the TextArea class. • import java.awt.*; • import java.awt.event.*; • import java.text.*; • class TextAreaExample2 extends Frame implements ActionListener • { • TextArea aTextArea = new TextArea(); • TextField someTextTextField = new TextField(30); • TextField firstPositionTextField = new TextField(); • TextField secondPositionTextField = new TextField(); • Button insertButton = new Button("Insert"); • Button appendButton = new Button("Append"); • Button replaceButton = new Button("Replace"); • Button exitButton = new Button("Exit"); Programming and Problem Solving With Java

  49. Components: TextArea • // Constructor • public TextAreaExample2() • { • // Call the constructor for the Frame class with the title • // for the window • super("TextArea Example"); • // Choose the layout manager and initialize it • setLayout(new BorderLayout()); • // Make a panel for data entry and buttons • Panel controlPanel = new Panel(new GridLayout(5, 2)); • // Put control components on the panel • controlPanel.add(new Label("Text")); • controlPanel.add(someTextTextField); • controlPanel.add(new Label("Position 1")); • controlPanel.add(firstPositionTextField); • controlPanel.add(new Label("Position 2")); • controlPanel.add(secondPositionTextField); • controlPanel.add(insertButton); • controlPanel.add(appendButton); • controlPanel.add(replaceButton); • controlPanel.add(exitButton); Programming and Problem Solving With Java

  50. Components: TextArea • // Put components on the frame • add(aTextArea, BorderLayout.CENTER); • add(controlPanel, BorderLayout.SOUTH); • // Register the buttons with the listener object • insertButton.addActionListener(this); • appendButton.addActionListener(this); • replaceButton.addActionListener(this); • exitButton.addActionListener(this); • // Set the size for the frame and display it • pack(); • show(); • } • // actionPerformed: Handle the event for the associated button • // press • public void actionPerformed(ActionEvent event) • { • // Close the program if exitButton pressed • if (event.getSource() == exitButton) • { • setVisible(false); • dispose(); • System.exit(0); • } Programming and Problem Solving With Java

More Related