在 Swing 中切换面板和传递数据

发布于 2024-10-07 20:23:03 字数 419 浏览 4 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-10-14 20:23:03

如果我理解你的问题,你可以使用这样的回调逻辑;

MyLoginPanel login = new MyLoginPanel(new IMyCallback(){

            public void processLogin(){
                //frame can remove the current panel and replace it with a new
            }

        });
  • MyLoginPanel 从 Jpanel 扩展而来,带有构造函数 public MyLoginPanel(IMyCallback callback)
  • IMyCallback 是一个具有 public void processLogin() 方法的接口。

您可以从 LoginPanel 调用 callback.processLogin();

它适合您吗?

If I understood your question, you can use callback logic like this;

MyLoginPanel login = new MyLoginPanel(new IMyCallback(){

            public void processLogin(){
                //frame can remove the current panel and replace it with a new
            }

        });
  • MyLoginPanel extended from Jpanel with Constructor public MyLoginPanel(IMyCallback callback)
  • IMyCallback is an interface which has public void processLogin() method.

You could call callback.processLogin(); from LoginPanel

Does it work for you?

情感失落者 2024-10-14 20:23:03

您应该查看 java.awt.CardLayout。此布局可以处理彼此堆叠的多个面板。您可以选择哪个面板应该位于最上面并因此可见。

以下代码显示了上述教程中的相关部分:

//Where instance variables are declared:
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";


//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();

//Create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

以及切换可见面板:

CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, TEXTPANEL);

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:

//Where instance variables are declared:
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";


//Where the components controlled by the CardLayout are initialized:
//Create the "cards".
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();

//Create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

and to switch the visible panel:

CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, TEXTPANEL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文