我一直在嘗試使用GridBagLayout來組織一堆面板(包含一組改變面板大小的標籤,主要是垂直)。我之所以選擇這個主要原因是因爲面板尺寸可能會有很大的不同,我希望面板尺寸改變時可以縮放面板。以表格格式組織動態大小的面板(Java SWING)
我不斷遇到不同尺寸的面板(通過面板檢查邊界),奇怪的尺寸問題,無法讓面板垂直相互粘合(面板間的巨大空間)。
Gridbag似乎是最好的選擇,但它似乎成爲比我想象的更大的痛苦。其他選項在改變幀大小時提供適當的縮放比例?
我一直在嘗試使用GridBagLayout來組織一堆面板(包含一組改變面板大小的標籤,主要是垂直)。我之所以選擇這個主要原因是因爲面板尺寸可能會有很大的不同,我希望面板尺寸改變時可以縮放面板。以表格格式組織動態大小的面板(Java SWING)
我不斷遇到不同尺寸的面板(通過面板檢查邊界),奇怪的尺寸問題,無法讓面板垂直相互粘合(面板間的巨大空間)。
Gridbag似乎是最好的選擇,但它似乎成爲比我想象的更大的痛苦。其他選項在改變幀大小時提供適當的縮放比例?
很久以前我已經成爲what @SplungeBob showed me的粉絲。下面的代碼片段讓您瞭解如何完成下面顯示的GUI。
frame.add(menu, new GBConstraints(0, 0).spanX(32).fill(HORIZONTAL));
frame.add(new JLabel("Available letters"),
new GBConstraints(0, 1).insets(2, 2, 0, 2));
frame.add(new JLabel("Play by play..."),
new GBConstraints(2, 1).insets(0, 5, 0, 0));
frame.add(pnlPlays, new GBConstraints(2, 1).insets(10, 0, 0, 0).spanY(14).anchor(WEST).fill(BOTH));//.ipad(0,0));
frame.add(pnlAvail, new GBConstraints(0, 2).insets(0, 5, 5, 5));
frame.add(pnlLegend, new GBConstraints(1, 2) .spanY(14) .fill(BOTH));
frame.add(pnlBonuses, new GBConstraints(0, 3).insets(0, 100, 0, 0));
frame.add(pnlSBBonuses, new GBConstraints(0, 3).insets(0, 125, 0, 0));
frame.add(gamePanel, new GBConstraints(0, 5).insets(0, 50, 0, 0));
frame.add(pnlTheScore, new GBConstraints(0, 5) .anchor(EAST));
btnDebug.setPreferredSize(new Dimension(1, 1));
frame.add(btnDebug, new GBConstraints(0, 0) .anchor(WEST));
frame.add(btnNextReplayStep,new GBConstraints(0, 6).insets(0,310, 0, 0) .anchor(CENTER));
btnNextReplayStep.setVisible(false);
frame.add(new JLabel("User letters"),
new GBConstraints(0, 6).insets(0,240, 30, 0));
frame.add(btnCopyUser, new GBConstraints(0, 6).insets(0,310,0,0) .anchor(CENTER));
frame.add(btnCopyAllLetters,new GBConstraints(0, 6) .anchor(EAST));
frame.add(pnlNotes, new GBConstraints(0, 6).insets(25,10,10, 10).spanY( 2).anchor(WEST));
frame.add(btnClearMnemHi, new GBConstraints(0, 7).insets(0,430, 0, 0));
frame.add(btnClearBorders, new GBConstraints(0, 7).insets(0,530, 0, 0));
我不會撒謊;它仍然是一項重大任務,但仔細規劃時,需要在垂直和水平線上標註實際網格,以顯示一個字段必須有多少列,這是值得一試的。而且@Hovercraft所建議的甚至更好的機會非常棒。
(我還沒有掌握它,它似乎怪異insets
值似乎表明,但它是一個非常漂亮的可靠GUI)。
編輯
下面是如何將兩個代碼在執行上面的代碼之前創建了JPanel
對象。一個也使用GridBagLayout
;其他沒有。只要是最簡單和最好的作品。
private void makePlays(){
pnlPlays = new JPanel(new GridBagLayout());
pnlPlays.setSize(500,300);
pnlPlays.add(scrPlays, new GBConstraints(0,1).span(40, 62)
.fill(BOTH).ipad(100, 50));
scrPlays.setEnabled(false);
}
private void makeScoremasterBonuses(){
BonusBackgroundColors c;
BonusBackgroundColors all [] = BonusBackgroundColors.values();
String labels[] = new String[all.length];
String abbrs [] = new String[all.length];
JButton but;
pnlBonuses = new JPanel();
pnlBonuses.add(new JLabel("Legend:"));
for (int i = 0; i < all.length; i++) {
labels[i] = all[i].name().replace("_", " ").toLowerCase();
abbrs [i] = Utilities.abbreviate(all[i].name());
c = BonusBackgroundColors.values()[i];
but = new JButton(abbrs[i]);
but.setToolTipText(labels[i]);
but.setBackground(c.background(c));
but.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
but.setActionCommand("" + i);
but.addActionListener(this);
pnlBonuses.add(but);
}
}
1)查看MigLayout的使用情況,您需要下載第三方佈局管理器。 2),或者如果您希望我們的幫助來幫助您優化GridBagLayout或任何其他佈局,請返回此處的代碼,最好是[mcve],以及所需和觀察GUI的圖像。 –
編輯 - 找到下載鏈接,但不確定要下載哪一個,那麼多版本。 –