简体   繁体   中英

How do I get the input from a textfield in a netbeans RCP app?

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}

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 .

try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM