java 从另一个类中的 JPanel 文本字段获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12347616/
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-31 08:30:42  来源:igfitidea点击:

Get value from JPanel textfield in another class

javaswing

提问by shakkir3435

I have JPanelwith text box, save button in another panel. If I click the save button I have to get the 1st panel text box value.

我有JPanel文本框,在另一个面板中保存按钮。如果我单击保存按钮,我必须获得第一个面板文本框的值。

How to access it?

如何访问它?

回答by Azuu

Just make the TextField as Public Static dats it. And then u can Access the TextField Using ClassName.TextFiledName

只需将 TextField 设为 Public Static 即可。然后你可以使用 ClassName.TextFiledName 访问 TextField

回答by Francisco Puga

An easy solution will be create a constructor in the class where you implement your ActionListener and pass in the constructor the components that you need to update or to retrieve values.

一个简单的解决方案是在您实现 ActionListener 的类中创建一个构造函数,并将需要更新或检索值的组件传入构造函数。

This solution will work, but there is a better approach that allows make the code more reusable. Take a look at the observer patternand use it in your code.

此解决方案将起作用,但有一种更好的方法可以使代码更可重用。查看观察者模式并在您的代码中使用它。

回答by nt.bas

Well since there's no SSCCE, this will be a general answer.
First you could make the text box public static field and access it from the other class but that will be one of the worst code you will ever write. Second you can use setters/getters methods which are cool but don't behave well as your program grows complex. Setters/getters will create tight coupling between components. Finally I suggest using the Observer pattern. It may seem like using a nuke against a bicycle but in the end it is well worth the initial trouble - and you learn something in the process.
More information and example source code can be found at Source making.

好吧,由于没有 SSCCE,这将是一个通用的答案。
首先,您可以将文本框设为公共静态字段并从其他类访问它,但这将是您编写的最糟糕的代码之一。其次,您可以使用很酷但随着程序变得复杂而表现不佳的 setter/getter 方法。Setter/getter 将在组件之间创建紧密耦合。最后我建议使用观察者模式。这看起来像是对自行车使用核武器,但最终还是值得一开始的麻烦——你会在这个过程中学到一些东西。
更多信息和示例源代码可以在Source Making 中找到。

回答by basiljames

You should have a Controllerclass from where you create the panel. Keep reference to the panel in the controller class. Expose a getter method in your CustomPanel1to return the text in the TextField(not the textfield itself). Similarly you create the second CustomPanel2also from the Controllerand keep reference. Define the listener class in your controller and pass it to your second panel. In the second panel add the listener to your button.

您应该有一个Controller用于创建面板的类。保持对控制器类中面板的引用。在您中公开一个 getter 方法CustomPanel1以返回 TextField 中的文本(而不是文本字段本身)。同样,您CustomPanel2也从Controller和 保持参考创建第二个。在您的控制器中定义侦听器类并将其传递给您的第二个面板。在第二个面板中,将侦听器添加到您的按钮。

This would be a simple solution.

这将是一个简单的解决方案。

Swing Tutorial

摇摆教程