简体   繁体   English

零宽度高度jPanel启动时

[英]Zero Width Height jPanel on start

Why does the jPanel return 0 for the height and width when it starts and how can I get the right values on start. 为什么jPanel在启动时返回高度和宽度为0,如何在启动时获得正确的值。

import javax.swing.JPanel;

class ZeroJPanel extends JPanel {

    /**
     * Creates new form ZeroJPanel
     */
    ZeroJPanel() {
        initComponents();

        System.out.println( this.getHeight() );
    }


    public static void main(String Args[]) {
        new ZeroJPanel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    // End of variables declaration
}

The panel returns 0, 0 because, quite simply, that is the default value. 面板返回0, 0因为很简单,这是默认值。

When you add the panel to a frame, and call pack() on the frame, that will calculate the correct (preferred) size and set it. 将面板添加到框架并在框架上调用pack()时,将计算正确的(首选)尺寸并进行设置。 Until then, you won't be able to find the size because it hasn't been calculated. 在此之前,您将无法找到尺寸,因为它尚未计算。

Why do you need these values? 你为什么需要这些价值? If you can explain the problem in broader terms, we may be able to help you. 如果您能从更广泛的角度解释问题,我们可能会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM