sezoka
UX/UI Master
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
This is a simple demo of a functional calculator native mobile application. It is written using Codename One, and can be deployed as a native Android, iOS, Windows Phone, J2ME, BlackBerry, Web, Mac, or Windows App.
The code is as follows:
To run/build this, you just need Eclipse, NetBeans, or Idea IntelliJ with the free NetBeans Codename One plugin installed.
I have included the full Netbeans project in the ZIP file attachment so you should be able to download it and run/build it directly in NetBeans.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
The code is as follows:
- package
com.codename1.demos.calculator
;
- import
com.codename1.ui.Button
;
- import
com.codename1.ui.Component
;
- import
com.codename1.ui.Container
;
- import
com.codename1.ui.Display
;
- import
com.codename1.ui.Form
;
- import
com.codename1.ui.TextField
;
- import
com.codename1.ui.layouts.BorderLayout
;
- import
com.codename1.ui.layouts.GridLayout
;
- import
com.codename1.ui.plaf.UIManager
;
- import
com.codename1.ui.util.Resources
;
- import
java.io.IOException
;
- /**
- * This is a demo Calculator app that performs simple Math. Its purpose
- * is to demonstrate how to create a simple yet functional GUI in a mobile
- * application that runs on iOS, Android, Javascript,
- * @author shannah
- */
- public
class
Calculator {
- private
Form current;
- private
Resources theme;
- /**
- * The current operation
- * '+', '-', '*', or '/'
- */
- private
char
op;
- /**
- * The current calculated value
- */
- private
double
currVal;
- /**
- * The input field
- */
- TextField
inputField;
- /**
- * Flag to indicate that the value has just been reset
- */
- boolean
reset =
true
;
- /**
- * Flag to indicate that input mode is to append digits.
- * If false, the next digit pressed will replace whatever
- * is in the input field.
- */
- boolean
appendDigits =
false
;
- public
void
init(
Object
context)
{
- try
{
- theme =
Resources.openLayered
(
"/theme"
)
;
- UIManager
.getInstance
(
)
.setThemeProps
(
theme.getTheme
(
theme.getThemeResourceNames
(
)
[
0
]
)
)
;
- }
catch
(
IOException
e)
{
- e.printStackTrace
(
)
;
- }
- }
- /**
- * Clears the current calculation.
- */
- private
void
clear(
)
{
- inputField.setText
(
String
.valueOf
(
0
)
)
;
- op =
0
;
- currVal =
0
;
- reset =
true
;
- appendDigits =
false
;
- }
- /**
- * Executes the calculation given the most recent operation that
- * was pressed and displays the value in the input field.
- */
- private
void
exec(
)
{
- double
a =
Double
.parseDouble
(
inputField.getText
(
)
)
;
- if
(
reset)
{
- currVal =
a;
- return
;
- }
else
{
- switch
(
op)
{
- case
'+'
:
currVal +=
a;
break
;
- case
'-'
:
currVal -=
a;
break
;
- case
'*'
:
currVal *=
a;
break
;
- case
'/'
:
currVal /=
a;
break
;
- }
- }
- inputField.setText
(
String
.valueOf
(
currVal)
)
;
- appendDigits =
true
;
- }
- /**
- * Appends the given digit to the input field.
- * @param digit
- */
- private
void
append(
int
digit)
{
- String
s =
digit >=
0
?
String
.valueOf
(
digit)
:
"."
;
- if
(
appendDigits)
{
- inputField.setText
(
inputField.getText
(
)
+
s)
;
- }
else
{
- inputField.setText
(
s)
;
- appendDigits =
true
;
- }
- }
- /**
- * Sets the current operation, and executes it.
- * @param op
- */
- private
void
setOp(
char
op)
{
- exec(
)
;
- this
.op
=
op;
- this
.reset
=
false
;
- appendDigits =
false
;
- }
- public
void
start(
)
{
- if
(
current !=
null
)
{
- current.show
(
)
;
- return
;
- }
- // Create the calculator form
- Form f =
new
Form(
"Calculator"
)
;
- f.setLayout
(
new
BorderLayout
(
)
)
;
- // The input field placed in the north section
- inputField =
new
TextField
(
)
;
- inputField.getDisabledStyle
(
)
.setAlignment
(
Component
.RIGHT
)
;
- inputField.setEnabled
(
false
)
;
- inputField.setText
(
String
.valueOf
(
0
)
)
;
- f.addComponent
(
BorderLayout
.NORTH
, inputField)
;
- // We place the numbers in a GridLayout with 4 rows and 3 columns.
- Container
numbers =
new
Container
(
)
;
- numbers.setLayout
(
new
GridLayout
(
4
,3
)
)
;
- // We'll set up the digits as integers. Only non-negative ints
- // will result in a button being placed for it.
- int
digits[
]
=
{
1
,2
,3
,4
,5
,6
,7
,8
,9
,-
1
,0
}
;
- for
(
int
i=
0
;
i<
digits.length
;
i++
)
{
- int
digit =
digits[
i]
;
- if
(
digit >=
0
)
{
- Button
digitButton =
new
Button
(
String
.valueOf
(
digit)
)
;
- // Add action listener to digit button which appends
- // the current digit to the input.
- digitButton.addActionListener
(
(
e)
->
{
append(
digit)
;
}
)
;
- numbers.addComponent
(
digitButton)
;
- }
else
{
- // If the digit was negative, we just want an empty space in the
- // grid.
- Container
c =
new
Container
(
)
;
- numbers.addComponent
(
c)
;
- }
- }
- // Add a decimal button to the last position in the grid.
- Button
decimalButton =
new
Button
(
"."
)
;
- // We adopt convention that appending -1 results in decimal
- // being placed in the input field.
- decimalButton.addActionListener
(
(
e)
->
{
append(
-
1
)
;
}
)
;
- numbers.addComponent
(
decimalButton)
;
- f.addComponent
(
BorderLayout
.CENTER
, numbers)
;
- // Set up container for the command buttons (+, -, *, etc..)
- // We will place this in the east section in a grid layout.
- Container
commands =
new
Container
(
)
;
- commands.setPreferredW
(
Display.getInstance
(
)
.getDisplayWidth
(
)
/
4
)
;
- commands.setLayout
(
new
GridLayout
(
4
,1
)
)
;
- Button
clear =
new
Button
(
"C"
)
;
- clear.addActionListener
(
(
e)
->
{
clear(
)
;
}
)
;
- commands.addComponent
(
clear)
;
- Button
plus =
new
Button
(
"+"
)
;
- plus.addActionListener
(
(
e)
->
{
setOp(
'+'
)
;
}
)
;
- commands.addComponent
(
plus)
;
- Button
minus =
new
Button
(
"-"
)
;
- minus.addActionListener
(
(
e)
->
{
setOp(
'-'
)
;
}
)
;
- commands.addComponent
(
minus)
;
- Button
mult =
new
Button
(
"x"
)
;
- mult.addActionListener
(
(
e)
->
{
setOp(
'*'
)
;
}
)
;
- commands.addComponent
(
mult)
;
- Button
div =
new
Button
(
"/"
)
;
- div.addActionListener
(
(
e)
->
{
setOp(
'/'
)
;
}
)
;
- commands.addComponent
(
div)
;
- Button
eq =
new
Button
(
"="
)
;
- eq.addActionListener
(
(
e)
->
{
setOp(
'='
)
;
}
)
;
- commands.addComponent
(
eq)
;
- f.addComponent
(
BorderLayout
.EAST
, commands)
;
- // Show the form.
- f.show
(
)
;
- }
- public
void
stop(
)
{
- current =
Display.getInstance
(
)
.getCurrent
(
)
;
- }
- public
void
destroy(
)
{
- }
- }
To run/build this, you just need Eclipse, NetBeans, or Idea IntelliJ with the free NetBeans Codename One plugin installed.
I have included the full Netbeans project in the ZIP file attachment so you should be able to download it and run/build it directly in NetBeans.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view hidden text.