[英]How do I get the input from a textfield in a netbeans RCP app?
I am making a text based game in Java. 我正在用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 . 我会听一个按钮
actionperformed
或者一个鼠标点击事件。 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 . 我建议阅读更多有关Java 流 控制的信息 。
try 尝试
JButton launch=new JButton(new AbstractAction("Launch")
{
@Override
public void actionPerformed(ActionEvent e)
{
yourLabel.setText("Missile Launched");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.