[英]Netbeans GUI passing textfield values to action defined in App not in View
I created a simple app in Netbeans, it contains a few textfields for user input and a button, I've associated an action with the button through the Netbeans interface but I decided to define the action in the App and not the View so as to follow some notion of MVC. 我在Netbeans中创建了一个简单的应用程序,它包含一些用于用户输入的文本字段和一个按钮,我已经通过Netbeans界面将一个操作与该按钮相关联,但是我决定在该应用程序中而不是在View中定义该操作,以便遵循MVC的一些概念。
The action works fine, I can print out the console every time the button is clicked. 动作正常,每次单击按钮时我都可以打印出控制台。
But in order to do what I want, I need the values included in the jTextFields! 但是为了做我想做的事,我需要jTextFields中包含的值!
How to do this? 这个怎么做? This is the code in TestApp.java:
这是TestApp.java中的代码:
@Action
public void ClickedOnButton() {
System.out.println("Clicked ok");
System.out.println("Will now attempt to read notes.ini");
ReadNotesFile();
}
And this is the code in TestView.java: 这是TestView.java中的代码:
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(tpa_fixer.TPA_FixerApp.class).getContext().getActionMap(TPA_FixerView.class, this);
jButton1.setAction(actionMap.get("ClickedOnButton")); // NOI18N
What have you tried, and how doesn't it work? 您尝试了什么,它怎么不起作用? The standard way to get a JTextField to display text is to call
setText()
on it. 使JTextField显示文本的标准方法是在其上调用
setText()
。 Have you tried doing this? 您是否尝试过这样做?
Also, 也,
Edit 编辑
You state: 您声明:
I don't want to set the text in the jTextFields, I want to get the values out of them and use it in the method that gets run when I click on the button.
我不想在jTextFields中设置文本,我想从它们中获取值,并在单击按钮时运行的方法中使用它。 I can't see how to do this unless I can pass arguments somehow within the body of the action definition in the View class.
除非可以在View类中的动作定义主体内以某种方式传递参数,否则我看不到如何执行此操作。
What I've done in this situation, where I need to extract information out of gui fields for manipulation in other classes: 在这种情况下,我需要从gui字段中提取信息以便在其他类中进行操作,这是我要做的事情:
nameField.getText();
nameField.getText();
. getText
method to obtain the correct JTextfield, get its text and return it. getText
方法获取正确的JTextfield,获取其文本并返回它。 I often use the same Strings used as JLabels associated with the JTextField as my key Strings.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.