Java GUI计算器

时间:2015-03-10 19:32:21

标签: java swing user-interface calculator jtextarea

我的JTextArea没有显示,也没有出现任何错误或问题。它是一个Java GUI计算器。

主类代码:

    import javax.swing.JFrame;


    public class calulator_main{

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            calulator_ui frame = new calulator_ui();
            frame.frame.setVisible(true);
        }

    }

计算器代码:

import java.awt.BorderLayout;
import java.awt.FlowLayout;        
import javax.swing.*;        
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class calulator_ui extends calulator_main {

    JFrame frame = new JFrame("cal");

    JPanel panel = new JPanel(new FlowLayout());
    JPanel screen = new JPanel(new BorderLayout());
    JTextArea text = new JTextArea(1,20);
    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button4 = new JButton("4");
    JButton button5 = new JButton("5");
    JButton button6 = new JButton("6");
    JButton button7 = new JButton("7");

    JButton button8 = new JButton("8");
    JButton button9 = new JButton("9");
    JButton button0 = new JButton("0");

    JButton add = new JButton("+");
    JButton subtract = new JButton("-");
    JButton mutliple = new JButton("*");
    JButton divide = new JButton("/");
    JButton equals = new JButton("=");

    JButton clear = new JButton("C");

    JLabel label = new JLabel();



    double numberone,numbertwo,result;


    int addc=0, subc=0,mutlic=0,divc=0;






    public calulator_ui() {
        ui();
        placeComponets();
        createListeners();



    }




    public void placeComponets(){

        screen.add(text, BorderLayout.NORTH);
        panel.add(button0);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(add);
        panel.add(mutliple);
        panel.add(subtract);
        panel.add(divide);
        panel.add(clear);
        panel.add(equals);
        panel.add(label);
    }


    public void ui(){

        frame.setVisible(true); 
        frame.setSize(250,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

    }




    public void createListeners(){

        this.button0.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button1.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button2.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button3.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button4.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button5.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button6.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button7.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button8.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.button9.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.add.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.subtract.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.mutliple.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.divide.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });
        this.equals.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        onCutClicked(ae);
                    }
                });



    }

    void onCutClicked(ActionEvent ae){

        Object source = ae.getSource();

        if(source==button0)
        {
            text.append("0");

        }   

        if(source==button1)
        {
            text.append("1");

        }   

        if(source==button2)
        {
            text.append("2");

        }   

        if(source==button3)
        {
            text.append("3");

        }   

        if(source==button4)
        {
            text.append("4");

        }   

        if(source==button5)
        {
            text.append("5");

        }   

        if(source==button6)
        {
            text.append("6");

        }   

        if(source==button7)
        {
            text.append("7");

        }   

        if(source==button8)
        {
            text.append("8");

        }   

        if(source==button9)
        {
            text.append("9");


        }

        if (source==add)
        {
            numberone=number_reader();
            text.setText("");
            addc=1;
            subc=0;
            mutlic=0;
            divc=0;
        }

        if (source==subtract)
        {
            numberone=number_reader();
            text.setText("");
            addc=0;
            subc=1;
            mutlic=0;
            divc=0;
        }

        if (source==mutliple)
        {
            numberone=number_reader();
            text.setText("");
            addc=0;
            subc=0;
            mutlic=1;
            divc=0;
        }

        if (source==divide)
        {
            numberone=number_reader();
            text.setText("");
            addc=0;
            subc=0;
            mutlic=0;
            divc=1;
        }



        if (source==equals)
        {
            numbertwo=number_reader();
            if (addc>0)
            {
                result=numberone+numbertwo;
                text.setText(Double.toString(result));
            }

            if (subc>0)
            {
                result=numberone-numbertwo;
                text.setText(Double.toString(result));
            }

            if (divc>0)
            {
                result=numberone/numbertwo;
                text.setText(Double.toString(result));
            }

            if (mutlic>0)
            {
                result=numberone*numbertwo;
                text.setText(Double.toString(result));
            }

        }
    }









    public double number_reader()

    {

        double returnnumber;
        String numberstring;
        numberstring=text.getText();
        returnnumber=Double.valueOf(numberstring);

        return returnnumber;


    }


}

有关代码改进的任何建议吗?

3 个答案:

答案 0 :(得分:3)

您正在将JTextArea文本添加到屏幕JPanel,

screen.add(text, BorderLayout.NORTH);

但是你将屏幕添加到任何东西,因此完全可以理解JTextArea不会显示。您应该将JTextArea添加到以某种方式直接或间接添加到JFrame的容器中,否则它不会显示。这里没有魔力 - GUI只会显示你添加的东西。

请注意,由于您的JTextArea被写为单行,为什么不使用JTextField呢?但无论如何,此组件的容器层次结构必须到达顶级窗口。

顺便说一句,请阅读并了解数组和ArrayLists,因为您可以使用它和for循环来大大缩短程序,使其更易于阅读,理解和调试。

请查看this example中的代码,了解我的意思。它将产生这个GUI:
enter image description here

答案 1 :(得分:0)

尝试查看您的代码,您尝试使用参数传递Panel。 使用一个布局,添加将由该特定布局管理的组件,然后添加另一个管理器,后跟其组件。  如您所述声明将使JVM仅使用BorderLayout。

JPanel panel = new JPanel(new FlowLayout());
JPanel screen = new JPanel(new BorderLayout());

答案 2 :(得分:0)

这是另一个计算代码:)

import javax.swing.*;
import java.awt.*;
import java.util.regex.Pattern;

public class Calculator {
private static final int WINDOW_WIDTH = 410;
private static final int WINDOW_HEIGHT = 600;
private static final int BUTTON_WIDTH = 80; // Button width
private static final int BUTTON_HEIGHT = 70; // Button height
private static final int MARGIN_X = 20;
private static final int MARGIN_Y = 60;

private JFrame window; // Main window
private JTextField inText; // Input text
private JButton btnC, btnBack, btnMod, btnDiv, btn7, btn8, btn9,
        btnMul, btn4, btn5, btn6, btnSub, btn1, btn2, btn3, btnAdd, btnPoint, btn0, btnEqual, btnRoot, btnPower, btnLog, btnSwitchThemes, btnSwitchToScientificMode;
private char opt = ' '; // Save the operator
private boolean go = true; // For calculate with Opt != (=)
private boolean addWrite = true; // Racordé des Nombres dans l'Affichage
private double val = 0; // Save value typed for calculation
private boolean isToggleColorSelected = false;
private boolean isScientificMode = false;

/*
    Mx Calculator: 
    X = Row
    Y = Column

    +-------------------+
    |   +-----------+   |   y[0]
    |   |           |   |
    |   +-----------+   |
    |                   |
    |   C  <-   %   /   |   y[1]
    |   7   8   9   *   |   y[2]
    |   4   5   6   -   |   y[3]
    |   1   2   3   +   |   y[4]
    |   .   0     =     |   y[5]
    +-------------------+
     x[0] x[1] x[2] x[3]

*/

/*    
    +-------------------+
    |   +-----------+   |   y[0]
    |   |           |   |
    |   +-----------+   |
    |                   |
    |   0   1   1   3   |   y[1]
    |   4   5   6   7   |   y[2]
    |   8   9   10  11  |   y[3]
    |   12  13  14  15  |   y[4]
    |   16  17    18    |   y[5]
    +-------------------+
     x[0] x[1] x[2] x[3]

*/

private Calculator() {
    window = new JFrame("Calculator");
    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set the width and the Height of the window
    window.setLocationRelativeTo(null); // Move Window To Center

    // Button fonts
    Font btnFont = new Font("Comic Sans MS", Font.PLAIN, 28);
    Font smallTxtBtnFont = new Font("Comic Sans MS", Font.PLAIN, 24);

    btnSwitchThemes = new JButton();
    btnSwitchThemes.setBounds(230, 30, 140, 18);
    btnSwitchThemes.setText("Toggle colors");
    btnSwitchThemes.setBackground(Color.GREEN.darker());
    btnSwitchThemes.setForeground(Color.WHITE);
    btnSwitchThemes.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnSwitchThemes.addActionListener(event -> onSwitchTheme());
    window.add(btnSwitchThemes);

    btnSwitchToScientificMode = new JButton();
    btnSwitchToScientificMode.setBounds(30, 30, 140, 18);
    btnSwitchToScientificMode.setText("Scientific Mode");
    btnSwitchToScientificMode.setBackground(Color.BLUE.darker());
    btnSwitchToScientificMode.setForeground(Color.WHITE);
    btnSwitchToScientificMode.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnSwitchToScientificMode.addActionListener(event -> onShowScientificMode());
    window.add(btnSwitchToScientificMode);

    int j = -1;
    int k = -1;
    int[] x = {MARGIN_X, MARGIN_X + 90, 200, 290, 380};
    int[] y = {MARGIN_Y, MARGIN_Y + 100, MARGIN_Y + 180, MARGIN_Y + 260, MARGIN_Y + 340, MARGIN_Y + 420};

    inText = new JTextField("0");
    inText.setBounds(x[0], y[0], 350, 70);
    inText.setEditable(false);
    inText.setBackground(Color.WHITE);
    inText.setFont(new Font("Comic Sans MS", Font.PLAIN, 33));
    window.add(inText);

    btnC = new JButton("C");
    btnC.setBounds(x[0], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnC.setFont(btnFont);
    btnC.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnC.addActionListener(event -> {
        repaintFont();
        inText.setText("0");
        opt = ' ';
        val = 0;
    });
    window.add(btnC);

    btnBack = new JButton("<-");
    btnBack.setBounds(x[1], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnBack.setFont(btnFont);
    btnBack.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnBack.addActionListener(event -> {
        repaintFont();
        String str = inText.getText();
        StringBuilder str2 = new StringBuilder();
        for (int i = 0; i < (str.length() - 1); i++) {
            str2.append(str.charAt(i));
        }
        if (str2.toString().equals("")) {
            inText.setText("0");
        } else {
            inText.setText(str2.toString());
        }
    });
    window.add(btnBack);

    btnMod = new JButton("%");
    btnMod.setBounds(x[2], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnMod.setFont(btnFont);
    btnMod.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnMod.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '%';
                go = false;
                addWrite = false;
            }
    });
    window.add(btnMod);

    btnDiv = new JButton("/");
    btnDiv.setBounds(x[3], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnDiv.setFont(btnFont);
    btnDiv.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnDiv.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '/';
                go = false;
                addWrite = false;
            } else {
                opt = '/';
            }
    });
    window.add(btnDiv);

    btn7 = new JButton("7");
    btn7.setBounds(x[0], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn7.setFont(btnFont);
    btn7.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn7.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("7");
            } else {
                inText.setText(inText.getText() + "7");
            }
        } else {
            inText.setText("7");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn7);

    btn8 = new JButton("8");
    btn8.setBounds(x[1], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn8.setFont(btnFont);
    btn8.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn8.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("8");
            } else {
                inText.setText(inText.getText() + "8");
            }
        } else {
            inText.setText("8");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn8);

    btn9 = new JButton("9");
    btn9.setBounds(x[2], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn9.setFont(btnFont);
    btn9.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn9.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("9");
            } else {
                inText.setText(inText.getText() + "9");
            }
        } else {
            inText.setText("9");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn9);

    btnMul = new JButton("*");
    btnMul.setBounds(x[3], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnMul.setFont(btnFont);
    btnMul.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnMul.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '*';
                go = false;
                addWrite = false;
            } else {
                opt = '*';
            }
    });
    window.add(btnMul);

    btn4 = new JButton("4");
    btn4.setBounds(x[0], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn4.setFont(btnFont);
    btn4.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn4.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("4");
            } else {
                inText.setText(inText.getText() + "4");
            }
        } else {
            inText.setText("4");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn4);

    btn5 = new JButton("5");
    btn5.setBounds(x[1], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn5.setFont(btnFont);
    btn5.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn5.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("5");
            } else {
                inText.setText(inText.getText() + "5");
            }
        } else {
            inText.setText("5");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn5);

    btn6 = new JButton("6");
    btn6.setBounds(x[2], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn6.setFont(btnFont);
    btn6.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn6.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("6");
            } else {
                inText.setText(inText.getText() + "6");
            }
        } else {
            inText.setText("6");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn6);

    btnSub = new JButton("-");
    btnSub.setBounds(x[3], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnSub.setFont(btnFont);
    btnSub.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnSub.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }

                opt = '-';
                go = false;
                addWrite = false;
            } else {
                opt = '-';
            }
    });
    window.add(btnSub);

    btn1 = new JButton("1");
    btn1.setBounds(x[0], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn1.setFont(btnFont);
    btn1.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn1.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("1");
            } else {
                inText.setText(inText.getText() + "1");
            }
        } else {
            inText.setText("1");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn1);

    btn2 = new JButton("2");
    btn2.setBounds(x[1], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn2.setFont(btnFont);
    btn2.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn2.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("2");
            } else {
                inText.setText(inText.getText() + "2");
            }
        } else {
            inText.setText("2");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn2);

    btn3 = new JButton("3");
    btn3.setBounds(x[2], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn3.setFont(btnFont);
    btn3.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn3.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("3");
            } else {
                inText.setText(inText.getText() + "3");
            }
        } else {
            inText.setText("3");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn3);

    btnAdd = new JButton("+");
    btnAdd.setBounds(x[3], y[4], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnAdd.setFont(btnFont);
    btnAdd.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnAdd.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '+';
                go = false;
                addWrite = false;
            } else {
                opt = '+';
            }
    });
    window.add(btnAdd);

    btnPoint = new JButton(".");
    btnPoint.setBounds(x[0], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnPoint.setFont(new Font("Comic Sans MS", Font.BOLD, 32));
    btnPoint.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnPoint.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            inText.setText(inText.getText() + ".");
        } else {
            inText.setText("0.");
            addWrite = true;
        }
        go = true;
    });
    window.add(btnPoint);

    btn0 = new JButton("0");
    btn0.setBounds(x[1], y[5], BUTTON_WIDTH, BUTTON_HEIGHT);
    btn0.setFont(btnFont);
    btn0.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btn0.addActionListener(event -> {
        repaintFont();
        if (addWrite) {
            if (Pattern.matches("[0]*", inText.getText())) {
                inText.setText("0");
            } else {
                inText.setText(inText.getText() + "0");
            }
        } else {
            inText.setText("0");
            addWrite = true;
        }
        go = true;
    });
    window.add(btn0);

    btnEqual = new JButton("=");
    btnEqual.setBounds(x[2], y[5], 2 * BUTTON_WIDTH + 10, BUTTON_HEIGHT);
    btnEqual.setFont(btnFont);
    btnEqual.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnEqual.addActionListener(event -> {
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '=';
                addWrite = false;
            }
    });
    window.add(btnEqual);

    btnRoot = new JButton("√");
    btnRoot.setBounds(x[4], y[1], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnRoot.setFont(btnFont);
    btnRoot.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnRoot.addActionListener(event -> {
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = Math.sqrt(Double.parseDouble(inText.getText()));
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '√';
                addWrite = false;
            }
    });
    btnRoot.setVisible(false);
    window.add(btnRoot);

    btnPower = new JButton("pow");
    btnPower.setBounds(x[4], y[2], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnPower.setFont(smallTxtBtnFont);
    btnPower.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnPower.addActionListener(event -> {
        repaintFont();
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = calc(val, inText.getText(), opt);
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = '^';
                go = false;
                addWrite = false;
            } else {
                opt = '^';
            }
    });
    btnPower.setVisible(false);
    window.add(btnPower);

    btnLog = new JButton("log");
    btnLog.setBounds(x[4], y[3], BUTTON_WIDTH, BUTTON_HEIGHT);
    btnLog.setFont(smallTxtBtnFont);
    btnLog.setCursor(new Cursor(Cursor.HAND_CURSOR));
    btnLog.addActionListener(event -> {
        if (Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)", inText.getText()))
            if (go) {
                val = Math.log(Double.parseDouble(inText.getText()));
                if (Pattern.matches("[-]?[\\d]+[.][0]*", String.valueOf(val))) {
                    inText.setText(String.valueOf((int) val));
                } else {
                    inText.setText(String.valueOf(val));
                }
                opt = 'l';
                addWrite = false;
            }
    });
    btnLog.setVisible(false);
    window.add(btnLog);

    window.setLayout(null);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // If Click into The Red Button => End The Processes
    window.setVisible(true);
}

private double calc(double x, String input, char opt) {
    inText.setFont(inText.getFont().deriveFont(Font.BOLD));
    double y = Double.parseDouble(input);
    if (opt == '+') {
        return x + y;
    } else if (opt == '-') {
        return x - y;
    } else if (opt == '*') {
        return x * y;
    } else if (opt == '/') {
        return x / y;
    } else if (opt == '%') {
        return x % y;
    } else if (opt == '^') {
        return Math.pow(x, y);
    }
    inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
    return y;
}

private void repaintFont() {
    inText.setFont(inText.getFont().deriveFont(Font.PLAIN));
}

private void onSwitchTheme() {
    if (isToggleColorSelected) {
        btnSwitchThemes.setText("Toggle colors");
        btnSwitchThemes.setBackground(Color.GREEN.darker());
        btnSwitchThemes.setForeground(Color.WHITE);
        btnC.setBackground(null);
        btnBack.setBackground(null);
        btnMod.setBackground(null);
        btnDiv.setBackground(null);
        btnMul.setBackground(null);
        btnSub.setBackground(null);
        btnAdd.setBackground(null);
        btnRoot.setBackground(null);
        btnLog.setBackground(null);
        btnPower.setBackground(null);
        btnEqual.setBackground(null);
        btn0.setBackground(null);
        btn1.setBackground(null);
        btn2.setBackground(null);
        btn3.setBackground(null);
        btn4.setBackground(null);
        btn5.setBackground(null);
        btn6.setBackground(null);
        btn7.setBackground(null);
        btn8.setBackground(null);
        btn9.setBackground(null);
        btnPoint.setBackground(null);

        btnC.setForeground(Color.BLACK);
        btnBack.setForeground(Color.BLACK);
        btnMod.setForeground(Color.BLACK);
        btnDiv.setForeground(Color.BLACK);
        btnMul.setForeground(Color.BLACK);
        btnSub.setForeground(Color.BLACK);
        btnAdd.setForeground(Color.BLACK);
        btnEqual.setForeground(Color.BLACK);
        btnLog.setForeground(Color.BLACK);
        btnPower.setForeground(Color.BLACK);
        btnRoot.setForeground(Color.BLACK);

        isToggleColorSelected = false;
    } else {
        btnSwitchThemes.setText("Untoggle colors");
        btnSwitchThemes.setBackground(null);
        btnSwitchThemes.setForeground(Color.BLACK);
        btnC.setBackground(Color.RED);
        btnBack.setBackground(Color.ORANGE);
        btnMod.setBackground(Color.GREEN);
        btnDiv.setBackground(Color.PINK);
        btnMul.setBackground(Color.PINK);
        btnSub.setBackground(Color.PINK);
        btnAdd.setBackground(Color.PINK);
        btnRoot.setBackground(Color.PINK);
        btnLog.setBackground(Color.PINK);
        btnPower.setBackground(Color.PINK);
        btnEqual.setBackground(Color.BLUE);
        btn0.setBackground(Color.WHITE);
        btn1.setBackground(Color.WHITE);
        btn2.setBackground(Color.WHITE);
        btn3.setBackground(Color.WHITE);
        btn4.setBackground(Color.WHITE);
        btn5.setBackground(Color.WHITE);
        btn6.setBackground(Color.WHITE);
        btn7.setBackground(Color.WHITE);
        btn8.setBackground(Color.WHITE);
        btn9.setBackground(Color.WHITE);
        btnPoint.setBackground(Color.WHITE);

        btnC.setForeground(Color.WHITE);
        btnBack.setForeground(Color.WHITE);

        btnMod.setForeground(Color.WHITE);
        btnDiv.setForeground(Color.WHITE);
        btnMul.setForeground(Color.WHITE);
        btnSub.setForeground(Color.WHITE);
        btnAdd.setForeground(Color.WHITE);
        btnEqual.setForeground(Color.WHITE);
        btnLog.setForeground(Color.WHITE);
        btnPower.setForeground(Color.WHITE);
        btnRoot.setForeground(Color.WHITE);
        isToggleColorSelected = true;
    }
}

private void onShowScientificMode() {
    if (isScientificMode) {
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        btnRoot.setVisible(false);
        btnPower.setVisible(false);
        btnLog.setVisible(false);
        isScientificMode = false;
    } else {
        window.setSize(WINDOW_WIDTH + 80, WINDOW_HEIGHT);
        btnRoot.setVisible(true);
        btnPower.setVisible(true);
        btnLog.setVisible(true);
        isScientificMode = true;
    }
}

public static void main(String[] args) {
    new Calculator();
}

}