consider a instance


jtextfield t=new jtextfield();
jtextfield t1=new jtextfield();
jtextfield t2=new jtextfield();
i want to get the values of textfield t and t1 and display in t2
like i want to add values of t and t1 and store in t2

Recommended Answers

All 10 Replies

Have a look at the getText() and setText(String text) methods that JTextField inherits from JTextComponent.
ps Watch out for your capitalisation - java is case-sensitive

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class calc extends JFrame 
{
	JTextField t1=new JTextField();
	JTextField t2=new JTextField();
	JTextField t3=new JTextField();
JButton	b1=new JButton("+");
  JButton b2=new JButton("-");
  JButton b3=new JButton("*");
  JButton b4=new JButton("/");
  JPanel panel=new JPanel(new GridLayout(2,2));

	calc()
	{
		
		panel.add(t1);
		panel.add(t2);
		panel.add(t3);
		panel.add(b1);
		panel.add(b2);
		panel.add(b3);
		panel.add(b4);
		add(panel,BorderLayout.CENTER);
		
	
		b1.addActionListener(new ActionListener(){
			
			public void actionperformed(ActionEvent e)
			{
			 t3.settext=t1.gettext()+t2.getText();
			 
			}
		}
			
			b1.addActionListener(new ActionListener(){
				public void actionperformed(action event e)
				{
					t3.settext=t1.gettext()-t2.getText();
				}
			}
		
		
		
		
	}
	public static void main(string args[])
	{
		calc frame=new calc();
		frame.setVisible(true);
		frame.setSize(300,100)
	}
	
}

this is my problem i want to do a simple calculator please solve me

good one, and what's with that

Have another look at setText... your syntax is wrong in
t3.settext=t1.gettext()-t2.getText();

We will help you with your problem, but it's your homework, we won't do it for you. You have made a start, so keep going! If you get stuck post your latest code and the details of why you're stuck, and someone will help.

sir i didnt tel you to solve my problem sir my doubt is in

t3.settext=t1.gettext()-t2.getText();

i dont know what to do over there
if i do t1.gettext() how will it get value sir ????? i just need to get the value of two text fields and display in t3

t1.getText() will give you the current contents of the field as a String.
Next you have to convert that to a numeric value (eg int or float).
Then you can add/subtract etc the values
Then convert the result back to a String
Finally set the result field's text to that String.

To convert between Strings and numbers there are useful methods in the appropriate numeric classes, eg
Integer.parseInt(aString); // converts String to Integer
Integer.toString(anInt): // converts int to String
You should look these up yourself to learn about them properly.

so can i use integer.parse method will it convert string to integer??

Yes. Have a look at its documentation for details (eg what happens if the String cannot be converted to integer)

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.