java code for scientific calculator gui

Solutions on MaxInterview for java code for scientific calculator gui by the best coders in the world

showing results for - "java code for scientific calculator gui"
Gianluca
30 Feb 2017
1public class Gui extends JFrame {
2    private static JButton [] button = new JButton[36];
3    private static TextField tf;
4    private JPanel panel;
5    private JPanel panel1;
6
7
8
9public Gui(){
10    super("Scientific Calculator");
11    panel = new JPanel();
12    panel1 = new JPanel(new GridLayout(0,4));
13
14    tf = new TextField(20);
15    tf.setEditable(false);
16    panel.add(tf);
17
18    button[0]=new JButton("Rad/Deg");
19    button[1]=new JButton("x!");
20    button[2]=new JButton("sqrt");
21    button[3]=new JButton("sin");
22    button[4]=new JButton("cos");
23    button[5]=new JButton("tan");
24    button[6]=new JButton("ln");
25    button[7]=new JButton("log");
26    button[8]=new JButton("1/x");
27    button[9]=new JButton("e^x");
28    button[10]=new JButton("x^2");
29    button[11]=new JButton("y^x");
30    button[12]=new JButton("|x|");
31    button[13]=new JButton("pi");
32    button[14]=new JButton("e");
33
34    button[15]=new JButton("C");
35    button[16]=new JButton("(");
36    button[17]=new JButton(")");
37    button[18]=new JButton("%");
38    button[19]=new JButton("/");
39    button[20]=new JButton("7");
40    button[21]=new JButton("8");
41    button[22]=new JButton("9");
42    button[23]=new JButton("*");
43    button[24]=new JButton("4");
44    button[25]=new JButton("5");
45    button[26]=new JButton("6");
46    button[27]=new JButton("-");
47    button[28]=new JButton("1");
48    button[29]=new JButton("2");
49    button[30]=new JButton("3");
50    button[31]=new JButton("+");
51    button[32]=new JButton(".");
52    button[33]=new JButton("0");
53    button[34] =new JButton("+/-");
54    button[35] =new JButton("=");
55
56
57
58    for(int i = 0; i<button.length;i++){
59        panel1.add(button[i]);
60    }
61
62
63    panel.add(panel1);
64    add(panel);
65
66    HandlerClass handler = new HandlerClass();
67    for(int i = 0; i<button.length;i++){
68        button[i].addActionListener(handler);
69    }
70
71}
72private class HandlerClass implements ActionListener{
73
74    @Override
75    public void actionPerformed(ActionEvent e) {
76        double num1 = 0,num2;
77
78        String alreadyDisplayed = tf.getText();
79        String toDisplay = "";
80
81        if(e.getSource()==button[33]){
82            alreadyDisplayed = tf.getText();
83            toDisplay = alreadyDisplayed + (button[33].getText());
84            tf.setText(toDisplay);
85        }
86        if(e.getSource()==button[28]){
87             alreadyDisplayed = tf.getText();
88             toDisplay = alreadyDisplayed + (button[28].getText());
89            tf.setText(toDisplay);
90        }
91        if(e.getSource()==button[29]){
92            alreadyDisplayed = tf.getText();
93            toDisplay = alreadyDisplayed + (button[29].getText());
94            tf.setText(toDisplay);
95        }
96        if(e.getSource()==button[30]){
97            alreadyDisplayed = tf.getText();
98            toDisplay = alreadyDisplayed + (button[30].getText());
99            tf.setText(toDisplay);
100        }
101        if(e.getSource()==button[24]){
102            alreadyDisplayed = tf.getText();
103            toDisplay = alreadyDisplayed + (button[24].getText());
104            tf.setText(toDisplay);
105        }
106        if(e.getSource()==button[25]){
107            alreadyDisplayed = tf.getText();
108            toDisplay = (button[25].getText());
109            tf.setText(toDisplay);
110        }
111        if(e.getSource()==button[26]){
112            alreadyDisplayed = tf.getText();
113            toDisplay = alreadyDisplayed + (button[26].getText());
114            tf.setText(toDisplay);
115        }
116        if(e.getSource()==button[20]){
117            alreadyDisplayed = tf.getText();
118            toDisplay = alreadyDisplayed + (button[20].getText());
119            tf.setText(toDisplay);
120        }
121        if(e.getSource()==button[21]){
122            alreadyDisplayed = tf.getText();
123            toDisplay = alreadyDisplayed + (button[21].getText());
124            tf.setText(toDisplay);
125        }
126        if(e.getSource()==button[22]){
127            alreadyDisplayed = tf.getText();
128            toDisplay = alreadyDisplayed + (button[22].getText());
129            tf.setText(toDisplay);
130        }
131        if(e.getSource()==button[31]){
132            alreadyDisplayed = tf.getText();
133            toDisplay = alreadyDisplayed + (button[31].getText());
134            tf.setText(toDisplay);
135        }
136
similar questions
queries leading to this page
java code for scientific calculator gui