The calculator - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

The calculator

Description:

Lesson 30: The calculator Java GUI – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 22
Provided by: Berg140
Category:
Tags: calculator | java

less

Transcript and Presenter's Notes

Title: The calculator


1
Lesson 30 The calculator Java GUI
2
Many Buttons
  • // Addition.java
  • import java.awt.event.
  • class Addition implements ActionListener
  • public void actionPerformed(ActionEvent e)
  • System.out.println("The answer is 4")
  • System.exit(0)
  • // Subtraction.java
  • import java.awt.event.
  • class Subtraction implements ActionListener
  • public void actionPerformed(ActionEvent e)
  • System.out.println("The answer is 111")
  • System.exit(0)

RECAP
3
Text input
RECAP
// TextInput.java import java.awt. import
javax.swing. class TextInput public
static void main (String args) JFrame frame
new JFrame("textInput") Container pane
frame.getContentPane() JTextField input new
JTextField("enter text and press return") Echo
listnernew Echo() input.addActionListener(liste
ner) pane.add(input) frame.pack() frame.sh
ow()
4
Text input
RECAP
// Echo.java import java.awt.event. class Echo
implements ActionListener public void
actionPerformed(ActionEvent e) JTextField
source (JTextField)e.getSource() String text
source.getText() System.out.println("text") S
ystem.exit(0)
5
The Calculator
  • In this example we will start using
  • JLabels
  • the GridLayout
  • Typecasting to double from a text input
  • With use of JButtons, JFrame, Containers,
    Listeners and JTextFields we will create a
    calculator java application using the GUI
    components in the swing library

6
The Calculator
7
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Imporing the libraries
Creating the MiniCalc class
The main section of the program
8
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Creating a frame called frame and labeled MiniCalc
Creating a container for the frame
Creating text field based on the JTextField
provided by the library
The length of the field
The name of the field
9
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Creating 3 text fields
Creating a button named addButton based on the
JButton and labeled Add
Creating a button named subButton based on the
JButton and labeled Subtract
10
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Dividing the Container in a grid with 4 rows and
2 columns
11
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Adding a label enter a number to the pane
container
Adding a the textfield firstNumber to the pane
container
12
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Adding all labels, text fields and buttons to the
container
13
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Creating a listener based on the DoMath class (we
will look at this in a little while)
Assigning the listener to each of the buttons
14
// MiniCalc.java - demo gridlayout import
java.awt. import javax.swing. class
MiniCalc public static void main (String
args) JFrame frame new JFrame("MiniCalc") Co
ntainer pane frame.getContentPane() //Creatin
g the major components JTextField firstNumber
new JTextField(20) JTextField secondNumber new
JTextField(20) JTextField result new
JTextField(20) JButton addButton new
JButton("Add") JButton subButton new
JButton("Subtract") pane.setLayout(new
GridLayout(4,2)) pane.add(new JLabel("Enter a
number")) pane.add(firstNumber) pane.add(new
JLabel("Enter a number")) pane.add(secondNumber)
pane.add(new JLabel("Result")) pane.add(resul
t) pane.add(addButton) pane.add(subButton)
DoMath listener new DoMath(firstNumber,
secondNumber, result) subButton.addActionListene
r(listener) addButton.addActionListener(listener
) frame.pack() frame.show()
Determins the width of the column, and therefore
the size of th frame and buttons (we are using
the pack method
Shrink the frame to minimal size
Show the frame with the content in the container
15
Importing the awt.event package from the library
that contains the various event listing
interfaces.
// DoMath.java import javax.swing. import
java.awt.event. class DoMath implements
ActionListener DoMath(JTextField first,
JTextField second, JTextField result) inputOne
first inputTwo second output result
public void actionPerformed(ActionEven
t e) double first, second first
Double.parseDouble(inputOne.getText().trim()) se
cond Double.parseDouble(inputTwo.getText().trim(
)) if (e.getActionCommand().equals("Add"))
output.setText(String.valueOf(firstsecond)) els
e output.setText(String.valueOf(first-secon
d)) private JTextField inputOne,
inputTwo, output
This example use the interface ActionListener to
indicate that DoMath contains the method
actionPerformed().
16
Here we indicate that the class DoMath contains
all methods specified in the interface
ActionListener. That is to say that the class
implements the interface ActionListener
// DoMath.java import javax.swing. import
java.awt.event. class DoMath implements
ActionListener DoMath(JTextField first,
JTextField second, JTextField result) inputOne
first inputTwo second output result
public void actionPerformed(ActionEven
t e) double first, second first
Double.parseDouble(inputOne.getText().trim()) se
cond Double.parseDouble(inputTwo.getText().trim(
)) if (e.getActionCommand().equals("Add"))
output.setText(String.valueOf(firstsecond)) els
e output.setText(String.valueOf(first-secon
d)) private JTextField inputOne,
inputTwo, output
The DoMath method is defined as accepting 3
JTextFields that are translated into 3 fields
called inputOne, inputTwo and output. These are
private variables defined in this class
17
This method is called when the button is clicked.
It is executed through the actionPerformed
method, which , as we covered before, for a
JButton is a mouseclick.
// DoMath.java import javax.swing. import
java.awt.event. class DoMath implements
ActionListener DoMath(JTextField first,
JTextField second, JTextField result) inputOne
first inputTwo second output result
public void actionPerformed(ActionEven
t e) double first, second first
Double.parseDouble(inputOne.getText().trim()) se
cond Double.parseDouble(inputTwo.getText().trim(
)) if (e.getActionCommand().equals("Add"))
output.setText(String.valueOf(firstsecond)) els
e output.setText(String.valueOf(first-secon
d)) private JTextField inputOne,
inputTwo, output
Creating to primitive double variables
Gets the string text
Removing any spaces
Converting a string to a double
18
// DoMath.java import javax.swing. import
java.awt.event. class DoMath implements
ActionListener DoMath(JTextField first,
JTextField second, JTextField result) inputOne
first inputTwo second output result
public void actionPerformed(ActionEven
t e) double first, second first
Double.parseDouble(inputOne.getText().trim()) se
cond Double.parseDouble(inputTwo.getText().trim(
)) if (e.getActionCommand().equals("Add"))
output.setText(String.valueOf(firstsecond)) els
e output.setText(String.valueOf(first-secon
d)) private JTextField inputOne,
inputTwo, output
This returns the actionCommand string associated
with the button. By default this is the label of
the button
If the button clicked is the button labeled Add
we will set the output variable to the string
text of the valueOf (firstsecond)
19
// DoMath.java import javax.swing. import
java.awt.event. class DoMath implements
ActionListener DoMath(JTextField first,
JTextField second, JTextField result) inputOne
first inputTwo second output result
public void actionPerformed(ActionEven
t e) double first, second first
Double.parseDouble(inputOne.getText().trim()) se
cond Double.parseDouble(inputTwo.getText().trim(
)) if (e.getActionCommand().equals("Add"))
output.setText(String.valueOf(firstsecond)) els
e output.setText(String.valueOf(first-secon
d)) private JTextField inputOne,
inputTwo, output
Simple ifelse statement
20
Compiling the files
21
Running the application
Write a Comment
User Comments (0)
About PowerShow.com