我如何将数据从一个 JPanel 传递到另一个 JPanel...?
我有一个 JFrame
,其中包含 3 个 JPanel
。我想将一个面板的 JTextField
值传递给另一个面板。每个面板均使用 JTabbedPane
显示。当我访问其他文本字段的值时,我得到空值。我怎样才能访问?
I have a JFrame
which contains 3 JPanels
. I want to pass the JTextField
value of one panel to other. Each panel is shown using JTabbedPane
. I am getting null when i access the value of other text field. How can i access?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有显示任何代码,因此不可能知道为什么会得到“空”值。如果您希望所有三个 JPanel 都保存具有相同内容的 JTextField,则有两种可能的解决方案:
例如,
编辑 1
我看到您已将其交叉发布在 java-forums.org/ 您在其中显示了一些代码:
问题:
不管怎样,这个设计都不好。您最好向我们展示一个小型演示程序,该程序通过编译和运行的代码显示您的问题,sscce,因此我们可以使用和修改您的代码,并且能够更好地向您展示一个不错的解决方案。
一种不错的解决方案是向
JTextField
添加一个DocumentListener
,以便将JTextField
所保存的文本的更改“推送”到观察者中正在监听变化(你的其他类)。例如,使用文档监听器:
You don't show any code, and so it's impossible to know why you're getting "null" values. Two possible solutions if you want all three JPanels to hold JTextFields with the same content:
e.g.,
Edit 1
I see that you've cross-posted this on java-forums.org/ where you show some of your code:
Problems:
Either way, this design is not good. You're better off showing us a small demo program that shows your problem with code that compiles and runs, an sscce, so we can play with and modify your code and better be able to show you a decent solution.
One such decent solution is to add a
DocumentListener
to theJTextField
so that changes to the text held by theJTextField
are "pushed" into the observers that are listening for changes (your other classes).For example, using DocumentListeners:
一种简单的解决方案是将 JTextField 设为全局,以便所有面板都可以访问它。
确保您的所有面板都可以访问 JTextField,即 textField 是全局可访问的。
以下代码演示了这一点:
One simple solution will be making JTextField global so all panel can access it.
Make sure all your panel can access JTextField that is textField is globally accessible.
Following code demonstrate this:
我不知道你到底要实现什么,但也许尝试数据绑定?
查看 BetterBeansBinding 库。
I don't know what exactly are you going to achieve, but maybe try data binding?
Take a look at BetterBeansBinding library.