870 likes | 887 Views
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
E N D
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 • 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Layout and Layout Managers • Resizing GridLayout frame Programming and Problem Solving With Java
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Components: TextArea • Like TextField, but allows user to type more than one line Programming and Problem Solving With Java
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
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
Components: TextArea • TextArea methods Programming and Problem Solving With Java
Components: TextArea • Example of using TextArea methods Programming and Problem Solving With Java
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
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
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