簡體   English   中英

零寬度高度jPanel啟動時

[英]Zero Width Height jPanel 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
}

面板返回0, 0因為很簡單,這是默認值。

將面板添加到框架並在框架上調用pack()時,將計算正確的(首選)尺寸並進行設置。 在此之前,您將無法找到尺寸,因為它尚未計算。

你為什么需要這些價值? 如果您能從更廣泛的角度解釋問題,我們可能會幫助您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM