views:

201

answers:

2

I am making a text based game in Java. I have a text field, enter button, and a label.

What code do i use to scan the text field once the button is clicked and respond?

So that if I type in (launch missile) the label should say (missile launched).

I will be listening to a button actionperformed or maybe a mouseclick event . Something like this

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (Text field says: launch missile)
   {print on label:Missile launched}

or

if (text field says: invade)
   {Print on label: Invasion started}
+3  A: 

you can read the text field using

 textField1.getText()

to compare, use

 if (textField1.getText().equals("launch missle"))
 {
     //do something
 }

similarly, to set the label's text, use

label1.setText("Missle launched");

I suggest reading more about Java flow control.

Aziz
Thank You So much. That helped a lot
+1  A: 

try

JButton launch=new JButton(new AbstractAction("Launch")
    {
    @Override
    public void actionPerformed(ActionEvent e)
         {
         yourLabel.setText("Missile Launched");
         }
    });
Pierre