Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GridBagConstraints;
- import java.awt.GridLayout;
- import java.awt.TextArea;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Window{
- static int i;
- JButton buttons;
- public static void main(String args[]){
- JFrame frame = new JFrame("Calculator");
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- TextArea text = new TextArea(2,25);
- text.setEditable(false);
- JPanel gui = new JPanel(new BorderLayout(5,5));
- frame.setContentPane(gui);
- gui.add(text, BorderLayout.NORTH);
- JPanel panelButtons = new JPanel(new GridLayout(3,0,3,3));
- JPanel buttonPanel = new JPanel();
- buttonPanel.setVisible(true);
- buttonPanel.setPreferredSize(new Dimension(150,200));
- frame.getContentPane().add(buttonPanel);
- Font font = new Font("SansSerif", Font.PLAIN, 30);
- JButton[] buttons = new JButton[10];
- for(i=0; i<buttons.length; i++){
- buttons[i] = new JButton(String.valueOf(i));
- buttons[i].setFont(font);
- }
- JButton plusButton = new JButton("+");
- JButton minusButton = new JButton("-");
- JButton equalsButton = new JButton("=");
- JButton clearButton = new JButton("C");
- ActionListener listener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JButton source = (JButton) e.getSource();
- if(source.getText().equals("1")) {
- text.append("1");
- }
- if(source.getText().equals("2")) {
- text.append("2");
- }
- if(source.getText().equals("3")) {
- text.append("3");
- }
- if(source.getText().equals("4")) {
- text.append("4");
- }
- if(source.getText().equals("0")) {
- text.append("0");
- }
- if(source.getText().equals("5")) {
- text.append("5");
- }
- if(source.getText().equals("6")){
- text.append("6");
- }
- if(source.getText().equals("7")){
- text.append("7");
- }
- if(source.getText().equals("8")){
- text.append("8");
- }
- if(source.getText().equals("9")){
- text.append("9");
- }
- if(source.getText().equals("+")){
- text.append("+");
- }
- if(source.getText().equals("-")){
- text.append("-");
- }
- if(source.getText().equals("C")){
- text.setText(" ");
- }
- if(source.getText().equals("=")){
- String textStuff = text.getText();
- if(textStuff.contains("-")){
- String[] parts = textStuff.split("-");
- String part1 = parts[0];
- String part2 = parts[1];
- double num1 = Double.parseDouble(parts[0]);
- double num2 = Double.parseDouble(parts[1]);
- double total = (num1 - num2);
- String finalNum = Double.toString(total);
- text.setText(finalNum);
- }else{
- String[] parts = textStuff.split("[+]");
- String part1 = parts[0];
- String part2 = parts[1];
- double num1 = Double.parseDouble(parts[0]);
- double num2 = Double.parseDouble(parts[1]);
- double total = (num1 + num2);
- String finalNum = Double.toString(total);
- text.setText(finalNum);
- }
- }
- }
- };
- for(i=0; i<buttons.length; i++){
- buttons[i].addActionListener(listener);
- }
- minusButton.addActionListener(listener);
- plusButton.addActionListener(listener);
- equalsButton.addActionListener(listener);
- clearButton.addActionListener(listener);
- GridBagConstraints c = new GridBagConstraints();
- for(i=0; i<buttons.length; i++){
- panelButtons.add(buttons[i], c);
- }
- panelButtons.add(minusButton);
- panelButtons.add(plusButton);
- panelButtons.add(equalsButton);
- panelButtons.add(clearButton);
- gui.add(panelButtons, BorderLayout.CENTER);
- frame.pack();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement