单击按钮时打开面板

发布于 2024-12-22 01:58:00 字数 69 浏览 0 评论 0原文

有谁知道如何让 GUI 按钮在 java 中打开一个新的 JPanel 吗?它不在谷歌上。它显示一个关于面板。感谢您的帮助!

does anyone know how to make a GUI button open up a new JPanel in java? its not on google. its to show an about panel. thanks for the help!

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

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

发布评论

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

评论(4

三岁铭 2024-12-29 01:58:01

您可以使用未装饰的 JDialog 显示面板

    public static void main(String args[])
    {
         final JDialog bwin = new JDialog();
         bwin.addWindowFocusListener(new WindowFocusListener()
         {
             @Override
             public void windowLostFocus(WindowEvent e)
             {
               bwin.setVisible(false);
               bwin.dispose();
             }

             @Override
             public void windowGainedFocus(WindowEvent e)
             {
             }
         }); 

        bwin.setUndecorated(true);

        JLabel label = new JLabel("About");

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(label);
        panel.setPreferredSize(new Dimension(200,200));

        bwin.add(panel);
        bwin.pack();
        bwin.setVisible(true);
    }

You can show panel using an undecorated JDialog

    public static void main(String args[])
    {
         final JDialog bwin = new JDialog();
         bwin.addWindowFocusListener(new WindowFocusListener()
         {
             @Override
             public void windowLostFocus(WindowEvent e)
             {
               bwin.setVisible(false);
               bwin.dispose();
             }

             @Override
             public void windowGainedFocus(WindowEvent e)
             {
             }
         }); 

        bwin.setUndecorated(true);

        JLabel label = new JLabel("About");

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(label);
        panel.setPreferredSize(new Dimension(200,200));

        bwin.add(panel);
        bwin.pack();
        bwin.setVisible(true);
    }
最初的梦 2024-12-29 01:58:00

我想 JDialog 就是您所需要的。

有关详细信息,请参阅此:如何制作对话框

这是示例:

在此处输入图像描述

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CreateDialogFromOptionPane {

    public static void main(final String[] args) {
        JFrame parent = new JFrame();
        JButton button = new JButton();

        button.setText("Click me to show dialog!");
        parent.add(button);
        parent.pack();
        parent.setVisible(true);

        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JOptionPane optionPane = new JOptionPane("Is this what you need?", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
                JDialog dialog = optionPane.createDialog("Dialog");
                dialog.setVisible(true);
            }
        });
    }
}

I guess JDialog is what you need.

See this for details : How to Make Dialogs

Here is a sample :

enter image description here

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CreateDialogFromOptionPane {

    public static void main(final String[] args) {
        JFrame parent = new JFrame();
        JButton button = new JButton();

        button.setText("Click me to show dialog!");
        parent.add(button);
        parent.pack();
        parent.setVisible(true);

        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JOptionPane optionPane = new JOptionPane("Is this what you need?", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
                JDialog dialog = optionPane.createDialog("Dialog");
                dialog.setVisible(true);
            }
        });
    }
}
岁吢 2024-12-29 01:58:00

我认为通过实现 CardLayout 你可以解决这个问题

I think that by implements CardLayout you can solve that

白云不回头 2024-12-29 01:58:00

首先,您需要为按钮创建一个事件处理程序,然后在处理程序中您应该创建面板并使其可见。如果您想要更多弹出窗口,您应该使用类似:

JOptionPane.showMessageDialog(frame, "This is my message");

这将创建一条警告消息,您也可以创建自己的服装对话框,我建议阅读 这个

first you would need to create an event handler for your button, then in your handler you should create your panel and make it visible. if you want more of a popup you should use like:

JOptionPane.showMessageDialog(frame, "This is my message");

that will create a warning message, you could also create your own costume dialog i would suggest reading this

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