Guys I have an homework activity creating a simple calculator and I encountered some errors in my code can someone help me out of this activity or revised my code. My code have not finish yet and still thinking how my calculator works or what code to be in functionality. Here are my codes so far. thanks alot:(

import java.awt.*;



class Calculator extends Frame{

	

	public static void main(String [] args){

		

		Frame f = new Frame("Calculator");

		f.setSize(250,250);

		f.setVisible(true);

		

		Panel panei1 = new Panel();

		Panel panel2 = new Panel();

	

		panel2.setLayout(new GridLayout(4,4,55));	

		

		panel1.add(new Textfield(20));	

	    panel1.add(new Button("Clear"));

	    panel2.add(new Button("7"));

	    panel2.add(new Button("8"));

	    panel2.add(new Button("9"));

	    panel2.add(new Button("/"));	    

	    panel2.add(new Button("4"));

	    panel2.add(new Button("5"));

	    panel2.add(new Button("6"));

	    panel2.add(new Button("*"));

	    panel2.add(new Button("1"));

	    panel2.add(new Button("2"));

	    panel2.add(new Button("3"));

	    panel2.add(new Button("-"));	    

	    panel2.add(new Button("0"));

	    panel2.add(new Button("."));

	    panel2.add(new Button("="));

	    panel2.add(new Button("+"));

	    

	    f.add(panel1,BorderLayout.NORTH);

	    f.add(panel2,BorderLayout.CENTER);

	    f.pack();		

	}

}

your previous code was better by far, why did you not just keep that and implement the logic into it?

first I tried to use NetBeans but since i don't know what the code to be used, i switch to JCreator,can you help me out to work my codes better?

1. create a class that extends JFrame. use this class for the GUI part
2. create a separate class which 'll contain your main method. do not create your gui within your main method, that'll make it just a bit complex afterwards, unless you'll never want to work on it.

the NetBeans IDE is a handy tool if you want to create a Swing GUI rapidly, but if you actually want to know how it works, control how it works and keep control over the code you'll end up with (not to mention, keep it readable), writing the code yourself is a far better approach indeed.

if you want how to do this, I would recommend you to read on on the use of the Swing classes. this tutorial is provided by Oracle to help you on your way. Don't try to do it all at once if you're not familliar with the components yet.

thanks for helping me

I'm just a newbie in Java language I bit confuse of what of said

1. create a class that extends JFrame. use this class for the GUI part
2. create a separate class which 'll contain your main method. do not create your gui within your main method, that'll make it just a bit complex afterwards, unless you'll never want to work on it.

and .. what part confuses you?

just separate the logic from the UI and from the driver class,
that 'll make it a lot easier to maintain and, if need to, alter your code

Guys need a hand again to help me, as of now here are some update of my code in my calculator. My problem now is how to input more one number e.g when i try to press number 7, number 7 printed in the textfield but when i try to press a new number like 8, number seven will replace by number 8 in the textfield. And also what code to be in the remaining buttons divide,multiply,minus,add,decimal and equals.:(:(

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class gui implements ActionListener{

	

	TextField t;

	Panel p1;

	Panel p2;

	Button clear;

	Button seven;

	Button eight;

	Button nine;

	Button divide;

	Button four;

	Button five;

	Button six;

	Button multiply;

	Button one;

	Button two;

	Button three;

	Button minus;

	Button zero;

	Button decimal;

	Button equals;

	Button add;

	

	void launch(){

		Frame f = new Frame("Calculator");

		p1 = new Panel();

		p2 = new Panel();

		p2.setLayout(new GridLayout(4,4,5,5));

		t = new TextField(15);

		clear = new Button("Clear");

		seven = new Button("7");

	    eight = new Button("8");

	    nine = new Button("9");

	    divide = new Button("/");	

	    four = new Button("4");	

	    five = new Button("5");		

	    six = new Button("6");

	    multiply = new Button("*");

	    one = new Button("1");

	    two = new Button("2");

	    three = new Button("3");

	    minus = new Button("-");

	    zero = new Button("0");

	    decimal = new Button(".");			

	    equals = new Button("=");

	    add = new Button("+");	    

	    clear.addActionListener(this);

		seven.addActionListener(this);

	    eight.addActionListener(this);

	    nine.addActionListener(this);

	    divide.addActionListener(this);	

	    four.addActionListener(this);	

	    five.addActionListener(this);		

	    six.addActionListener(this);

	    multiply.addActionListener(this);

	    one.addActionListener(this);

	    two.addActionListener(this);

	    three.addActionListener(this);

	    minus.addActionListener(this);

	    zero.addActionListener(this);

	    decimal.addActionListener(this);			

	    equals.addActionListener(this);

	    add.addActionListener(this);	    

	    p1.add(t);

	    p1.add(clear);

	    p2.add(seven);

	    p2.add(eight);	    

	    p2.add(nine);

	    p2.add(divide);	    

	    p2.add(four);

	    p2.add(five);

	    p2.add(six);

	    p2.add(multiply);	   

	    p2.add(one);

	    p2.add(two);

	    p2.add(three);

	    p2.add(minus);	   

	    p2.add(zero);

	    p2.add(decimal);

	    p2.add(equals);	

	    p2.add(add);

	    f.add(p1, BorderLayout.NORTH);

	    f.add(p2, BorderLayout.CENTER);

	    f.setSize(250,250);

	    f.setVisible(true);

	    f.pack();	

	}

	public static void main(String [] args){

		gui g = new gui();

		g.launch();

	}	

	public void actionPerformed(ActionEvent john){

		if(john.getSource()==clear){

			t.setText("");

		}

		if(john.getSource()==seven){

			t.setText("7");

		}

		if(john.getSource()==eight){

			t.setText("8");

		}

		if(john.getSource()==nine){

			t.setText("9");

		}

		if(john.getSource()==four){

			t.setText("4");

		}

		if(john.getSource()==five){

			t.setText("5");

		}	

	        if(john.getSource()==six){

			t.setText("6");

		}

		if(john.getSource()==one){

			t.setText("1");

		}

		if(john.getSource()==two){

			t.setText("2");

		}

		if(john.getSource()==three){

			t.setText("3");

		}				

	}

}

replace (for instance)

t.setText("8");

by

t.setText(t.getText()+"8");

by just setting t.setText("8"); you are overwriting the existing value of t, while you want to append to it.

thanks sir I'll try it :)

Can I ask something, When i add new button "On" & "Off" it is possible that when I press button "Off" all buttons of my calculator will disable and when I press button "On" it will enable again.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;



class NewKalc implements ActionListener{

	

	double x, y;

	String a, b, c, d, e;

	TextField t;

	Panel p1;

	Panel p2;

	Button clear;

	Button seven;

	Button eight;

	Button nine;

	Button divide;

	Button four;

	Button five;

	Button six;

	Button multiply;

	Button one;

	Button two;

	Button three;

	Button minus;

	Button zero;

	Button decimal;

	Button equals;

	Button add;

	

	void launch(){

		Frame f = new Frame("Calculator");

		p1 = new Panel();

		p2 = new Panel();

		p2.setLayout(new GridLayout(4,4,5,5));

		t = new TextField(20);

		clear = new Button("Clear");

		seven = new Button("7");

	    eight = new Button("8");

	    nine = new Button("9");

	    divide = new Button("/");	

	    four = new Button("4");	

	    five = new Button("5");		

	    six = new Button("6");

	    multiply = new Button("*");

	    one = new Button("1");

	    two = new Button("2");

	    three = new Button("3");

	    minus = new Button("-");

	    zero = new Button("0");

	    decimal = new Button(".");			

	    equals = new Button("=");

	    add = new Button("+");	    

	    clear.addActionListener(this);

		seven.addActionListener(this);

	    eight.addActionListener(this);

	    nine.addActionListener(this);

	    divide.addActionListener(this);	

	    four.addActionListener(this);	

	    five.addActionListener(this);		

	    six.addActionListener(this);

	    multiply.addActionListener(this);

	    one.addActionListener(this);

	    two.addActionListener(this);

	    three.addActionListener(this);

	    minus.addActionListener(this);

	    zero.addActionListener(this);

	    decimal.addActionListener(this);			

	    equals.addActionListener(this);

	    add.addActionListener(this);	    

	    p1.add(t);

	    p1.add(clear);

	    p2.add(seven);

	    p2.add(eight);	    

	    p2.add(nine);

	    p2.add(divide);	    

	    p2.add(four);

	    p2.add(five);

	    p2.add(six);

	    p2.add(multiply);	   

	    p2.add(one);

	    p2.add(two);

	    p2.add(three);

	    p2.add(minus);	   

	    p2.add(zero);

	    p2.add(decimal);

	    p2.add(equals);	

	    p2.add(add);

	    f.add(p1, BorderLayout.NORTH);

	    f.add(p2, BorderLayout.CENTER);

	    f.setSize(275,250);

	    f.setVisible(true);

	    f.pack();	

	}

	public static void main(String [] args){

		DecimalFormat decimal =  new DecimalFormat("#.##");

		NewKalc c = new NewKalc();

		c.launch();

	}	

	public void actionPerformed(ActionEvent john){

		if(john.getSource()==clear){

			t.setText("");		

		}

		if(john.getSource()==decimal){

			c=t.getText();

			d=".";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==zero){

			c=t.getText();

			d="0";

			e=c+d;

			t.setText(e);  					

		}		

		if(john.getSource()==one){

			c=t.getText();

			d="1";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==two){

			c=t.getText();

			d="2";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==three){

			c=t.getText();

			d="3";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==four){

			c=t.getText();

			d="4";

			e=c+d;

			t.setText(e);

		}	

	    if(john.getSource()==five){

			c=t.getText();

			d="5";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==six){

			c=t.getText();

			d="6";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==seven){

			c=t.getText();

			d="7";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==eight){

			c=t.getText();

			d="8";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==nine){

			c=t.getText();

			d="9";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==add){

			a=t.getText();

			t.setText("");

			x=1;

		}

		if(john.getSource()==minus){

			a=t.getText();

			t.setText("");

			x=2;

		}

		if(john.getSource()==multiply){

			a=t.getText();

			t.setText("");

			x=3;

		}

		if(john.getSource()==divide){

			a=t.getText();

			t.setText("");

			x=4;

		}

		if(john.getSource()==equals){

			b=t.getText();

			if(x==1){

				y=Double.parseDouble(a)+Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==2){

				y=Double.parseDouble(a)-Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==3){

			    y=Double.parseDouble(a)*Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==4){

				y=Double.parseDouble(a)/Double.parseDouble(b);

				t.setText(String.valueOf(y));

			} 		

		}											

	}

}

sure ... there are several ways:
the easiest (and best) is to use the setEnabled(boolean b) method of your Button Objects.
an other way would be: keep a local boolean variable that is default true, when you click on 'off' is set to false, when you click on 'on' is set back to true.
when pressing a button, you can write your code so, that it is only run when that variable is true. But, as I already said, the first way I mentioned is much better. I would suggest using that.

A big advantage of setEnabled(false) is that the UI will automatically update the appearance of the buttons so it's clear to the user that they are disabled.

thanks sir I really appreciate your kindness.:)

Guys, I cant figure out how to code the functions of on & off button and how to disable button. Can you help me the code:(:(

Have a button with text "Off". Add the listener just like all your other buttons. In the actionPerformed, test if text is "Off" - if so, disable all the other buttons and change the text to "On", else enable all the other buttons and change the txt to "Off".

can you make the code sir? all I want is, when I want to press the "OFF" button all buttons will disable except the button "ON". And when I want to press the "ON" button, all buttons are Enable again. My head is aching in this ON & OFF button functions...:(:(

No. I will not, and neither will anyone else.
This isn't a "we do your homework" service. There are lots of people here who will freely give their time to help you become the best Java programmer you can be, but there's nobody here who is interested in doing your homework for you.

okey sir, so what is the first step i must to do so that i can mage the functions of my buttons as I want to be.

I already posted step-by-step instructions for you. Try following them. If you get stuck post what you have coded so far and explain what's stopping you.

Here is my code so far, Sir, I wonder why I cant click the "OFF" button it's already disabled.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.*;



class NewKalc implements ActionListener{

	



	double x, y;

	boolean ans = true;

	String a, b, c, d, e, f;

	TextField t;

	Panel p1;

	Panel p2;

	Panel p3;

	Button clear;

	Button seven;

	Button eight;

	Button nine;

	Button divide;

	Button four;

	Button five;

	Button six;

	Button multiply;

	Button one;

	Button two;

	Button three;

	Button minus;

	Button zero;

	Button decimal;

	Button equals;

	Button add;

	Button on;

	Button off;

	

	void launch(){

		Frame f = new Frame("Calculator");

		p1 = new Panel();

		p2 = new Panel();

		p3 = new Panel();

		p2.setLayout(new GridLayout(4,4,2,2));

		p3.setLayout(new GridLayout(1,2,2,2));

		t = new TextField(20);

		clear = new Button("Clear");

		seven = new Button("7");

	    eight = new Button("8");

	    nine = new Button("9");

	    divide = new Button("/");	

	    four = new Button("4");	

	    five = new Button("5");		

	    six = new Button("6");

	    multiply = new Button("*");

	    one = new Button("1");

	    two = new Button("2");

	    three = new Button("3");

	    minus = new Button("-");

	    zero = new Button("0");

	    decimal = new Button(".");			

	    equals = new Button("=");

	    add = new Button("+");

	    on = new Button("On");

	    off = new Button("Off");    

	    clear.addActionListener(this);

		seven.addActionListener(this);

	    eight.addActionListener(this);

	    nine.addActionListener(this);

	    divide.addActionListener(this);	

	    four.addActionListener(this);	

	    five.addActionListener(this);		

	    six.addActionListener(this);

	    multiply.addActionListener(this);

	    one.addActionListener(this);

	    two.addActionListener(this);

	    three.addActionListener(this);

	    minus.addActionListener(this);

	    zero.addActionListener(this);

	    decimal.addActionListener(this);			

	    equals.addActionListener(this);

	    add.addActionListener(this);

	    on.addActionListener(this);	    

	    off.addActionListener(this);

	    p1.add(t);

	    p1.add(clear);

	    p2.add(seven);

	    p2.add(eight);	    

	    p2.add(nine);

	    p2.add(divide);	    

	    p2.add(four);

	    p2.add(five);

	    p2.add(six);

	    p2.add(multiply);	   

	    p2.add(one);

	    p2.add(two);

	    p2.add(three);

	    p2.add(minus);	   

	    p2.add(zero);

	    p2.add(decimal);

	    p2.add(equals);	

	    p2.add(add);

	    p3.add(on);

	    p3.add(off);

	    f.add(p1, BorderLayout.NORTH);

	    f.add(p2, BorderLayout.CENTER);

	    f.add(p3, BorderLayout.SOUTH);

	    f.setSize(275,250);

	    f.setVisible(true);

	    f.pack();

	    on.setEnabled(true);

	    off.setEnabled(false);	

	}

	public static void main(String [] args){

		DecimalFormat decimal =  new DecimalFormat("#.##");

		NewKalc c = new NewKalc();

		c.launch();

	}	

	public void actionPerformed(ActionEvent john){

		if(john.getSource()==clear){

			t.setText("");		

		}

		if(john.getSource()==decimal){

			c=t.getText();

			d=".";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==zero){

			c=t.getText();

			d="0";

			e=c+d;

			t.setText(e);  					

		}		

		if(john.getSource()==one){

			c=t.getText();

			d="1";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==two){

			c=t.getText();

			d="2";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==three){

			c=t.getText();

			d="3";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==four){

			c=t.getText();

			d="4";

			e=c+d;

			t.setText(e);

		}	

	    if(john.getSource()==five){

			c=t.getText();

			d="5";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==six){

			c=t.getText();

			d="6";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==seven){

			c=t.getText();

			d="7";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==eight){

			c=t.getText();

			d="8";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==nine){

			c=t.getText();

			d="9";

			e=c+d;

			t.setText(e);

		}

		if(john.getSource()==add){

			a=t.getText();

			t.setText("");

			x=1;

		}

		if(john.getSource()==minus){

			a=t.getText();

			t.setText("");

			x=2;

		}

		if(john.getSource()==multiply){

			a=t.getText();

			t.setText("");

			x=3;

		}

		if(john.getSource()==divide){

			a=t.getText();

			t.setText("");

			x=4;

		}

		if(john.getSource()==equals){

			b=t.getText();

			if(x==1){

				y=Double.parseDouble(a)+Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==2){

				y=Double.parseDouble(a)-Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==3){

			    y=Double.parseDouble(a)*Double.parseDouble(b);

				t.setText(String.valueOf(y));

			}

			else

			if(x==4){

				y=Double.parseDouble(a)/Double.parseDouble(b);

				t.setText(String.valueOf(y));

			} 		

		}

		if(john.getSource()==on){

			ans=true;

		}

		else

			if(john.getSource()==off){

				ans=false;

			}										

	}

}

Are you sure its disabled? Is its text grey?
All you do in your code is set ans=true; so there's nothing visible to happen when you click it.

yes sir im really sure, I cant click the OFF button....

On line 217 you disable it, so it's disabled.
Maybe when the use clicks "on" you could disable "on" and enable "off"?

No, thats not just what happened, when I click or press the button "ON" nothing happen.

On button: All you do in your code is set ans=true; so there's nothing visible to happen when you click it.

Off button: On line 217 you disable it, so it's disabled.

When the use clicks "on" you could disable "on" and enable "off" (and vice-versa for "off")

I already change the line 217 true and the button "Off" are enabled but no functions when i click it.

There's no function because you haven't coded any function. You just set the variable "ans" but you never use it.

thanks for helping me, by the way how to solve the thread?

You'll see "mark this thread solved" just above the box where you can type a new message. Just click it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.