简体   繁体   English

Netbeans GUI将文本字段值传递给App中未在View中定义的操作

[英]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, 也,

  • Have you gone through the Swing tutorial about these concepts including using text components, JButtons, and ActionListeners? 您是否已阅读有关这些概念的Swing教程,包括使用文本组件,JButton和ActionListeners?
  • Are you seeing any errors in these attempts? 您在这些尝试中是否看到任何错误? If so, post them here. 如果是这样,请在此处发布。
  • Is your "control" class, the one with the listener code, separate from your "view" or GUI class? 您的“控件”类(带有侦听器代码的类)是否与“视图”或GUI类分开? If so, does control have a valid reference to view? 如果是这样,控件是否具有有效的参考视图?

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字段中提取信息以便在其他类中进行操作,这是我要做的事情:

  • You can give each field an associated public getText() method and then call these methods using the control's reference to the view object. 您可以给每个字段一个关联的公共getText()方法,然后使用控件对视图对象的引用来调用这些方法。 For instance say view has a nameField JTextField, then I'd give it a getNameFieldText() method that returns nameField.getText(); 例如说视图有一个nameField JTextField,然后我给它一个getNameFieldText()方法,该方法返回nameField.getText(); .
  • If you had many such fields, then it may be more efficient to use just one getText method but allow it a parameter to let you choose which field to extract text from. 如果您有许多这样的字段,那么仅使用一个getText方法可能会更有效,但允许它使用一个参数让您选择要从中提取文本的字段。 To make this work efficiently, I've sometimes given my GUI a HashMap and then have control pass in the String key that allows the getText method to obtain the correct JTextfield, get its text and return it. 为了使此工作高效进行,我有时给我的GUI一个HashMap,然后在String键中传递控制权,该键允许getText方法获取正确的JTextfield,获取其文本并返回它。 I often use the same Strings used as JLabels associated with the JTextField as my key Strings. 我经常使用与与JTextField关联的JLabel相同的字符串作为我的关键字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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