Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Calculator;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JTextField;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JButton;
- import java.awt.Font;
- import javax.swing.JRadioButton;
- public class Calc {
- private JFrame frmStandardCalculator;
- private JTextField txtDisplay;
- double firstnum = 0;
- double secondnum = 0;
- double result;
- String operations;
- String answer;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Calc window = new Calc();
- window.frmStandardCalculator.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Calc() {
- initialize();
- }
- public static int Fact(int n) {
- if (n <= 1)
- return 1;
- else
- return n * Fact(n - 1);
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frmStandardCalculator = new JFrame();
- frmStandardCalculator.setTitle("Calculator");
- frmStandardCalculator.setBounds(100, 100, 275, 367);
- frmStandardCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- frmStandardCalculator.setJMenuBar(menuBar);
- JMenu mnFile = new JMenu("File");
- menuBar.add(mnFile);
- JMenuItem mntmStandard = new JMenuItem("Standard");
- mntmStandard.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frmStandardCalculator.setTitle("Calculator");
- frmStandardCalculator.setBounds(100, 100, 275, 367);
- txtDisplay.setBounds(10, 11, 243, 37);
- }
- });
- mnFile.add(mntmStandard);
- JMenuItem mntmScintific = new JMenuItem("Scientific");
- mntmScintific.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frmStandardCalculator.setTitle("Scientific Calculator");
- frmStandardCalculator.setBounds(100, 100, 459, 367);
- txtDisplay.setBounds(10, 11, 243, 37);
- }
- });
- mnFile.add(mntmScintific);
- JMenuItem mntmExit = new JMenuItem("Exit");
- mntmExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- mnFile.add(mntmExit);
- JMenu mnAbout = new JMenu("About");
- menuBar.add(mnAbout);
- JMenuItem mntmMadeByVadim = new JMenuItem("Made by Vadim Gromov");
- mnAbout.add(mntmMadeByVadim);
- frmStandardCalculator.getContentPane().setLayout(null);
- txtDisplay = new JTextField();
- txtDisplay.setBounds(9, 11, 244, 37);
- frmStandardCalculator.getContentPane().add(txtDisplay);
- txtDisplay.setColumns(10);
- JButton btnNewButton = new JButton("\u2190");
- btnNewButton.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);
- }
- }
- });
- btnNewButton.setBounds(9, 50, 46, 47);
- frmStandardCalculator.getContentPane().add(btnNewButton);
- JRadioButton rdbtnRad = new JRadioButton("Rad");
- JRadioButton rdbtnGrad = new JRadioButton("Deg");
- rdbtnGrad.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- rdbtnRad.setSelected(false);
- rdbtnGrad.setSelected(true);
- }
- });
- rdbtnRad.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- rdbtnRad.setSelected(true);
- rdbtnGrad.setSelected(false);
- }
- });
- JButton btnCe = new JButton("CE");
- btnCe.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = 0;
- secondnum = 0;
- operations = "";
- txtDisplay.setText("");
- }
- });
- btnCe.setFont(new Font("Tahoma", Font.PLAIN, 8));
- btnCe.setBounds(58, 50, 46, 47);
- frmStandardCalculator.getContentPane().add(btnCe);
- JButton btnC = new JButton("C");
- btnC.setFont(new Font("Tahoma", Font.PLAIN, 8));
- btnC.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- txtDisplay.setText("");
- }
- });
- btnC.setBounds(107, 50, 46, 47);
- frmStandardCalculator.getContentPane().add(btnC);
- JButton button_plus_minus = new JButton("\u00B1");
- button_plus_minus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = ops * (-1);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- button_plus_minus.setBounds(156, 50, 46, 47);
- frmStandardCalculator.getContentPane().add(button_plus_minus);
- JButton button_square = new JButton("\u221A");
- button_square.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Math.sqrt(ops);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- button_square.setBounds(206, 50, 46, 47);
- frmStandardCalculator.getContentPane().add(button_square);
- JButton button_seven = new JButton("7");
- button_seven.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_seven.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_seven.setBounds(9, 100, 46, 47);
- frmStandardCalculator.getContentPane().add(button_seven);
- JButton button_eight = new JButton("8");
- button_eight.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_eight.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_eight.setBounds(58, 100, 46, 47);
- frmStandardCalculator.getContentPane().add(button_eight);
- JButton button_nine = new JButton("9");
- button_nine.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_nine.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_nine.setBounds(107, 100, 46, 47);
- frmStandardCalculator.getContentPane().add(button_nine);
- JButton button_del = new JButton("/");
- button_del.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "/";
- }
- });
- button_del.setBounds(156, 100, 46, 47);
- frmStandardCalculator.getContentPane().add(button_del);
- JButton button_prochenti = new JButton("%");
- button_prochenti.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = ops/100;
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- button_prochenti.setBounds(206, 100, 46, 47);
- frmStandardCalculator.getContentPane().add(button_prochenti);
- JButton button_four = new JButton("4");
- button_four.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_four.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_four.setBounds(9, 150, 46, 47);
- frmStandardCalculator.getContentPane().add(button_four);
- JButton button_five = new JButton("5");
- button_five.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_five.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_five.setBounds(58, 150, 46, 47);
- frmStandardCalculator.getContentPane().add(button_five);
- JButton button_six = new JButton("6");
- button_six.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_six.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_six.setBounds(107, 150, 46, 47);
- frmStandardCalculator.getContentPane().add(button_six);
- JButton button_mult = new JButton("*");
- button_mult.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "*";
- }
- });
- button_mult.setBounds(156, 150, 46, 47);
- frmStandardCalculator.getContentPane().add(button_mult);
- JButton button_podlit_x = new JButton("1/x");
- button_podlit_x.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText()));
- ops = 1/ops;
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- button_podlit_x.setFont(new Font("Tahoma", Font.PLAIN, 9));
- button_podlit_x.setBounds(206, 150, 46, 47);
- frmStandardCalculator.getContentPane().add(button_podlit_x);
- JButton button_one = new JButton("1");
- button_one.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_one.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_one.setBounds(9, 200, 46, 47);
- frmStandardCalculator.getContentPane().add(button_one);
- JButton button_two = new JButton("2");
- button_two.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_two.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_two.setBounds(58, 200, 46, 47);
- frmStandardCalculator.getContentPane().add(button_two);
- JButton button_three = new JButton("3");
- button_three.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_three.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_three.setBounds(107, 200, 46, 47);
- frmStandardCalculator.getContentPane().add(button_three);
- JButton button_minus = new JButton("-");
- button_minus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "-";
- }
- });
- button_minus.setBounds(156, 200, 46, 47);
- frmStandardCalculator.getContentPane().add(button_minus);
- JButton button_point = new JButton(".");
- button_point.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String backspace = null;
- if(txtDisplay.getText().length() > 0 && !txtDisplay.getText().contains(".")) {
- StringBuilder strB = new StringBuilder(txtDisplay.getText());
- strB.append('.');
- backspace = strB.toString();
- txtDisplay.setText(backspace);
- }
- }
- });
- button_point.setBounds(107, 250, 46, 47);
- frmStandardCalculator.getContentPane().add(button_point);
- JButton button_plus = new JButton("+");
- button_plus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "+";
- }
- });
- button_plus.setBounds(156, 250, 46, 47);
- frmStandardCalculator.getContentPane().add(button_plus);
- JButton button_zero = new JButton("0");
- button_zero.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String iNum = txtDisplay.getText() + button_zero.getText();
- txtDisplay.setText(iNum);
- }
- });
- button_zero.setBounds(9, 250, 95, 47);
- frmStandardCalculator.getContentPane().add(button_zero);
- JButton button_equal = new JButton("=");
- button_equal.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String answer;
- secondnum = Double.parseDouble(txtDisplay.getText());
- if(operations == "+") {
- result = firstnum + secondnum;
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- if(operations == "-") {
- result = firstnum - secondnum;
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- if(operations == "*") {
- result = firstnum * secondnum;
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- if(operations == "/") {
- result = firstnum / secondnum;
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- if(operations == "pow") {
- result = Math.pow(firstnum, secondnum);
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- if(operations == "sqrt") {
- result = Math.pow(firstnum, (1/secondnum));
- answer = String.format("%f", result).replace(',', '.');
- txtDisplay.setText(answer);
- }
- }
- });
- button_equal.setBounds(206, 200, 46, 97);
- frmStandardCalculator.getContentPane().add(button_equal);
- JButton btnSin = new JButton("sin");
- btnSin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.sin(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.sin(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnSin.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnSin.setBounds(260, 50, 55, 47);
- frmStandardCalculator.getContentPane().add(btnSin);
- JButton btnCos = new JButton("cos");
- btnCos.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.cos(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.cos(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnCos.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnCos.setBounds(260, 100, 55, 47);
- frmStandardCalculator.getContentPane().add(btnCos);
- JButton btnTan = new JButton("tan");
- btnTan.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.tan(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.tan(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnTan.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnTan.setBounds(260, 150, 55, 47);
- frmStandardCalculator.getContentPane().add(btnTan);
- JButton btnLog = new JButton("log");
- btnLog.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Math.log10(ops);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnLog.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnLog.setBounds(260, 200, 55, 47);
- frmStandardCalculator.getContentPane().add(btnLog);
- JButton btnPi = new JButton("\u03C0");
- btnPi.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops;
- ops = Math.PI;
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnPi.setFont(new Font("Centaur", Font.PLAIN, 11));
- btnPi.setBounds(260, 250, 55, 47);
- frmStandardCalculator.getContentPane().add(btnPi);
- JButton btnSinh = new JButton("sinh");
- btnSinh.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.sinh(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.sinh(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnSinh.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnSinh.setBounds(320, 50, 55, 47);
- frmStandardCalculator.getContentPane().add(btnSinh);
- JButton btnCosh = new JButton("cosh");
- btnCosh.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.cosh(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.cosh(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnCosh.setFont(new Font("Tahoma", Font.PLAIN, 10));
- btnCosh.setBounds(320, 100, 55, 47);
- frmStandardCalculator.getContentPane().add(btnCosh);
- JButton btnTanh = new JButton("tanh");
- btnTanh.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- if(rdbtnRad.isSelected() == true) {
- ops = Math.tanh(ops);
- }
- if(rdbtnGrad.isSelected() == true) {
- ops = Math.tanh(Math.toRadians (ops));
- }
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnTanh.setFont(new Font("Tahoma", Font.PLAIN, 10));
- btnTanh.setBounds(320, 150, 55, 47);
- frmStandardCalculator.getContentPane().add(btnTanh);
- JButton btnLn = new JButton("ln");
- btnLn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Math.log(ops);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnLn.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnLn.setBounds(320, 200, 55, 47);
- frmStandardCalculator.getContentPane().add(btnLn);
- JButton btnE = new JButton("e");
- btnE.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops;
- ops = Math.E;
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnE.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnE.setBounds(320, 250, 55, 47);
- frmStandardCalculator.getContentPane().add(btnE);
- JButton btnPow2 = new JButton("x^2");
- btnPow2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Math.pow(ops, 2);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnPow2.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnPow2.setBounds(380, 50, 55, 47);
- frmStandardCalculator.getContentPane().add(btnPow2);
- JButton btnPow3 = new JButton("x^3");
- btnPow3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- double ops = Double.parseDouble(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Math.pow(ops, 3);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnPow3.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnPow3.setBounds(380, 100, 55, 47);
- frmStandardCalculator.getContentPane().add(btnPow3);
- JButton btnXy = new JButton("x^y");
- btnXy.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "pow";
- }
- });
- btnXy.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnXy.setBounds(380, 150, 55, 47);
- frmStandardCalculator.getContentPane().add(btnXy);
- JButton btnSqrtY = new JButton("x^1/y");
- btnSqrtY.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- firstnum = Double.parseDouble(txtDisplay.getText().replace(',', '.'));
- txtDisplay.setText("");
- operations = "sqrt";
- }
- });
- btnSqrtY.setFont(new Font("Tahoma", Font.PLAIN, 8));
- btnSqrtY.setBounds(380, 200, 55, 47);
- frmStandardCalculator.getContentPane().add(btnSqrtY);
- JButton btnFact = new JButton("x!");
- btnFact.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int ops = Integer.parseInt(String.valueOf(txtDisplay.getText().replace(',', '.')));
- ops = Fact(ops);
- txtDisplay.setText(String.valueOf(ops).replace(',', '.'));
- }
- });
- btnFact.setFont(new Font("Tahoma", Font.PLAIN, 11));
- btnFact.setBounds(380, 250, 55, 47);
- frmStandardCalculator.getContentPane().add(btnFact);
- rdbtnRad.setSelected(true);
- rdbtnRad.setBounds(259, 18, 55, 23);
- frmStandardCalculator.getContentPane().add(rdbtnRad);
- rdbtnGrad.setBounds(317, 18, 55, 23);
- frmStandardCalculator.getContentPane().add(rdbtnGrad);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement