Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class aa extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- float numA = 0;
- static JTextArea ans = new JTextArea(1,15);
- static JMenuBar menuB = new JMenuBar();
- static JMenu menu = new JMenu("File");
- JMenuItem graph = new JMenuItem("Graph");
- JButton clr = new JButton("clr");
- JButton sqr = new JButton("^2");
- JButton pow = new JButton("^x");
- JButton pnB = new JButton("+/-");
- JButton deci = new JButton(".");
- JButton addB = new JButton("+");
- JButton subB = new JButton("-");
- JButton mulB = new JButton("*");
- JButton divB = new JButton("%");
- JButton equB = new JButton("=");
- JButton key1 = new JButton("1");
- JButton key2 = new JButton("2");
- JButton key3 = new JButton("3");
- JButton key4 = new JButton("4");
- JButton key5 = new JButton("5");
- JButton key6 = new JButton("6");
- JButton key7 = new JButton("7");
- JButton key8 = new JButton("8");
- JButton key9 = new JButton("9");
- JButton key0 = new JButton("0");
- float x = 0;
- float y = 0;
- String operator;
- String lastP;
- public static void main(String[] args) {
- aa app =new aa();
- app.setTitle("Calculator");
- }
- public aa(){
- this.setJMenuBar(menuB);
- menuB.add(menu);
- menu.add(graph);
- graph.addActionListener(this);
- this.setVisible(true);
- this.setLocationRelativeTo(null);
- this.setSize(230, 240);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //JPanel content = new JPanel();
- this.setLayout(new FlowLayout());
- this.add(ans);
- ans.setEditable(false);
- this.add(clr);
- clr.addActionListener(this);
- this.add(sqr);
- sqr.addActionListener(this);
- this.add(pow);
- pow.addActionListener(this);
- this.add(pnB);
- pnB.addActionListener(this);
- this.add(key1);
- key1.addActionListener(this);
- this.add(key2);
- key2.addActionListener(this);
- this.add(key3);
- key3.addActionListener(this);
- this.add(addB);
- addB.addActionListener(this);
- this.add(key4);
- key4.addActionListener(this);
- this.add(key5);
- key5.addActionListener(this);
- this.add(key6);
- key6.addActionListener(this);
- this.add(subB);
- subB.addActionListener(this);
- this.add(key7);
- key7.addActionListener(this);
- this.add(key8);
- key8.addActionListener(this);
- this.add(key9);
- key9.addActionListener(this);
- this.add(mulB);
- mulB.addActionListener(this);
- this.add(key0);
- key0.addActionListener(this);
- this.add(deci);
- deci.addActionListener(this);
- this.add(equB);
- equB.addActionListener(this);
- this.add(divB);
- divB.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getSource() == this.key1){
- if(lastP == "P"){
- ans.setText(" ");
- operator="";
- }
- ans.append("1");
- lastP = "1";
- }
- if(e.getSource() == this.key2){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("2");
- lastP = "2";
- }
- if(e.getSource() == this.key3){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("3");
- lastP = "3";
- }
- if(e.getSource() == this.key4){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("4");
- lastP = "4";
- }
- if(e.getSource() == this.key5){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("5");
- lastP = "5";
- }
- if(e.getSource() == this.key6){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("6");
- lastP = "6";
- }
- if(e.getSource() == this.key7){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("7");
- lastP = "7";
- }
- if(e.getSource() == this.key8){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("8");
- lastP = "8";
- }
- if(e.getSource() == this.key9){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("9");
- lastP = "9";
- }
- if(e.getSource() == this.key0){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append("0");
- lastP = "0";
- }
- if(e.getSource() == this.addB){
- x = Float.parseFloat(ans.getText());
- ans.setText("");
- operator = "+";
- lastP = "+";
- }
- if(e.getSource() == this.subB){
- x = Float.parseFloat(ans.getText());
- ans.setText("");
- operator = "-";
- lastP = "-";
- }
- if(e.getSource() == this.mulB){
- x = Float.parseFloat(ans.getText());
- ans.setText("");
- operator = "*";
- lastP = "*";
- }
- if(e.getSource() == this.divB){
- x = Float.parseFloat(ans.getText());
- ans.setText("");
- operator = "/";
- lastP = "/";
- }
- if(e.getSource() == this.pow){
- x = Float.parseFloat(ans.getText());
- ans.setText("");
- operator = "^";
- lastP = "^";
- }
- if(e.getSource() == this.equB){
- lastP = "P";
- if(operator == "+"){
- y = Float.parseFloat(ans.getText());
- numA = x+y;
- ans.setText(numA+"");
- }
- if(operator == "-"){
- y =Float.parseFloat(ans.getText());
- numA = x-y;
- ans.setText(numA+"");
- }
- if(operator == "*"){
- y =Float.parseFloat(ans.getText());
- numA = x*y;
- ans.setText(numA+"");
- }
- if(operator == "/"){
- y = Float.parseFloat(ans.getText());
- numA = x/y;
- ans.setText(numA+"");
- }
- if(operator == "^"){
- y = Float.parseFloat(ans.getText());
- numA = (float) Math.pow(x, y);
- ans.setText(numA+"");
- }
- }
- if(e.getSource() == this.clr){
- ans.setText("");
- operator="";
- lastP = "c";
- }
- if(e.getSource() == this.sqr){
- x = Float.parseFloat(ans.getText());
- numA = x*x;
- ans.setText(numA+"");
- lastP = "^2";
- }
- if(e.getSource() == this.deci){
- if(lastP == "P"){
- ans.setText("");
- operator="";
- }
- ans.append(".");
- lastP = "d";
- }
- if(e.getSource() == this.pnB){
- x = Float.parseFloat(ans.getText());
- numA = x * -1;
- ans.setText(numA+"");
- }
- if(e.getSource() == this.graph){
- //JOptionPane.showMessageDialog(null, "It worked");
- ab grapher = new ab();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement