在 Swing 中切换面板和传递数据
我刚刚开始使用 Swing - 如果这个问题很难理解,我很抱歉,但我觉得这是一件非常简单的事情,但在 Swing 中似乎很难。
我有一个带有两个文本字段和一个提交按钮的面板。
我在提交按钮上添加了一个侦听器,单击它时我会验证数据等。
现在,我希望框架显示一个新面板 - 删除带有文本字段和提交按钮的当前面板,并根据文本字段中输入的数据实例化一个新面板。
如何将此数据发送回框架,以便框架可以删除当前面板并将其替换为使用第一个面板中的数据创建的新的不同面板。
虽然这不是我正在做的事情,但它可以被认为是登录。
显示登录面板 面板获取用户名和密码,进行验证(验证也可以在更高层完成) 如果经过验证,则将登录面板替换为真实内容面板
这在 Swing 中很难弄清楚。我应该定义自己的事件类型并使框架成为该事件的侦听器吗?
I'm just starting out with Swing - I'm sorry if this question is hard to follow, but I feel like this is a very simple thing but it seems surprisingly hard in Swing.
I have a panel with two text fields and a submit button.
I've added a listener on the submit button, when it's clicked I validate the data and such.
Now, I want the frame to display a new panel - get rid of the current panel with the text fields and submit button, and instantiate a new one based on the data entered in the text fields.
How can I send this data back to the frame, so the frame can remove the current panel and replace it with a new, different panel, created with the data from the first panel.
Though it's not what I'm doing, it could be thought of like a login.
Display login panel
Panel gets username and password, validates (validation could be done higher up, too)
If validated, replace login panel with real content panel
This is surprisingly hard to figure out in Swing. Should I be defining my own event type and making the frame a listener for that event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解你的问题,你可以使用这样的回调逻辑;
public MyLoginPanel(IMyCallback callback)
public void processLogin()
方法的接口。您可以从 LoginPanel 调用
callback.processLogin();
它适合您吗?
If I understood your question, you can use callback logic like this;
public MyLoginPanel(IMyCallback callback)
public void processLogin()
method.You could call
callback.processLogin();
from LoginPanelDoes it work for you?
您应该查看 java.awt.CardLayout。此布局可以处理彼此堆叠的多个面板。您可以选择哪个面板应该位于最上面并因此可见。
以下代码显示了上述教程中的相关部分:
以及切换可见面板:
You should look at the java.awt.CardLayout. This Layout can handle multiple panels which are stacked on top of each other. And you can choose which panel should be the topmost and therefore visible.
The following code shows the relevent parts from the tutorial mentioned above:
and to switch the visible panel: