[英]How to properly use GridLayout to position elements in a JFrame?
這就是我希望JFrame看起來像的樣子:
________________________________________________________________________________
|Play - 0 x |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Score List: |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mario: 100 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Luigi: 50 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Waluigi: 20 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Wario: 10 |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|Status: Ready! Score: 30 |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
目前,每個元素的大小均相等,基本上只是將中間部分和頂部/底部分開。 Play是Frame的標題,%s表示用戶可以在其中玩游戲的JPanel。 “狀態:就緒”是JLabel,“得分:30”也是。 分數列表是一個JTextArea。
那么,如何設置這些元素的大小和位置? 到目前為止,我將為您提供我的代碼,也許您可以告訴我我要去哪里了。
this.add(playPanel);
this.add(scoreArea);
this.add(statusLabel);
this.add(scoreLabel);
this.setLayout(new GridLayout(2,2));
GridBagLayout
應該是您的理想解決方案。 它支持遵循組件的首選大小以及GridBagConstraints
巧妙使用。 使用GridBagLayout's GridBagConstraint
屬性:
gridx, gridy
:您的組件將放置在grid(X, Y)
。 在添加任何組件之前,請使用這些屬性指定網格,然后將網格添加到其中。 anchor
: FIRST_LINE_START, PAGE_START, FIRST_LINE_END, LINE_START, LINE_END
等用於在每個網格的可用空間中定向組件。 weightx, weighty
:因為it is the center
標簽,所以weightx
和weighty
被指定為響應容器的width
和height
調整大小。 對於score list
標簽,僅指定weighty
以僅在height
調整容器大小。 gridwidth
:因為it is the center
標簽,所以將此屬性指定為2
以采用兩個網格。 BOTH
, it is the center
填充可用顯示it is the center
標簽的寬度和高度。 並將此屬性設置為“ VERTICAL
以使score list
標簽填充高度的顯示區域。 這是為您構建的演示,可讓您乍一看:
沒有真正編碼為標准( 例如,不應該通過setPreferredSize()
設置首選大小 ),但我認為它符合演示目的。
源代碼:
import java.awt.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.HeadlessException;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.SwingUtilities;
/**
*
* @author Rashed
*/
class GridBagLayoutDemo extends JFrame{
public JLabel createLabel(String txt, int width, int height, Color color)
{
JLabel label = new JLabel(txt);
label.setOpaque(true);
label.setBackground(color);
label.setPreferredSize(new Dimension(width, height));
return label;
}
public GridBagLayoutDemo() throws HeadlessException {
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new java.awt.GridBagLayout());
GridBagConstraints labCnst = new GridBagConstraints();
labCnst.fill = GridBagConstraints.NONE;
labCnst.insets = new Insets(3, 3, 3, 3);
labCnst.anchor = GridBagConstraints.FIRST_LINE_START;
labCnst.gridx = 0;
labCnst.gridy = 0;
panel.add(createLabel("play", 100, 30, new Color(0x359DBD)), labCnst);
// labCnst.anchor = GridBagConstraints.LAST_LINE_START;
labCnst.gridx = 0;
labCnst.gridy = 2;
panel.add(createLabel("Status: Ready!", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.FIRST_LINE_END;
labCnst.gridx = 2;
labCnst.gridy = 0;
panel.add(createLabel("-0x", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.LAST_LINE_END;
labCnst.gridx = 2;
labCnst.gridy = 2;
panel.add(createLabel("score:30", 100, 30, new Color(0x359DBD)), labCnst);
labCnst.anchor = GridBagConstraints.LINE_START;
labCnst.fill = GridBagConstraints.VERTICAL;
labCnst.gridx = 2;
labCnst.gridy = 1;
labCnst.gridwidth = 1;
labCnst.weightx = 0.7;
labCnst.weighty = 0.7;
panel.add(createLabel("ScoreList", 100, 200, new Color(0xFFAA00)), labCnst);
labCnst.gridx = 0;
labCnst.gridy = 1;
//labCnst.anchor = GridBagConstraints.LIN;
labCnst.gridwidth = 2;
labCnst.weightx = 0.8;
labCnst.weighty = 0.8;
labCnst.fill = GridBagConstraints.BOTH;
panel.add(createLabel("It is the center", 200, 200, new Color(0xFFD47E)), labCnst);
//labCnst.anchor = GridBagConstraints.LINE_END;
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutDemo().setVisible(true);
}
});
}
}
您可能想看看Gridbaglayout 。 它可能更適合您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.