java 如何从 netbeans RCP 应用程序中的文本字段获取输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1383676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:22:01  来源:igfitidea点击:

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

java

提问by Pierre

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

我正在用 Java 制作一个基于文本的游戏。我有一个文本字段、输入按钮和一个标签。

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 actionperformedor maybe a mouseclick event . Something like this

我会听一个按钮actionperformed或者一个 mouseclick 事件。像这样的东西

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}

回答by Aziz

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 Javaflowcontrol.

我建议阅读更多关于Java控制的信息

回答by Pierre

try

尝试

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