Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

write java program for windows based calcu'ator

  1. User

    write java program for windows based calcu'ator

  2. Assistant

    Sorry, as an AI language model, I cannot write and execute the code here. However, here is a sample code for a basic Windows-based calculator:

    import java.awt.; import java.awt.event.; import javax.swing.*;

    public class Calculator extends JFrame implements ActionListener{ private JTextField tfResult; private double num1, num2, result; private String operator;

    public Calculator(){
        super("Calculator");
    
        // Display
        tfResult = new JTextField(12);
        tfResult.setEditable(false);
        add(tfResult, BorderLayout.NORTH);
        JPanel panel = new JPanel(new GridLayout(4, 4));
    
        // Buttons
        String[] buttonTxt = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
        JButton[] button = new JButton[buttonTxt.length];
    
        for(int i = 0; i < buttonTxt.length; i++){
            button[i] = new JButton(buttonTxt[i]);
            panel.add(button[i]);
            button[i].addActionListener(this);
        }
    
        add(panel, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    
    // Action listener
    public void actionPerformed(ActionEvent e){
        String s = e.getActionCommand();
    
        if(s.matches("\\d")){
            if(operator == null){
                tfResult.setText(tfResult.getText() + s);
            }
            else{
                tfResult.setText(s);
                operator = null;
            }
        }
        else if(s.equals(".")){
            if(!tfResult.getText().contains(".")){
                tfResult.setText(tfResult.getText() + ".");
            }
        }
        else if(s.matches("[*/\\-+]")){
            num1 = Double.parseDouble(tfResult.getText());
            operator = s;
        }
        else if(s.equals("=")){
            num2 = Double.parseDouble(tfResult.getText());
    
            if(operator.equals("+")){
                result = num1 + num2;
            }
            else if(operator.equals("-")){
                result = num1 - num2;
            }
            else if(operator.equals("*")){
                result = num1 * num2;
            }
            else if(operator.equals("/")){
                result = num1 / num2;
            }
    
            tfResult.setText(Double.toString(result));
            operator = null;
        }
        else{
            tfResult.setText("");
            operator = null;
        }
    }
    
    public static void main(String[] args){
        new Calculator();
    }
    

    }

    This program creates a simple calculator using Java JFrame and Swing components. It has buttons for basic arithmetic functions like addition, subtraction, multiplication, and division, as well as buttons for numbers 0 through 9 and a decimal point. When the user clicks a button, the corresponding action is performed and displayed on the calculator's text field.