Description : A simple Java Swing Calculator, Example for setBounds in java, Example for actionListener interface. Here i tried to Implement a simple Java Calculator. copy and paste the code to a notepad. compile the program with javac and run using command prompt java calculator
Simple Java Calculator. This project was originally written using Eclipse, but I have migrated it to NetBeans. This calculator is simple with an easy code to help novices learn how to create a calculator program with Java. Java NetBeans Tutorial_How to Create a Calculator DJ Oamen. To see Java NetBeans Calculator Tutorial using IF Statement see link below. 10 Programming Languages in ONLY 15 minutes! 1 Build a Calculator With Case Statements Using Java. The Java programming language is deployed on a variety of computing platforms that application development businesses can leverage for. NetBeans and Java Forms Developing a Graphic User Interface (GUI) using Java can be an art-form in itself, as there's quite a lot to get used to: Components, Containers, Layout Managers, and a whole lot more besides.
Author +belazy
RMI Calculator using Java in Netbeans Back. This video is about RMI program in java using netbeans. Netbeans makes your job really simple by loading the rmi registry unlike we have to load the registry by ourselves by using rmic tool, here we just have to run the server and the client file. Create A Grocery Store Calculator In Netbeans GUI Nov 7, 2014. I need to create a grocery store calculator in Netbeans GUI. It should function as follows: 1. Click 'Checkout'. I have got a problem when coding a java program to create a calculator with JCreator. How to write codes for decimal point.
/* this is a program to make a
using java program */
/* For SNR Sons Lab exam ...
* @author belazy
*/
import java.awt.*;
import java.awt.event.*;
public class
Calculator
extends Frame implements ActionListener{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ex,p1,eq,min,div,mul,cl,back;
TextField t;
int n,i;
int n1,n2;
int ans;
Calculator()
{
this.setLayout(null);
b0=new Button(' 0 ');
b1=new Button(' 1 ');
b2=new Button(' 2 ');
b3=new Button(' 3 ');
b4=new Button(' 4 ');
b5=new Button(' 5 ');
b6=new Button(' 6 ');
b7=new Button(' 7 ');
b8=new Button(' 8 ');
b9=new Button(' 9 ');
ex=new Button('exit');
p1=new Button('+');
min=new Button('-');
mul=new Button('X');
div=new Button('/');
eq=new Button('=');
cl=new Button('AC');
back=new Button('clear');
t=new TextField(40);
b0.setBounds(130,190,30,30);
b1.setBounds(100,100,30,30);
b2.setBounds(130,100,30,30);
b3.setBounds(160,100,30,30);
b4.setBounds(100,130,30,30);
b5.setBounds(130,130,30,30);
b6.setBounds(160,130,30,30);
b7.setBounds(100,160,30,30);
b8.setBounds(130,160,30,30);
b9.setBounds(160,160,30,30);
ex.setBounds(100,190,30,30);
p1.setBounds(190,100,30,30);
min.setBounds(190,130,30,30);
mul.setBounds(190,160,30,30);
div.setBounds(190,190,30,30);
eq.setBounds(160,190,30,30);
cl.setBounds(220,100,30,30);
back.setBounds(220,130,30,30);
t.setBounds(100,60,150,30);
this.add(b0);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(b6);
this.add(b7);
this.add(b8);
this.add(b9);
this.add(ex);
this.add(p1);
this.add(mul);
this.add(min);
this.add(div);
this.add(eq);
this.add(cl);
this.add(back);
this.add(t);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
ex.addActionListener(this);
p1.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
min.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
back.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()ex)
{
System.exit(0);
}
if(ae.getSource()p1) // while pressing plus
{
n1=Integer.parseInt(t.getText());
i=1; // this expression is for equals
t.setText('0');
}
if(ae.getSource()min) // while pressing minus
{
n1=Integer.parseInt(t.getText());
i=2;
t.setText('0');
}
if(ae.getSource()mul) // while pressing multiplication
{
n1=Integer.parseInt(t.getText());
i=3;
t.setText('0');
}
if(ae.getSource()div) // while pressing division
{
n1=Integer.parseInt(t.getText());
i=4;
t.setText('0');
}
if(ae.getSource()cl) // while pressing all clear
{
//n1=Integer.parseInt(t.getText());
//i=4;
t.setText('0');
}
if (ae.getSource()back) // for back space
{
//if (!(t.getText().equals('0') && t.getText().length() > 1)
t.setText(t.getText().substring(0,t.getText().length()-1));
}
if(ae.getSource()b0)
{
if(t.getText().equals('0'))
t.setText('0');
else
t.setText(t.getText() +'0');
}
if(ae.getSource()b1)
{
if(t.getText().equals('0'))
t.setText('1');
else
t.setText(t.getText() +'1');
}
if(ae.getSource()b2)
{
if(t.getText().equals('0'))
t.setText('2');
else
t.setText(t.getText() +'2');
}
if(ae.getSource()b3)
{
if(t.getText().equals('0'))
t.setText('3');
else
t.setText(t.getText() +'3');
}
if(ae.getSource()b4)
{
if(t.getText().equals('0'))
t.setText('4');
else
t.setText(t.getText() +'4');
}
if(ae.getSource()b5)
{
if(t.getText().equals('0'))
t.setText('5');
else
t.setText(t.getText() +'5');
}
if(ae.getSource()b6)
{
if(t.getText().equals('0'))
t.setText('6');
else
t.setText(t.getText() +'6');
}
if(ae.getSource()b7)
{
if(t.getText().equals('0'))
t.setText('7');
else
t.setText(t.getText() +'7');
}
if(ae.getSource()b8)
{
if(t.getText().equals('0'))
t.setText('8');
else
t.setText(t.getText() +'8');
}
if(ae.getSource()b9)
{
if(t.getText().equals('0'))
t.setText('9');
else
t.setText(t.getText() +'9');
}
System.out.println(t.getText());
if(ae.getSource()eq) //while pressing equals
{
n2=Integer.parseInt(t.getText());
switch(i)
{
case 1:
{
ans=n1+n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 2:
{
ans=n1-n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 3:
{
ans=n1*n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
case 4:
{
ans=n1/n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;
}
}
}
public static void main(String free[])
{
Calculator c= new Calculator();
c.setTitle('
simple java calculator source code
');c.setSize(600,600);
c.setVisible(true);
}
}
Hi Friends,
This is my first program in java. so please try it out and find the issues. Thanks to ravi, vishnu , madhu, bithesh and nithin for support
/* contact me at belazy1987atgmail.com */
Most selling calculator in amazon ( Price around 1000) The best one, Buy this , worth buying.
fx-991 Ex ( Casio - the best calculator makers )
Hurry !! Before stock out
Another Program send by my friend,
simple java calculator source code
import javax.swing.*;import javax.swing.JOptionPane;
![Scientific calculator program in java using netbeans Scientific calculator program in java using netbeans](/uploads/1/2/6/4/126442172/770586278.png)
import java.awt.event.*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet {
public void init() {
CalculatorPanel calc=new CalculatorPanel();
getContentPane().add(calc);
}
}
class CalculatorPanel extends JPanel implements ActionListener {
JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
static JTextField result=new JTextField('0',45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;
private static void assign(String no)
{
if((result.getText()).equals('0'))
result.setText(no);
else if(lastCommand'=')
{
result.setText(no);
lastCommand=null;
}
else
result.setText(result.getText()+no);
}
public CalculatorPanel() {
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300,200);
add(result,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4));
n7=new JButton('7');
panel.add(n7);
n7.addActionListener(this);
n8=new JButton('8');
panel.add(n8);
n8.addActionListener(this);
n9=new JButton('9');
panel.add(n9);
n9.addActionListener(this);
div=new JButton('/');
panel.add(div);
div.addActionListener(this);
n4=new JButton('4');
panel.add(n4);
n4.addActionListener(this);
n5=new JButton('5');
panel.add(n5);
n5.addActionListener(this);
n6=new JButton('6');
panel.add(n6);
n6.addActionListener(this);
mul=new JButton('*');
panel.add(mul);
mul.addActionListener(this);
n1=new JButton('1');
panel.add(n1);
n1.addActionListener(this);
n2=new JButton('2');
panel.add(n2);
n2.addActionListener(this);
n3=new JButton('3');
panel.add(n3);
n3.addActionListener(this);
minus=new JButton('-');
panel.add(minus);
minus.addActionListener(this);
dot=new JButton('.');
panel.add(dot);
dot.addActionListener(this);
Creating A Calculator In Java
n0=new JButton('0');panel.add(n0);
n0.addActionListener(this);
equal=new JButton('=');
panel.add(equal);
equal.addActionListener(this);
plus=new JButton('+');
panel.add(plus);
plus.addActionListener(this);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()n1) assign('1');
else if(ae.getSource()n2) assign('2');
else if(ae.getSource()n3) assign('3');
else if(ae.getSource()n4) assign('4');
else if(ae.getSource()n5) assign('5');
else if(ae.getSource()n6) assign('6');
else if(ae.getSource()n7) assign('7');
else if(ae.getSource()n8) assign('8');
else if(ae.getSource()n9) assign('9');
else if(ae.getSource()n0) assign('0');
else if(ae.getSource()dot)
{
if(((result.getText()).indexOf('.'))-1)
result.setText(result.getText()+'.');
}
else if(ae.getSource()minus)
{
preRes=Double.parseDouble(result.getText());
lastCommand='-';
result.setText('0');
}
else if(ae.getSource()div)
{
preRes=Double.parseDouble(result.getText());
lastCommand='/';
result.setText('0');
}
else if(ae.getSource()equal)
{
secVal=Double.parseDouble(result.getText());
if(lastCommand.equals('/'))
res=preRes/secVal;
else if(lastCommand.equals('*'))
res=preRes*secVal;
else if(lastCommand.equals('-'))
res=preRes-secVal;
else if(lastCommand.equals('+'))
res=preRes+secVal;
result.setText(' '+res);
lastCommand='=';
}
else if(ae.getSource()mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand='*';
result.setText('0');
}
else if(ae.getSource()plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand='+';
result.setText('0');
}
}
}
The above is an example code for java swing scientific calculator, copy and paste it as calculator.java, compile the code and run it.
Please provide your feedback.
Java applet tutorial
Similar posts
- calendar using java swing
Sponsors
Now that we’ve completed our Hello World tutorial let’s move on to another awesome app we’re going to build called CalculatorFX—a simple calculator done up twice. First we’ll code the screen directly with a single class file and then we’ll use FXML Scene Builder. Try both and then you can judge which approach you like better.
Our final app will look like:
In this tutorial, we’re going to cover the Java Code approach. We will cover the FXML approach in the next article.
As we move on to build our app, we will make use of GridPane as our root container. Try to see the application like this, with rows and columns starting at 0, 0.
We start by declaring the control elements we will need at the top of the Application. This goes above the start method:
Now we create each control and give it a label. Notice how we style the answer label using css-like syntax to attach a border and padding. Very cool!
Using GridPane
Now that we have the elements ready, we will add them to a new GridPane. The GridPane uses a column-row style where we can tell it which cell we want to add the elements to. Notice that last 2 controls span across 2 columns. We also set the pane to the middle of the scene (which we will make later):
To layout the app properly, I iterated on each control and set how wide it should be. To keep things modular in the ‘start’ method, I added a separate method called ‘setWidths’. I also added a way to tell each button what to do when it gets clicked, so again a separate method is used to attach the code to the button. The ‘start’ method then finishes with the usual code that NetBeans provides for you.
The attach-code method tells each button to run the same method when it is clicked upon. If we send in ‘e’ (the click event) to the btnCode method, we can use it to tell us which button was pressed. That way we can use 1 method for all 5 buttons.
![Calculator Program Java Using Netbeans Calculator Program Java Using Netbeans](/uploads/1/2/6/4/126442172/113182829.jpg)
Here is the first part of the method that runs whenever a button is clicked. If we click the clear button we will erase the text in the textfields placing a ? in the labelReturn that prevents if from going any further.
We start by reading in 2 integer numbers from the 2 text fields (this can throw an error which I am not catching). Depending on the button clicked it will either add, subtract, divide or multiply to get an answer. I also use a char variable to represent the kind of calculation I am doing. Then I put the answer together in the label along with the symbol.
The final app looks just like what we showed in the beginning. Let us put it again here, with an input and output.
Let’s pin down some of the important things to note regardign the basic structure of any JavaFX application:
- The main class for a JavaFX application extends the javafx.application.Application class. The start() method is the main entry point for all JavaFX applications.
- A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. In JavaFX, the content of the scene is represented as a hierarchical scene graph of nodes. In this example, the root node is a StackPane object, which is a resizable layout node. This means that the root node's size tracks the scene's size and changes when the stage is resized by a user.
- The root node contains one child node, a button control with text, plus an event handler to print a message when the button is pressed.
- The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.
This project was straightforward but quite interesting. You can find all the source code for this project in this Github project. It is a netbeans based project so it should be easy to import into Netbeans and run as is.
{{CommentsModel.TotalCount}} Comments
Your Comment
Recent Stories
Top DiscoverSDK Experts
3355
Web and Windows developer
View Profile3220
Experienced with Ad network & Ad servers.
View Profile3060
7 years in Cross-Platform development.
View ProfileShow AllFeatured Products
Calculator Program Java Code
;
;
;
Compare Products
Select up to threetwo products to compare by clicking on the compare icon () of each product.