I need help in java calculator please i submit my assignment tomorrow 2+2=4 its work 2+2 then press + how to show result last | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need help in java calculator please i submit my assignment tomorrow 2+2=4 its work 2+2 then press + how to show result last

how to show result last two variables on button + and

19th Nov 2017, 9:33 PM
Gohar Ejaz
Gohar Ejaz - avatar
11 Answers
0
ok
19th Nov 2017, 10:08 PM
Gohar Ejaz
Gohar Ejaz - avatar
0
package javaapplication15; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Gui22 extends WindowAdapter implements ActionListener{ Frame f; Button b1,b2,b3,b4; TextField tf; double n1,n2,n3,re,add=0; Panel p; Gui22(){ f=new Frame("Calculator"); f.setLayout(new GridLayout(2,0)); f.setSize(300,300); f.show(); p =new Panel(); p.setLayout(new GridLayout(2,2)); b1=new Button("1"); b2=new Button("2"); b3=new Button("+"); b4=new Button("="); tf=new TextField(" "); f.add(tf); p.add(b1); p.add(b2); p.add(b3); p.add(b4); f.add(p); f.addWindowListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); } public void windowClosing(WindowEvent we){ System.exit(0); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==b1){ tf.setText(tf.getText()+b1.getLabel()); }if(ae.getSource()==b2){ tf.setText(tf.getText()+b2.getLabel()); }if(ae.getSource()==b3){ n1=Double.parseDouble(String.valueOf(tf.getText())); tf.setText(""); add=1; } if(ae.getSource()==b4){ n2=Double.parseDouble(String.valueOf(tf.getText())); if(add>0){ re=n1+n2; tf.setText(" "+re); n1=0; n2=0; } } } public class JavaApplication15 { public static void main(String[] args) { Gui22 gu=new Gui22(); } }
19th Nov 2017, 10:23 PM
Gohar Ejaz
Gohar Ejaz - avatar
0
1+2=3 answer is correct
19th Nov 2017, 10:24 PM
Gohar Ejaz
Gohar Ejaz - avatar
0
but when you use normal window calculator 2+2+ no use equal button then show answer 4
19th Nov 2017, 10:26 PM
Gohar Ejaz
Gohar Ejaz - avatar
0
bro i try
20th Nov 2017, 5:27 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
no bro its not work
20th Nov 2017, 6:50 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
when i press 1 then print on screen (null1)
20th Nov 2017, 6:51 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
yes bro
20th Nov 2017, 6:56 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
bro can you send me your whatsapp no i can talk easily please
20th Nov 2017, 6:57 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
bro i don't know about insert
20th Nov 2017, 7:07 AM
Gohar Ejaz
Gohar Ejaz - avatar
0
bro your facebook i d
20th Nov 2017, 7:07 AM
Gohar Ejaz
Gohar Ejaz - avatar