Вы находитесь на странице: 1из 13

package Calculators;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Calculator {

private JFrame frame;

private JTextField txtDisplay;

double firstnum;

double secondnum;

double result;

String operations;

String answer;

/**

* Launch the application.


*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Calculator window = new Calculator();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

});

/**

* Create the application.

*/

public Calculator() {

initialize();

/**

* Initialize the contents of the frame.

*/

private void initialize() {


frame = new JFrame();

frame.setBounds(100, 100, 247, 337);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

txtDisplay = new JTextField();

txtDisplay.setBounds(6, 6, 235, 50);

frame.getContentPane().add(txtDisplay);

txtDisplay.setColumns(10);

JButton btn7 = new JButton("7");

btn7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn7.getText();

txtDisplay.setText(EnterNumber);

});

btn7.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn7.setBounds(6, 110, 50, 50);

frame.getContentPane().add(btn7);

JButton btn8 = new JButton("8");

btn8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn8.getText();


txtDisplay.setText(EnterNumber);

});

btn8.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn8.setBounds(68, 110, 50, 50);

frame.getContentPane().add(btn8);

JButton btn9 = new JButton("9");

btn9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn9.getText();

txtDisplay.setText(EnterNumber);

});

btn9.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn9.setBounds(130, 110, 50, 50);

frame.getContentPane().add(btn9);

JButton btnminus = new JButton("-");

btnminus.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstnum=Double.parseDouble(txtDisplay.getText());

txtDisplay.setText("");

operations="-";

//txtDisplay.setText("-");
}

});

btnminus.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnminus.setBounds(190, 110, 50, 50);

frame.getContentPane().add(btnminus);

JButton btndel = new JButton("←");

btndel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String backspace=null;

if(txtDisplay.getText().length()>0) {

StringBuilder strB=new StringBuilder(txtDisplay.getText());

strB.deleteCharAt(txtDisplay.getText().length()-1);

backspace=strB.toString();

txtDisplay.setText(backspace);

});

btndel.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btndel.setBounds(6, 60, 50, 50);

frame.getContentPane().add(btndel);

JButton btnclr = new JButton("C");

btnclr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

txtDisplay.setText(null);

});

btnclr.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnclr.setBounds(68, 60, 50, 50);

frame.getContentPane().add(btnclr);

JButton btnper = new JButton("%");

btnper.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstnum=Double.parseDouble(txtDisplay.getText());

txtDisplay.setText("");

operations="%";

//txtDisplay.setText("%");

});

btnper.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnper.setBounds(130, 60, 50, 50);

frame.getContentPane().add(btnper);

JButton btnplus = new JButton("+");

btnplus.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstnum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");

operations="+";

//txtDisplay.setText("+");

});

btnplus.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnplus.setBounds(190, 60, 50, 50);

frame.getContentPane().add(btnplus);

JButton btn4 = new JButton("4");

btn4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn4.getText();

txtDisplay.setText(EnterNumber);

});

btn4.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn4.setBounds(6, 160, 50, 50);

frame.getContentPane().add(btn4);

JButton btn5 = new JButton("5");

btn5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn5.getText();

txtDisplay.setText(EnterNumber);
}

});

btn5.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn5.setBounds(68, 160, 50, 50);

frame.getContentPane().add(btn5);

JButton btn6 = new JButton("6");

btn6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn6.getText();

txtDisplay.setText(EnterNumber);

});

btn6.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn6.setBounds(130, 160, 50, 50);

frame.getContentPane().add(btn6);

JButton btnmul = new JButton("*");

btnmul.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstnum=Double.parseDouble(txtDisplay.getText());

txtDisplay.setText("");

operations="*";

//txtDisplay.setText("*");

}
});

btnmul.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnmul.setBounds(190, 160, 50, 50);

frame.getContentPane().add(btnmul);

JButton btn1 = new JButton("1");

btn1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn1.getText();

txtDisplay.setText(EnterNumber);

});

btn1.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn1.setBounds(6, 210, 50, 50);

frame.getContentPane().add(btn1);

JButton btn2 = new JButton("2");

btn2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn2.getText();

txtDisplay.setText(EnterNumber);

});

btn2.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn2.setBounds(68, 210, 50, 50);


frame.getContentPane().add(btn2);

JButton btn3 = new JButton("3");

btn3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn3.getText();

txtDisplay.setText(EnterNumber);

});

btn3.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn3.setBounds(130, 210, 50, 50);

frame.getContentPane().add(btn3);

JButton btndiv = new JButton("/");

btndiv.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstnum=Double.parseDouble(txtDisplay.getText());

txtDisplay.setText("");

operations="/";

//txtDisplay.setText("/");

});

btndiv.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btndiv.setBounds(190, 210, 50, 50);

frame.getContentPane().add(btndiv);
JButton btn0 = new JButton("0");

btn0.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String EnterNumber = txtDisplay.getText() + btn0.getText();

txtDisplay.setText(EnterNumber);

});

btn0.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btn0.setBounds(6, 260, 50, 50);

frame.getContentPane().add(btn0);

JButton btnpoint = new JButton(".");

btnpoint.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(! txtDisplay.getText().contains("."))

txtDisplay.setText(txtDisplay.getText() + btnpoint.getText());

});

btnpoint.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnpoint.setBounds(68, 260, 50, 50);

frame.getContentPane().add(btnpoint);
JButton btnsign = new JButton("±");

btnsign.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

double ops=Double.parseDouble(String.valueOf(txtDisplay.getText()));

ops=ops*(-1);

txtDisplay.setText(String.valueOf(ops));

});

btnsign.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnsign.setBounds(130, 260, 50, 50);

frame.getContentPane().add(btnsign);

JButton btnequal = new JButton("=");

btnequal.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String answer;

secondnum=Double.parseDouble(txtDisplay.getText());

if(operations=="+") {

result=firstnum+secondnum;

answer=String.format("%.2f",result);

txtDisplay.setText(answer);

else if(operations=="-") {

result=firstnum-secondnum;
answer=String.format("%.2f",result);

txtDisplay.setText(answer);

else if(operations=="*") {

result=firstnum*secondnum;

answer=String.format("%.2f",result);

txtDisplay.setText(answer);

else if(operations=="/") {

result=firstnum/secondnum;

answer=String.format("%.2f",result);

txtDisplay.setText(answer);

else if(operations=="%") {

result=firstnum%secondnum;

answer=String.format("%.2f",result);

txtDisplay.setText(answer);

});

btnequal.setFont(new Font("Lucida Grande", Font.BOLD, 18));

btnequal.setBounds(190, 260, 50, 50);

frame.getContentPane().add(btnequal);

Вам также может понравиться