Hi i was wondering if anyone can help me out. Im trying to make a applet when a button is clicked it will appear the name John . And when another button is pressed it will display "Smith" in a seperate text field. This is what I have so far.

import java.applet.*;
import java.awt.*;

public class Test extends Applet
{
Button first ;
Button last;
TextField one ;
TextField two ;


public void init ()
{
first = new Button;
last = new Button;
one = new TextField;
two = new TextField;
add (first);
add(last);
add(one);
add(two);
}

public boolean action (Event e , Object o)
{
if (e.target == first)
{
one.setText("john");
}
else if (e.target == last)
{
two.setText("smith");
}
}

}

Couple of problems:

1) You don't state what the problem is
2) You don't set anything visible
3) Your global variables are public
4) You don't set the layout

Member Avatar for iamthwee

Couple of problems:

1) You don't state what the problem is
2) You don't set anything visible
3) Your global variables are public
4) You don't set the layout

Not much to correct then... :lol:

Greetings:
You should really pay attention at server_crash points, anyway, this should do the trick:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test extends Applet implements ActionListener{
	private Button b1,b2;
	private Label l1,l2;
	public void init()
	{
		GridBagLayout g=new GridBagLayout();setLayout(g);
		GridBagConstraints c=new GridBagConstraints();
		c.fill=GridBagConstraints.HORIZONTAL;
		l1=new Label();l2=new Label();
		b1=new Button("Jhon");b1.addActionListener(this);
		b2=new Button("Smith");b2.addActionListener(this);
		c.gridx=0;c.gridy=0;add(l1,c);c.gridx=1;c.gridy=0;add(b1,c);
		c.gridx=0;c.gridy=1;add(l2,c);c.gridx=1;c.gridy=1;add(b2,c);
	}
	public void actionPerformed(ActionEvent e){
		String s=e.getActionCommand();
		if(s=="Jhon")l1.setText("Jhon");else l2.setText("Smith");
	}
}

Thanks man , helped a bunched.

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.