import java.awt.BorderLayout;
public class CalculatorProgram extends JFrame {
private JPanel contentPane;
private JTextField txtfield;
public double intone = 0;
public int lastoperator;
private JButton btnClear;
private JButton subtractBtn;
private JButton devideBtn;
private JButton multiplyBtn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorProgram frame = new CalculatorProgram();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalculatorProgram() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// /////////////////////////////////////////////////////////////////////////////////////Textfield////////////////////////////////////////////////////////////////////////////////////////
txtfield = new JTextField();
txtfield.setFont(new Font("Tahoma", Font.BOLD, 10));
txtfield.setBounds(27, 11, 363, 30);
contentPane.add(txtfield);
txtfield.setColumns(10);
txtfield.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
if (txtfield
.getText()
.equals("You need to set your number you want to calculate with!")
|| txtfield.getText().contains("Calculation is:")
|| !txtfield.getText().isEmpty()) {
txtfield.setText("");
}
}
});
// /////////////////////////////////////////////////////////////////////////////////////Text field////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////Add Button/////////////////////////////////////////////////////////////////////////////////////////
JButton addBtn = new JButton("+");
addBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String l = String.valueOf(intone);
lastoperator = 1;
if (intone == 0) {
try {
intone = Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
} catch (Exception eeeeee) {
}
}else if(intone == Double.parseDouble(txtfield.getText())){
} else {
if(!txtfield.getText().equals(l)){
try {
intone = intone
+ Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
} catch (Exception eeeeeeeeeee) {
}
}
}
}
});
addBtn.setBounds(37, 52, 50, 30);
contentPane.add(addBtn);
// //////////////////////////////////////////////////////////////////////////////////////Add Button/////////////////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////Equals Button/////////////////////////////////////////////////////////////////////////////////////
JButton equalsBtn = new JButton("=");
equalsBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
equalsBtn.setBounds(178, 105, 50, 30);
contentPane.add(equalsBtn);
equalsBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (lastoperator == 1) {
try {
intone = intone
+ Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
lastoperator = 0;
} catch (Exception abd) {
}
} else if (lastoperator == 2) {
try {
intone = intone
- Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
lastoperator = 0;
} catch (Exception aot) {
}
} else if (lastoperator == 3) {
try {
intone = intone
/ Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
lastoperator = 0;
} catch (Exception abbt) {
}
} else if (lastoperator == 4) {
try {
intone = intone
* Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
lastoperator = 0;
} catch (Exception eeeee) {
}
}
}
});
// /////////////////////////////////////////////////////////////////////////////////////Equals button/////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////Clear Button////////////////////////////////////////////////////////////////////////////////////////
btnClear = new JButton("Clear");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Variables Cleared!");
intone = 0;
txtfield.setText("");
}
});
btnClear.setFont(new Font("Tahoma", Font.BOLD, 14));
btnClear.setBounds(172, 162, 70, 30);
contentPane.add(btnClear);
// /////////////////////////////////////////////////////////////////////////////////////Clear Button////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////Subtract Button////////////////////////////////////////////////////////////////////////////////////////
subtractBtn = new JButton("-");
subtractBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
subtractBtn.setBounds(124, 52, 50, 30);
contentPane.add(subtractBtn);
subtractBtn.addActionListener(new ActionListener() {
String ll = String.valueOf(intone);
@Override
public void actionPerformed(ActionEvent arg0) {
lastoperator = 2;
if (intone == 0) {
try {
intone = Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
} catch (Exception a) {
}
}else if(intone == Double.parseDouble(txtfield.getText())){
} else {
try {
if(!txtfield.getText().equals(ll)){
intone = intone
- Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
}
} catch (Exception eeeeeeeeeee) {
}
}
}});
// /////////////////////////////////////////////////////////////////////////////////////Subtract Button////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////Devide Button////////////////////////////////////////////////////////////////////////////////////////
devideBtn = new JButton("/");
devideBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
devideBtn.setBounds(318, 52, 50, 30);
contentPane.add(devideBtn);
devideBtn.addActionListener(new ActionListener() {
String lll = String.valueOf(intone);
@Override
public void actionPerformed(ActionEvent e) {
lastoperator = 3;
if (intone == 0) {
try {
intone = Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
} catch (Exception p) {
}
}else if(intone == Double.parseDouble(txtfield.getText())){
} else {
try {
if(!txtfield.getText().equals(lll)){
intone = intone
/ Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
}
} catch (Exception eeeeeeeeeee) {
}
}
}});
// /////////////////////////////////////////////////////////////////////////////////////Devide Button////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////Multiply Button////////////////////////////////////////////////////////////////////////////////////////
multiplyBtn = new JButton("X");
multiplyBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
multiplyBtn.setBounds(220, 52, 50, 30);
contentPane.add(multiplyBtn);
multiplyBtn.addActionListener(new ActionListener() {
String llll = String.valueOf(intone);
@Override
public void actionPerformed(ActionEvent e) {
lastoperator = 4;
if (intone == 0) {
try {
intone = Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
} catch (Exception t) {
}
}else if(intone == Double.parseDouble(txtfield.getText())){
} else {
try {
if(!txtfield.getText().equals(llll)){
intone = intone
* Double.parseDouble(txtfield.getText());
System.out.println("Setting intone to " + intone);
txtfield.setText("" + intone);
}
} catch (Exception eeeeeeeeeee) {
}
}
}});
// /////////////////////////////////////////////////////////////////////////////////////Multiply Button////////////////////////////////////////////////////////////////////////////////////////
}
}