GridBagLayout - 左上角的所有组件

时间:2013-04-10 10:58:24

标签: java swing alignment gridbaglayout

如何在以下GridBagLayout代码中将alle组件设置为左上角。现在组件始终居中,看起来不太好。我在很多论坛上搜索过,但是我的代码没有找到任何好的答案。

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);
  }

提前谢谢。

0 个答案:

没有答案