fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.07s 54652KB
stdin
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator implements ActionListener {
    private JFrame frame;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField resultField;
    private JComboBox<String> operations;
    private JButton colorButton;

    public Calculator() {
        // Create the frame
        frame = new JFrame("Simple Calculator");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        // Create text fields
        textField1 = new JTextField(10);
        textField2 = new JTextField(10);
        resultField = new JTextField(10);
        resultField.setEditable(false);

        // Create operations combo box
        String[] ops = {"+", "-", "*", "/"};
        operations = new JComboBox<>(ops);

        // Create a button to calculate
        JButton calculateButton = new JButton("Calculate");
        calculateButton.addActionListener(this);

        // Create a button to change background color
        colorButton = new JButton("Change Background Color");
        colorButton.addActionListener(e -> changeBackgroundColor());

        // Add components to frame
        frame.add(textField1);
        frame.add(operations);
        frame.add(textField2);
        frame.add(calculateButton);
        frame.add(colorButton);
        frame.add(resultField);

        // Set frame visibility
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            double num1 = Double.parseDouble(textField1.getText());
            double num2 = Double.parseDouble(textField2.getText());
            String operator = operations.getSelectedItem().toString();
            double result = 0;

            switch (operator) {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    if (num2 == 0) {
                        throw new ArithmeticException("Cannot divide by zero, try again");
                    }
                    result = num1 / num2;
                    break;
            }
            resultField.setText(String.valueOf(result));
        } catch (NumberFormatException ex) {
            resultField.setText("Error: Please enter valid numbers");
        } catch (ArithmeticException ex) {
            resultField.setText(ex.getMessage());
        }
    }

    private void changeBackgroundColor() {
        Color newColor = JColorChooser.showDialog(frame, "Choose Background Color", frame.getBackground());
        if (newColor != null) {
            frame.getContentPane().setBackground(newColor);
        }
    }

    public static void main(String[] args) {
        new Calculator();
    }
}
stdout
Standard output is empty