3 Replies - 11975 Views - Last Post: 15 December 2008 - 10:27 AM Rate Topic: -----

#1 nagarajpinnu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-June 07

java program with buttons and text box

Posted 15 December 2008 - 08:28 AM

/* program to create an applet which contains 3 buttons having labels one two and three and on text flield when user clicks any button its label text appears in the text field*/
// but not working help me :v:







import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="buttons3" width=200 height=300>
</applet> */

public class buttons3 extends Applet implements ActionListener
{
String msg= " ";
TextField tf;
public void init()

{
setBackground(Color.pink);

Button b1 = new Button("One");
add(b1);
Button b2 = new Button("Two");
add(b2);
Button b3 = new Button("Three");
add(b3);

TextField tf = new TextField(20);

add(tf);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
//tf.setEchoChar('m');
}

public void actionPerformed(ActionEvent ae)
{

//String msg= " ";
String str = ae.getActionCommand();
if(str.equals("One"))
{

msg="One";

}



// tf.setChar(msg);

else if(str.equals("Two"))
{
msg ="Two";
tf.setText("msg");
}
else {
msg="Three";
}


repaint();
}
public void paint(Graphics g){
g.drawString(msg, 6, 100);
}


}

Quote



Is This A Good Question/Topic? 0
  • +

Replies To: java program with buttons and text box

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: java program with buttons and text box

Posted 15 December 2008 - 08:41 AM

What errors are you receiving?
--> Compile errors?
--> Runtime errors?
--> Logic erros?
--> etc.

Also: :code:
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: java program with buttons and text box

Posted 15 December 2008 - 08:50 AM

actually you will have to make an action listener for every button then under buttons' action listener for example that of b1

String b1_text = b1.getText();//this code will get the text that is being displayed in the button
		  tf.setText(b1_text);//prints the text of the button 1 into the text feild 



good luck ;), hope that helps....
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: java program with buttons and text box

Posted 15 December 2008 - 10:27 AM

Quote

actually you will have to make an action listener for every button then under buttons' action listener for example that of b1


You don't have to make an actionListener for each button, you could do it Like you have it, except instead of assigning the value to msg, you would set the text of the appropriate textfield, like you do for "Two" but not "One" or "Three"

Also in your messages you set the text like:

Quote

String str = ae.getActionCommand();
if(str.equals("One"))
{

msg="One";

}

When in fact the message is the same as the button text and you could just set the text field to this button text
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1