import java.awt.Color; import java.awt.FlowLayout; import java.awt.LayoutManager; import java.awt.TextField; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.text.ParseException; import java.util.Scanner; public class calculator_gui implements ActionListener { JFrame frame = new JFrame("Calculator"); JPanel Panel = new JPanel (new java.awt.FlowLayout()); JTextArea text = new JTextArea(1,20); JButton but1= new JButton("1"); JButton but2= new JButton("2"); JButton but3= new JButton("3"); JButton but4= new JButton("4"); JButton but5= new JButton("5"); JButton but6= new JButton("6"); JButton but7= new JButton("7"); JButton but8= new JButton("8"); JButton but9= new JButton("9"); JButton but0= new JButton("0"); JButton butadd= new JButton("+"); JButton butsub= new JButton("-"); JButton butmulti= new JButton("*"); JButton butdiv= new JButton("/"); JButton buteq= new JButton("="); JButton butclear= new JButton("C"); Double number1,number2,result; int addc=0,subc=0,multic=0,divc=0; public void gui(){ Panel.setLayout(FlowLayout()); frame.setVisible(true); frame.setBounds(100, 100, 450, 285); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.setResizable(false); frame.add(Panel); Panel.setBackground(Color.green); Panel.add(text); text.setBounds(10, 32, 361, 29); Panel.add(but1); but1.setBackground(Color.red); but1.setBounds(10, 81, 89, 23); Panel.add(but2); but2.setBounds(126, 81, 89, 23); Panel.add(but3); but3.setBounds(225, 81, 89, 23); Panel.add(but4); but4.setBounds(10, 115, 89, 23); Panel.add(but5); but5.setBounds(126, 115, 89, 23); Panel.add(but6); but6.setBounds(225, 115, 89, 23); Panel.add(but7); but7.setBounds(10, 149, 89, 23); Panel.add(but8); but8.setBounds(126, 149, 89, 23); Panel.add(but9); but9.setBounds(225, 149, 89, 23); Panel.add(but0); but0.setBounds(126, 183, 89, 23); Panel.add(butadd); butadd.setBounds(324, 81, 89, 23); Panel.add(butsub); butsub.setBounds(324, 115, 89, 23); Panel.add(butmulti); butmulti.setBounds(324, 183, 89, 23); Panel.add(butdiv); butdiv.setBounds(324, 149, 89, 23); Panel.add(buteq); buteq.setBounds(225, 183, 89, 23); Panel.add(butclear); butclear.setBounds(10, 183, 89, 23); but1.addActionListener(this); but2.addActionListener(this); but3.addActionListener(this); but4.addActionListener(this); but5.addActionListener(this); but6.addActionListener(this); but7.addActionListener(this); but8.addActionListener(this); but9.addActionListener(this); but0.addActionListener(this); butadd.addActionListener(this); butsub.addActionListener(this); butmulti.addActionListener(this); butdiv.addActionListener(this); buteq.addActionListener(this); butclear.addActionListener(this); } private LayoutManager FlowLayout() { // TODO Auto-generated method stub return null; } @Override public void actionPerformed(ActionEvent e){ Object source = e.getSource(); if(source==butclear){ number1=0.0; number2=0.0; text.setText(null); } if(source==but1){ text.append("1"); } if(source==but2){ text.append("2"); } if(source==but3){ text.append("3"); } if(source==but4){ text.append("4"); } if(source==but5){ text.append("5"); } if(source==but6){ text.append("6"); } if(source==but7){ text.append("7"); } if(source==but8){ text.append("8"); } if(source==but9){ text.append("9"); } if(source==but0){ text.append("0"); } if(source==butadd){ number1=number_reader(); text.setText(""); addc=1; subc=0; multic=0; divc=0; } if(source==butsub){ number1=number_reader(); text.setText(""); addc=0; subc=1; multic=0; divc=0; } if(source==butmulti){ number1=number_reader(); text.setText(""); addc=0; subc=0; multic=1; divc=0; } if(source==butdiv){ number1=number_reader(); text.setText(""); addc=0; subc=0; multic=0; divc=1; } if(source==buteq){ number2=number_reader(); if(addc>0){ result=number1+number2; text.setText(Double.toString(result)); } if(subc>0){ result=number1-number2; text.setText(Double.toString(result)); } if(multic>0){ result=number1*number2; text.setText(Double.toString(result)); } } if(divc>0){ result=number1/number2; text.setText(Double.toString(result)); } } public double number_reader(){ Double num1; String s; s=text.getText(); num1=Double.valueOf(s); return num1; } }