NetBeans 和Swing - 动态添加JPanel到JDialog

发布于 2025-01-06 20:13:33 字数 1389 浏览 5 评论 0 原文

我正在 NetBeans 中设计一个应用程序,如下面的屏幕截图所示。

在此处输入图像描述

当用户单击 JFrame 上的 JButton 时,会弹出一个 JDialog,要求用户输入使用数字键盘输入数值。我希望 JDialog 动态添加 2 个 JPanel。 JPanel 1 将包含一个用于输入的文本框。 JPanel 2 将包含一个数字键盘。我这样设计它们是为了我可以在需要时重复使用数字键盘。我面临的问题是在弹出的 JDialog 上动态显示这 2 个 JPanel。 JDialog 弹出为空。请看下面我的代码。谢谢大家,非常感谢您的帮助

这是 JDialog 的示例代码:

public class MyDialog extends javax.swing.JDialog {

    public MyDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {//Add JPanel 2 (Numeric Keypad) to JDialog
                Container contentPane = getContentPane();
                NumericKeypadPanel nkp = new NumericKeypadPanel();
                nkp.setLayout(new java.awt.BorderLayout());
                contentPane.removeAll();
                contentPane.add(nkp);
                contentPane.validate();
                contentPane.repaint();
            }
        });
    }

这是 JPanel 2(数字键盘)的示例代码:

public class NumericKeypadPanel extends javax.swing.JPanel {

    /** Creates new form NumericKeypadPanel */
    public NumericKeypadPanel() {
        initComponents();//Draws 10 number buttons
    }
}

I am designing an application in NetBeans, as illustrated in the screenshot below.

enter image description here

When the user clicks on a JButton on a JFrame, a JDialog pops-up asking the user to enter a numeric value using a numeric keypad. I would like the JDialog to dynamically add 2 JPanels. JPanel 1 will contain a textbox for input. JPanel 2 will contain a numeric keypad. I designed them this way so that I could reuse the numeric keypad whenever I need it. The problem I am facing is displaying dynamically these 2 JPanels on the JDialog that pops-up. JDialog pops-up empty. Please take a look at my code below. Thank you all, I appreciate your help

This is the sample code of JDialog:

public class MyDialog extends javax.swing.JDialog {

    public MyDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {//Add JPanel 2 (Numeric Keypad) to JDialog
                Container contentPane = getContentPane();
                NumericKeypadPanel nkp = new NumericKeypadPanel();
                nkp.setLayout(new java.awt.BorderLayout());
                contentPane.removeAll();
                contentPane.add(nkp);
                contentPane.validate();
                contentPane.repaint();
            }
        });
    }

This is the sample code for JPanel 2 (Numeric Keypad):

public class NumericKeypadPanel extends javax.swing.JPanel {

    /** Creates new form NumericKeypadPanel */
    public NumericKeypadPanel() {
        initComponents();//Draws 10 number buttons
    }
}

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

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

发布评论

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

评论(2

救赎№ 2025-01-13 20:13:33

基本上有两种方法

1) 通过在屏幕上保持 JDialog 大小(以像素为单位)添加新的 JComponent,所有 JCompoenets
或者可以缩小其中的一部分

2) 通过调用 pack() 调整 JDialog 的大小,然后 JDialog 将调整

我的 am 规则都可以通过使用 href="http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html" rel="nofollow noreferrer">标准 LayoutManagerAbsoluteLayout)

basicall there are two ways

1) add a new JComponent by holding JDialog size (in pixels) on the screen, all JCompoenets
or part of them could be shrinked

2) resize JDialog by calling pack(), then JDialog will be resized

both my a.m. rulles works by using Standard LayoutManagers (excepting AbsoluteLayout)

任性一次 2025-01-13 20:13:33

NumericKeypadPanel 的 initComponents() 函数中有什么内容?如果它实际上没有创建组件,您将不会在对话框中看到任何内容。我在 NumericKeypadPanel 的构造函数中添加了一行来更改该面板的背景颜色,事实上,它在对话框中显示为绿色面板。

public NumericKeypadPanel() {
    //initComponents();//Draws 10 number buttons
    setBackground(Color.green);
}

What is in the initComponents() function of the NumericKeypadPanel? If it's not actually creating components, you're not going to see anything in the dialog. I added a single line to the NumericKeypadPanel's constructor to change the background color of this panel, and indeed, it shows up in the dialog as a green panel.

public NumericKeypadPanel() {
    //initComponents();//Draws 10 number buttons
    setBackground(Color.green);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文