簡體   English   中英

如何從gridlayout知道組件的大小?

[英]how to know the size of the components from gridlayout?

我有一個帶有網格布局的未知尺寸的jpanel,我想知道如何從其組件上獲取尺寸信息。

這是一個例子:

    JPanel basicInformation=new JPanel();
    basicInformation.setMaximumSize(100,100);
    basicInformation.setPreferredSize(basicInformation.getMaximumSize());
    basicInformation.setSize(basicInformation.getMaximumSize());
    basicInformation.setMinimumSize(basicInformation.getMaximumSize());
    basicInformation.setLayout(new GridLayout(2, 2, 10, 10));

    JLabel username=new JLabel("Example");
    username.setHorizontalAlignment(JLabel.CENTER);
    username.setOpaque(true);
    basicInformation.add(username);

我想知道如何獲得標簽的尺寸?

JLabel從Component繼承GetSize ,該組件應返回一個Dimension供您使用。

您可以隨時調用此方法,但是如果框架或面板不可見(即仍在構造中),它將返回0,0。 在任何.setVisible(true) getSize()之后執行getSize()以獲取正確的值。

username.getPreferredSize();

返回維度。

username.getPreferredSize().width; 
username.getPreferredSize().height;

返回各個組件。

暫無
暫無

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

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