-1
我正在設計一個面板以使用GridBagLayout
來顯示項目。我要尋找的結果是這樣的:並非GridBagLayout中顯示的所有項都行
不過,目前我看到以下內容:
然後我初始化我的面板和設置佈局:
JPanel surveyDetailsFieldsPane = new JPanel();
surveyDetailsFieldsPane.setMinimumSize(new Dimension(0, 300));
surveyDetailsFieldsPane.setPreferredSize(new Dimension(410, 300));
GridBagLayout detailsGridBagLayout = new GridBagLayout();
surveyDetailsFieldsPane.setLayout(detailsGridBagLayout);
surveyDetailsFieldsPane.setBorder(new EmptyBorder(STANDARD_BORDER,
STANDARD_BORDER,
STANDARD_BORDER,
STANDARD_BORDER));
我初始化我的組件以添加到網格中:
JLabel jobNameLbl.setText("Job Name:");
JTextField jobNameTF = new JTextField(20);
JLabel jobNumLbl.setText("Job Number:");
JTextField jobNumTF = new JTextField(12);
然後我用addComponentToSurveyDetailsGrid
方法將組件添加到網格:
// Row 1
addComponentToSurveyDetailsGrid(jobNameLbl, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
addComponentToSurveyDetailsGrid(jobNameTF, 1, 0, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
// Row 2
addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST);
addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
如下所示使用方法:
private void addComponentToSurveyDetailsGrid(Component component, int gridX, int gridY,
int gridWidth, int gridHeight, int weightX,
int weightY, int fill, int anchor) {
GridBagConstraints constraint = new GridBagConstraints();
constraint.gridx = gridX;
constraint.gridy = gridY;
constraint.gridwidth = gridWidth;
constraint.gridheight = gridHeight;
constraint.weightx = weightX;
constraint.weighty = weightY;
constraint.fill = fill;
constraint.anchor = anchor;
constraint.insets = new Insets(Dimens.SMALL_BORDER,
Dimens.SMALL_BORDER,
Dimens.SMALL_BORDER,
Dimens.SMALL_BORDER);
detailsGridBagLayout.setConstraints(component, constraint);
surveyDetailsFieldsPane.add(component);
}
請讓我知道我做錯了這裏!
gridbaglayout是非常棘手的,我想,這就是爲什麼我切換到jgoodies formlayout。我想這與你的'weightx'有關,你如何創建你的網格 – XtremeBaumer
1)爲了更好地幫助你,請發佈[MCVE]或[Short,Self Contained,Correct Example](http://www.sscce。組織/)。 2)以最小尺寸提供ASCII藝術或簡單的GUI圖形*圖形,並且如果可調整大小,則具有更大的寬度和高度。 –
@AndrewThompson提供的圖紙有什麼問題? – petehallw