***WELCOME TO MY BLOG ** More information about me Click Here.

Create Calculator using java swing Design



Calculator Source Code :




import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.management.openmbean.OpenMBeanOperationInfo;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.table.TableStringConverter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Cal {

private JFrame frame;
private JTextField textField;
Double fstnum;
Double secnum;
String answer;
String oparetion;
double result;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cal window = new Cal();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Cal() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 368, 467);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

textField = new JTextField();
textField.setBounds(10, 21, 332, 54);
frame.getContentPane().add(textField);
textField.setColumns(10);

JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn9.getText();
textField.setText(EnterName);
}
});
btn9.setBounds(10, 108, 65, 54);
frame.getContentPane().add(btn9);

JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn8.getText();
textField.setText(EnterName);
}
});
btn8.setBounds(94, 108, 65, 54);
frame.getContentPane().add(btn8);

JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn7.getText();
textField.setText(EnterName);
}
});
btn7.setBounds(184, 108, 65, 54);
frame.getContentPane().add(btn7);

JButton btnclr = new JButton("C");
btnclr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(null);
}
});
btnclr.setBounds(277, 108, 65, 54);
frame.getContentPane().add(btnclr);

JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn4.getText();
textField.setText(EnterName);
}
});
btn4.setBounds(184, 195, 65, 54);
frame.getContentPane().add(btn4);

JButton btnplus = new JButton("+");
btnplus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fstnum=Double.parseDouble(textField.getText());
textField.setText("");
oparetion ="+";
}
});
btnplus.setBounds(277, 195, 65, 54);
frame.getContentPane().add(btnplus);

JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn5.getText();
textField.setText(EnterName);
}
});
btn5.setBounds(94, 195, 65, 54);
frame.getContentPane().add(btn5);

JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn6.getText();
textField.setText(EnterName);
}
});
btn6.setBounds(10, 195, 65, 54);
frame.getContentPane().add(btn6);

JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn3.getText();
textField.setText(EnterName);
}
});
btn3.setBounds(10, 287, 65, 54);
frame.getContentPane().add(btn3);

JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn2.getText();
textField.setText(EnterName);
}
});
btn2.setBounds(94, 287, 65, 54);
frame.getContentPane().add(btn2);

JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn1.getText();
textField.setText(EnterName);
}
});
btn1.setBounds(184, 287, 65, 54);
frame.getContentPane().add(btn1);

JButton btnmins = new JButton("-");
btnmins.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fstnum=Double.parseDouble(textField.getText());
textField.setText("");
oparetion ="-";
}
});
btnmins.setBounds(277, 287, 65, 54);
frame.getContentPane().add(btnmins);

JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String EnterName = textField.getText()+btn0.getText();
textField.setText(EnterName);
}
});
btn0.setBounds(10, 364, 65, 54);
frame.getContentPane().add(btn0);

JButton btndiv = new JButton("/");
btndiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fstnum=Double.parseDouble(textField.getText());
textField.setText("");
oparetion ="/";
}
});
btndiv.setBounds(94, 364, 65, 54);
frame.getContentPane().add(btndiv);




JButton btnequal = new JButton("=");
btnequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
secnum = Double.parseDouble(textField.getText());
if (oparetion=="+") {
result=fstnum+secnum;
answer = String.format("%.2f", result);
textField.setText(answer);

}
else if (oparetion=="-") {
result=fstnum-secnum;
answer = String.format("%.2f", result);
textField.setText(answer);
}
else if (oparetion=="/") {
result=fstnum/secnum;
answer = String.format("%.2f", result);
textField.setText(answer);
}
else if (oparetion=="*") {
result=fstnum*secnum;
answer = String.format("%.2f", result);
textField.setText(answer);
}
}
});
btnequal.setBounds(184, 364, 65, 54);
frame.getContentPane().add(btnequal);





JButton btninto = new JButton("X");
btninto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fstnum=Double.parseDouble(textField.getText());
textField.setText("");
oparetion ="*";
}
});
btninto.setBounds(277, 364, 65, 54);
frame.getContentPane().add(btninto);
}

}







2 comments: