Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.samkough.main;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- @SuppressWarnings("serial")
- public class Win extends JFrame implements ActionListener
- {
- private static final int WIDTH = 250;
- private static final int HEIGHT = 175;
- JButton add, subtract, multiply, divide, sqrt;
- JTextField num1, num2;
- JLabel result, enter1, enter2;
- public Win()
- {
- // this layout allows you to create a layout anyway you want basically
- setLayout(new GridBagLayout());
- // this will be for padding and whatnot
- GridBagConstraints c = new GridBagConstraints();
- enter1 = new JLabel("1st: ");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 0;
- add(enter1, c);
- num1 = new JTextField(10);
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 0;
- c.gridwidth = 3;
- add(num1, c);
- enter2 = new JLabel("2nd: ");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 1;
- c.gridwidth = 1;
- add(enter2, c);
- num2 = new JTextField(10);
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 1;
- c.gridwidth = 3;
- add(num2, c);
- add = new JButton("+");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 2;
- c.gridwidth = 1;
- add(add, c);
- subtract = new JButton("-");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 1;
- c.gridy = 2;
- add(subtract, c);
- multiply = new JButton("*");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 2;
- c.gridy = 2;
- add(multiply, c);
- divide = new JButton("/");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 3;
- c.gridy = 2;
- add(divide, c);
- result = new JLabel("");
- c.fill = GridBagConstraints.HORIZONTAL;
- c.gridx = 0;
- c.gridy = 4;
- c.gridwidth = 4;
- add(result, c);
- add.addActionListener(this);
- subtract.addActionListener(this);
- multiply.addActionListener(this);
- divide.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- double n1, n2;
- try
- {
- n1 = Double.parseDouble(num1.getText());
- }
- catch(NumberFormatException e1)
- {
- result.setText("Illegal character for 1st textbox!");
- result.setForeground(Color.RED);
- return;
- }
- try
- {
- n2 = Double.parseDouble(num2.getText());
- }
- catch(NumberFormatException e2)
- {
- result.setText("Illegal character for 2nd textbox!");
- result.setForeground(Color.RED);
- return;
- }
- // this is gonna help validate what button the user presses
- String op = e.getActionCommand();
- // this is the math logic for the symbols
- if (op.equals("+"))
- {
- double sum = n1 + n2;
- result.setText(n1 + " + " + n2 + " = " + sum);
- result.setForeground(Color.BLUE);
- }
- else if (op.equals("-"))
- {
- double difference = n1 - n2;
- result.setText(n1 + " - " + n2 + " = " + difference);
- result.setForeground(Color.BLUE);
- }
- else if (op.equals("*"))
- {
- double product = n1 * n2;
- result.setText(n1 + " * " + n2 + " = " + product);
- result.setForeground(Color.BLUE);
- }
- else if (op.equals("/"))
- {
- // we make an if because you can't divide by zero
- if (n2 == 0)
- {
- result.setText("Cannot divide by zero!");
- result.setForeground(Color.RED);
- }
- else
- {
- double quotient = n1 / n2;
- result.setText(n1 + " / " + n2 + " = " + quotient);
- result.setForeground(Color.BLUE);
- }
- }
- }
- public static void main(String args[])
- {
- Win frame = new Win();
- frame.setVisible(true);
- frame.setSize(WIDTH, HEIGHT);
- // frame.pack();
- frame.setTitle("Calculator");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement