桌面应用程序 - 如何动态创建和销毁表单

发布于 2024-09-26 16:43:01 字数 530 浏览 10 评论 0原文

我正在使用 java 为桌面创建一个小型加密应用程序。

我正在将 JFrames(导入 javax.swing.JFrame)与 Oracle 一起使用 Linux 下的 JDeveloper 11g。

我想要一个“欢迎”表单/框架供用户选择 他们的加密方法,然后选择方法, 我想动态地创建适当的表单 选择加密方法并销毁/释放/处置() 欢迎表格。当用户完成加密后, 他们应该关闭框架/表单(通过单击 x 在右上角 - 或使用“退出”按钮或通过任何 方法)并且欢迎框架应该动态重新创建 并出现。

我尝试过各种方法 - btnEncode_actionPerformed(ActionEvent e) 然后 this.dispose() - 我摆弄了 this_windowClosed(WindowEvent e) 和 dispose(),但似乎没有任何作用。

即使使用 setVisibl(true/false) 的解决方法也是可以接受的 这个阶段——这让我一整天都伤透了脑筋。这是非常 在德尔福很容易做到!

TIA 和 rgs,

保罗...

I'm creating a small crypto app for the desktop using java.

I'm using JFrames (import javax.swing.JFrame) with Oracle
JDeveloper 11g under Linux.

I want to have a "welcome" form/frame where users can choose
their encryption method, and then on choosing the method,
I want to dynamically create the appropriate form for the
chosen encryption method and also destroy/free/dispose() of
the welcome form. When the user has finished their encrypting,
they should close the frame/form (either by clicking on the
x at the top right - or using the Exit button or by any
method) and the welcome frame should be dynamically recreated
and appear.

I've tried various things - btnEncode_actionPerformed(ActionEvent e)
then this.dispose() - and I've fiddled with this_windowClosed(WindowEvent e)
and dispose(), but nothing seems to work.

Even a workaround using setVisibl(true/false) would be acceptable at
this stage - this has been wrecking my head all day. It's very
easy to do in Delphi!

TIA and rgs,

Paul...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

野の 2024-10-03 16:43:01

像这样的东西通常可以解决问题:(注意我还没有测试过这个)

public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
    this.dispose();
    SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}

其中 btnContinue 是欢迎表单上的“继续”按钮,而 JFrameAppropriateWindow 是您想​​要根据用户的选择显示的下一帧。 Args 是您需要传递的任何参数。

准备好后,您可以简单地处理当前框架并重新启动 WelcomeMsg 的实例

something like this usually does the trick: (Note I haven't tested this)

public class WelcomeMsg extends JFrame
.
.
.
public void btnContinue_actionPerformed(ActionEvent e)
{
    this.dispose();
    SwingUtilities.invokeLater(new Runnable(){ new JFrameAppropriateWindow(args) });
}

where btnContinue is the Continue button on your welcome form and JFrameAppropriateWindow is the next frame you would like to show depending on the user's choice. Args are any arguments you need to pass.

When you are ready, you can simply dispose the current frame and relaunch an instance of WelcomeMsg

李白 2024-10-03 16:43:01

我整理了这个简单的示例,用于根据用户的选择创建和显示面板。

public class Window extends JFrame {

public Window() {
    this.setLayout(new BorderLayout());
    JComboBox encryptionCombobox = new JComboBox();
    encryptionCombobox.addItem("foo");
    encryptionCombobox.addItem("bar");
    //...
    encryptionCombobox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // find choices and the correct panel
            JPanel formPanel = new JPanel();
            formPanel.setOpaque(true);
            formPanel.setBackground(Color.RED);
            //...
            Window.this.add(formPanel, BorderLayout.CENTER);
            Window.this.validate();
            Window.this.repaint();
        }
    });
    add(encryptionCombobox, BorderLayout.NORTH);
}

public static void main(String[] args) {
    new Window().setVisible(true);
}
}

当我想到这一点时,您可能应该使用 CardLayout,它允许您在不同的面板(卡片)之间切换。

I put together this simple example for creating and displaying a panel depending on a user choice.

public class Window extends JFrame {

public Window() {
    this.setLayout(new BorderLayout());
    JComboBox encryptionCombobox = new JComboBox();
    encryptionCombobox.addItem("foo");
    encryptionCombobox.addItem("bar");
    //...
    encryptionCombobox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // find choices and the correct panel
            JPanel formPanel = new JPanel();
            formPanel.setOpaque(true);
            formPanel.setBackground(Color.RED);
            //...
            Window.this.add(formPanel, BorderLayout.CENTER);
            Window.this.validate();
            Window.this.repaint();
        }
    });
    add(encryptionCombobox, BorderLayout.NORTH);
}

public static void main(String[] args) {
    new Window().setVisible(true);
}
}

When I come to think about it, you should probably use a CardLayout instead, which allows you to switch between different panels (cards).

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