0% found this document useful (0 votes)
51 views

GUI Programming: Reading: Savitch, Chapter 12

This document discusses GUI programming in Java and provides examples of numerical input/output and creating menus. The numerical input/output example shows how to take input from a text field, convert it to a double, and display the result. It also discusses the Label class. The menu example demonstrates how to build a menu system using the MenuBar, Menu, and MenuItem classes to create and link menus and menu items to perform actions like saving memos.

Uploaded by

mhòa_43
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

GUI Programming: Reading: Savitch, Chapter 12

This document discusses GUI programming in Java and provides examples of numerical input/output and creating menus. The numerical input/output example shows how to take input from a text field, convert it to a double, and display the result. It also discusses the Label class. The menu example demonstrates how to build a menu system using the MenuBar, Menu, and MenuItem classes to create and link menus and menu items to perform actions like saving memos.

Uploaded by

mhòa_43
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

35/1

GUI Programming
Reading: Savitch, Chapter 12

35/2
Components, Containers, and
Layout Managers in
AWT(cont)

35/3
Numerical Input and Output
The following example shows the way to
conduct numerical input and output through
GUI.
35/4
Example
//Adder.java
import java.awt.*;
import java.awt.event.*;
/* numerical input and output */
public class Adder extends Frame implements
ActionListener {
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
public static final int X = 80;
public static final int Y = 50;

private TextField inputOutputField;
private double sum = 0;
35/5

public static void main(String[] args) {
Adder guiAdder = new Adder();
guiAdder.setVisible(true);
}

public Adder() {
setTitle("Adding Machine");
addWindowListener(new WindowDestroyer());
setSize(WIDTH, HEIGHT);
setLocation(X,Y);

setLayout(new BorderLayout());
35/6
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.gray);
buttonPanel.setLayout(new FlowLayout());
Button addButton = new Button("Add In");
addButton.addActionListener(this);
buttonPanel.add(addButton);
Button resetButton = new Button("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel, "South");


35/7
Panel textPanel = new Panel();
textPanel.setBackground(Color.blue);
inputOutputField = new TextField(20);
inputOutputField.setBackground(Color.white);
Label prompt = new Label("Enter a number to add");
prompt.setForeground(Color.white);

textPanel.add(prompt, "Left");
textPanel.add(inputOutputField);
add(textPanel, "Center");
}
35/8
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add In")) {
sum = sum +
stringToDouble(inputOutputField.getText());
inputOutputField.setText(Double.toString(sum));
}
else if (e.getActionCommand().equals("Reset")) {
sum = 0;
inputOutputField.setText("0.0");
}
else
inputOutputField.setText("Error in adder code.");
repaint();
}
35/9
private static double stringToDouble(String stringObject) {
return Double.valueOf(stringObject.trim()).doubleValue();
}
}

35/10
Program execution
Java Adder
35/11
35/12
35/13
35/14
Some notes
- The example shows a way to take numerical
input from a text field.

private static double stringToDouble(String stringObject) {
return
Double.valueOf(stringObject.trim()).doubleValue();
}

stringToDouble(inputOutputField.getText());
35/15
- And a way to display a numerical value on a
text field.

inputOutputField.setText(Double.toString(sum));

35/16
- The Label class is defined in API. It is an
object which contains a line of text that can
be added into a container class.
- For example:

Label prompt = new Label("Enter an number to add");
prompt.setForeground(Color.white);
textPanel.add(prompt, "Left");

35/17
MenuBar, Menu and
MenuItem
The following example presents the way to
build up a menu system inside a frame.
35/18
Example
//MenuDemo.java
import java.awt.*;
import java.awt.event.*;
/* save and retrieve memos using a menu. (by W.
Savitch)*/
public class MenuDemo extends Frame implements
ActionListener {
public static final int WIDTH = 500;
public static final int HEIGHT = 300;
public static final int X = 100;
public static final int Y = 30;
private TextArea theText;
private String memo1 = "No Memo 1.";
private String memo2 = "No Memo 2.";
35/19
public MenuDemo() {
setTitle("Memo Saver");
setSize(WIDTH, HEIGHT);
setLocation(X, Y);
setBackground(Color.blue);
addWindowListener(new WindowDestroyer());

Menu comm = new Menu("System Commands");
MenuItem m;
m = new MenuItem("Clear");
m.addActionListener(this);
comm.add(m);

35/20
m = new MenuItem("Exit");
m.addActionListener(this);
comm.add(m);

Menu saveMemo = new Menu("Save Memos");
m = new MenuItem("Save Memo 1");
m.addActionListener(this);
saveMemo.add(m);

m = new MenuItem("Save Memo 2");
m.addActionListener(this);
saveMemo.add(m);
35/21
Menu getMemo = new Menu("Get Memos");
m = new MenuItem("Get Memo 1");
m.addActionListener(this);
getMemo.add(m);
m = new MenuItem("Get Memo 2");
m.addActionListener(this);
getMemo.add(m);

MenuBar mBar = new MenuBar();
mBar.add(comm);
mBar.add(saveMemo);
mBar.add(getMemo);
setMenuBar(mBar);
35/22
Panel textPanel = new Panel();
textPanel.setBackground(Color.blue);
theText = new TextArea(10, 50);
theText.setBackground(Color.white);
textPanel.add(theText);
setLayout(new FlowLayout());
add(textPanel);
}
35/23
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Memo 1"))
memo1 = theText.getText();
else if (actionCommand.equals("Save Memo 2"))
memo2 = theText.getText();
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("Get Memo 1"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
35/24
else if (actionCommand.equals("Exit"))
System.exit(0);
else
theText.setText("Error in memo interface.");

repaint();
}

public static void main(String[] args) {
MenuDemo menuGUI = new MenuDemo();
menuGUI.setVisible(true);
}
}
35/25
Program execution
Java MenuDemo
35/26


35/27
Some note
- MenuBar, Menu and MenuItem (built in API)
are the three classes we normally use to
establish our menu system.
menu
bar
menu
menu
item
35/28
- We add menu items onto a menu; add menus
onto a menu bar; and add the menu bar to
the frame. We then link an action with each
menu item.

You might also like