2

在下面的 GridBagLayout 代码中,如何将所有组件设置为真实的左上角。现在组件总是居中,看起来不太好。我在许多论坛中进行了搜索,但没有为我的代码找到任何好的答案。

private void initializeTabTiming()
  {
    GridBagLayout gridBagLayoutTabTiming = new GridBagLayout();
    GridBagConstraints lastConstraints = new GridBagConstraints();
    GridBagConstraints labelConstraints = new GridBagConstraints();
    GridBagConstraints titleConstraints = new GridBagConstraints();
    this.panelTabTiming = new JPanel(gridBagLayoutTabTiming);

    this.dateChooserStartDate = new JDateChooser(new Date());
    this.dateChooserStartDate.setDateFormatString("dd.MM.yyyy hh:mm:ss");
    this.dateChooserEndDate = new JDateChooser(new Date());
    this.dateChooserEndDate.setDateFormatString("dd.MM.yyyy hh:mm:ss");
    this.dateChooserPauseDate = new JDateChooser(new Date());
    this.dateChooserPauseDate.setDateFormatString("dd.MM.yyyy hh:mm:ss");

    lastConstraints.fill = GridBagConstraints.HORIZONTAL;
    lastConstraints.anchor = GridBagConstraints.NORTHWEST;
    lastConstraints.weightx = 1;
    lastConstraints.gridwidth = GridBagConstraints.REMAINDER;
    lastConstraints.insets = new Insets(8, 8, 8, 8);

    labelConstraints = (GridBagConstraints) lastConstraints.clone();
    labelConstraints.weightx = 0;
    labelConstraints.gridwidth = 1;

    titleConstraints = (GridBagConstraints) lastConstraints.clone();
    titleConstraints.gridwidth = 0;

    gridBagLayoutTabTiming.setConstraints(this.labelTabTimingTitle,
        titleConstraints);
    this.panelTabTiming.add(this.labelTabTimingTitle);

    JLabel temp = new JLabel("");
    gridBagLayoutTabTiming.setConstraints(temp,
        lastConstraints);
    this.panelTabTiming.add(temp);

    gridBagLayoutTabTiming.setConstraints(this.labelTabTimingStartDate,
        labelConstraints);
    this.labelTabTimingStartDate.setLabelFor(this.dateChooserStartDate);
    this.panelTabTiming.add(this.labelTabTimingStartDate);
    gridBagLayoutTabTiming.setConstraints(this.dateChooserStartDate,
        lastConstraints);
    this.panelTabTiming.add(this.dateChooserStartDate);

    gridBagLayoutTabTiming.setConstraints(this.labelTabTimingEndDate,
        labelConstraints);
    this.labelTabTimingEndDate.setLabelFor(this.dateChooserEndDate);
    this.panelTabTiming.add(this.labelTabTimingEndDate);
    gridBagLayoutTabTiming.setConstraints(this.dateChooserEndDate,
        lastConstraints);
    this.panelTabTiming.add(this.dateChooserEndDate);

    gridBagLayoutTabTiming.setConstraints(this.labelTabTimingPauseDate,
        labelConstraints);
    this.labelTabTimingPauseDate.setLabelFor(this.dateChooserPauseDate);
    this.panelTabTiming.add(this.labelTabTimingPauseDate);
    gridBagLayoutTabTiming.setConstraints(this.dateChooserPauseDate,
        lastConstraints);
    this.panelTabTiming.add(this.dateChooserPauseDate);
  }

先感谢您。

4

0 回答 0