簡體   English   中英

Java-GridLayout調整JPanel的大小

[英]Java - GridLayout resize JPanel

我正在嘗試調整另一個具有GridLayout的JPanel中每個JPanel的高度。

這是我到目前為止的內容:

JPanel store = new JPanel();
store.setLayout(new GridLayout(4, 0));

JPanel computerPanel = new JPanel();
computerPanel.setBorder(BorderFactory.createTitledBorder("Computers"));
computerPanel.setPreferredSize(new Dimension(20, 30));

JPanel cablesPanel = new JPanel();
cablesPanel.setBorder(BorderFactory.createTitledBorder("Cables"));

JPanel accessoriesPanel = new JPanel();
accessoriesPanel.setBorder(BorderFactory.createTitledBorder("Accessories"));

JPanel repairPanel = new JPanel();
repairPanel.setBorder(BorderFactory.createTitledBorder("Repair"));

store.add(computerPanel);
store.add(cablesPanel);
store.add(accessoriesPanel);
store.add(repairPanel);

JPanel view = new JPanel();
view.setLayout(new BorderLayout());
view.add(store, BorderLayout.CENTER);

add(view);

這是GUI的輸出: 屏幕截圖

我想調整JPanel存儲中每個JPanel的大小(computerPanel,cablelPanel,accessoriesPanel,repairPanel)。

我努力了:

computerPanel.setPrefferedSize()但是不起作用...

如果您希望能夠調整每個JPanel的大小,則說明您沒有使用適當的LayoutManager。 GridLayout(您現在正在使用)明確不允許這種大小調整,而是將每個單元格固定為完全相同的大小。

考慮使用其他LayoutManager,例如BoxLayout或GridBagLayout。

請參閱此處以確定哪種LayoutManager最適合您的需求。

暫無
暫無

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

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