Download presentation
Presentation is loading. Please wait.
1
By Dr. Jiang B. Liu 3. Graphic User Interface using AWT
2
Using the AWT Components The following figures show the inheritance hierarchies for all the AWT component classes.
3
The "Hello Bradley" Applet (with AWT component: button and TextComponent) … public class HelloGUIApplet extends Applet { Button b1, b2; TextField textField; TextArea textArea; public void init() { textField=new TextField("Hello, Bradley!", 20); add(textField); textArea = new TextArea(5, 20); add(textArea); b1 = new Button(); b1.setLabel("Send"); b2 = new Button("Exit"); add(b1); add(b2); }
4
The "Hello Bradley" Applet (with AWT component: button and TextComponent)... public boolean action(Event e, Object arg) { Object target = e.target; if (target == b1) { //They clicked "send button" String text = textField.getText(); textArea.appendText(text + "\n"); return true; } if (target == b2) { //They clicked "exit button" System.exit(0); return true; } return false; }
5
The "Hello Bradley" Application (with AWT component: button and TextComponent) … public class HelloGUI extends Frame {//vs. Applet Button b1, b2; TextField textField; TextArea textArea; public HelloGUI() { //vs. init() super("Hello GUI Application"); setLayout(new FlowLayout()); textField = new TextField("Hello, Bradley!", 20); add(textField); textArea = new TextArea(5, 20); add(textArea); b1 = new Button(); b1.setLabel("Send"); add(b1); b2 = new Button("Exit"); add(b2); }
6
The "Hello Bradley" Application (with AWT component: button and TextComponent) public boolean action(Event e, Object arg) { Object target = e.target; if (target == b1) { //They clicked "send button" String text = textField.getText(); textArea.appendText(text + "\n"); return true; } if (target == b2) { //They clicked "exit button" System.exit(0); return true; } return false; } public static void main(String args[]) { HelloGUI hFrame = new HelloGUI(); hFrame.pack(); hFrame.resize(200, 200); hFrame.show(); }
7
The "Hello Bradley" Application (with BorderLayout manager) public class HelloGUILayout1 extends Frame { Button b1, b2; TextField textField; TextArea textArea; Label infoField; public HelloGUILayout1() { super("Hello GUI Application"); setLayout(new BorderLayout()); textField = new TextField("Hello, Bradley!", 20); add("North", textField); textArea = new TextArea(5, 20); add("Center", textArea); b1 = new Button(); b1.setLabel("Send"); add("East", b1); b2 = new Button("Exit"); add("West", b2); infoField = new Label("BorderLayout Demo"); add("South", infoField); }
8
The "Hello Bradley" Application (with GridLayout manager) public class HelloGUILayout2 extends Frame { Button b1, b2; TextField textField; TextArea textArea; Label infoField; public HelloGUILayout2() { super("Hello GUI Application"); setLayout(new GridLayout(3,2)); textField = new TextField("Hello, Bradley!", 20); add(textField); b1 = new Button(); b1.setLabel("Send"); add(b1); textArea = new TextArea(5, 20); add(textArea); b2 = new Button("Exit"); add(b2); infoField = new Label("GridLayout Demo"); add(infoField); }
9
The "Hello Bradley" Application (with GridBagLayout manager)... GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; textField = new TextField("Hello, Bradley!", 20); gbl.setConstraints(textField, gbc); add(textField); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2; gbc.gridheight = 1; textArea = new TextArea(5, 20); gbl.setConstraints(textArea, gbc); add(textArea); … }
10
The "Pizza" Application public class Pizza extends Frame { // AWT Checkbox components CheckboxGroup sizeCBG; Checkbox smallCheckbox; Checkbox mediumCheckbox; Checkbox largeCheckbox;... public Pizza() { … // create the Pizza size checkbox area sizeCBG = new CheckboxGroup(); smallCheckbox = new Checkbox("Small", sizeCBG, true); smallCheckbox.reshape(10, 30, 120, 40); add(smallCheckbox); mediumCheckbox = new Checkbox("Medium",sizeCBG,false); mediumCheckbox.reshape(140, 30, 120, 40); add(mediumCheckbox); largeCheckbox = new Checkbox("Large", sizeCBG, false); largeCheckbox.reshape(270, 30, 120, 40); add(largeCheckbox);
11
The "Pizza" Application public class Pizza extends Frame { // AWT List and choice Components List toppingList; Choice typeChoice;... public Pizza() { // create the Pizza toppings list toppingList = new List(6,true); toppingList.addItem("Mushroom");... toppingList.addItem("Toufu"); toppingList.reshape(10,70,160,70); add(toppingList); // create the Pizza type choice typeChoice = new Choice(); typeChoice.addItem("Thin Crust");... typeChoice.addItem("Stuff"); typeChoice.reshape(200,70,160,20); add(typeChoice);
12
The "Pizza" Application public class Pizza extends Frame {... public Pizza() { // Order info text fields Label l = new Label("Name", Label.RIGHT); l.reshape(10, 160, 80, 30);add(l); nameField = new TextField(); nameField.reshape(100, 160, 260, 30); add(nameField); l = new Label("Address", Label.RIGHT); l.reshape(10, 200, 80, 30);add(l); addressField = new TextField(); addressField.reshape(100, 200, 260, 30); add(addressField); l = new Label("Phone #", Label.RIGHT); l.reshape(10, 240, 80, 30); add(l); phoneField = new TextField(); phoneField.reshape(100, 240, 260, 30); add(phoneField);...
13
The "Pizza" Application public boolean action(Event evt,Object obj){ if(evt.target == sendButton){ // get customer order String order= new String(); order = nameField.getText(); order += "|";... // determine the order if(smallCheckbox.getState()) { order += "Small"; order += "|";... // get toppings String toppings[] = new String[6]; toppings = toppingList.getSelectedItems(); for (int i = 0; i < toppings.length; i++) { order += toppings[i]; order += " "; }... // send the order to the server statusArea.setText("Sending Order: \n" + order); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.