2014-03-12 60 views
-2

从JTextField中获取值&将其重写到现有变量中。从JTextField获取值并将其重写到现有变量中

例如:

public class variables{ 

    String var = "hi"; 

    public static void main(String[]args){ 

    //do sth  

    } 

} 

然后接口读取来自主类的值&的值从一个JTextField改变

private void changeVariableActionPerformed(java.awt.event.ActionEvent evt) {            


    variables.var = txtField.getText(); 

    //then into textfield we write bye & variables.var takes the value of bye 

    //variables.var = "bye" 

} 

我想改变变种的与动作的值来执行

+0

添加有关更新的通知的一些信息。你提到获得的价值,但是什么行动导致提示 –

回答

2

ActionLister添加到JTextField,并且每次更改值时都将它赋值给您的变量。

你可以做这样的事情:

txtField.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     variables.var = txtField.getText(); 

    } 
}); 
+0

它不像行动事件那样做? 我的意思是手动? – man

+0

对不起,我没有明白你的意思? – Salah

+0

在netbeas中,只需右键单击到jtext字段或组合等,并选择了动作表演或任何你想要的动作。 我的问题是,如果有办法做到这一点。 – man

相关问题