Java:GridLayout在调整大小之前调用layoutContainer

时间:2011-02-10 14:58:52

标签: java resize grid-layout

我使用GridLayout Manager有一个带容器的简单gui。 我添加了两个从JComponent继承的组件,并使用paintComponent在屏幕上绘制一些东西。

现在我添加了componentListener来使用GridLayout Manager调整gui的大小。 调整大小后,这两个组件仍然很小,所以没有调整大小。

我通过创建一个简单的GridLayout副本来检查这一点,该副本打印了methode layoutContainer从父容器获取的大小,以查看父级是否具有新大小(在调整大小事件之后)。 它打印的尺寸仍然很小,只有很小的变化,但不是正确的。

我在layoutContainer中打印大小,并使用一个简单的Timer每秒打印我的父gui的大小。

我意识到在Timer打印正确的大小(1600x1099)之前,我的GridLayout Manager中的layoutContainer会被调用旧的大小。

我认为GridLayout总是根据行和列配置自动调整其所有组件的大小。但似乎没有,layoutContainer方法过早被调用。

有没有办法使用GridLayout Manager来实现这一点,还是我必须自己调整组件大小?

如何在LayoutManager中检查何时以及调用layoutContainer方法?

下面是正确调整大小的一个模块的代码,但它的子类子组件没有正确调整大小(希望它没有太多的代码):

信息:我在一个java文件中为这个模块编写了所有需要的类,所以我在一个java文件中拥有了我需要的一切。

public class Main extends Module {

  // ############### MAIN PAGE COMPONENT ##############
  public class MainPage extends ModulePageContainer {
    // ############### CLOCK COMPONENT ##############
    public class ClockComponent extends JComponent {
      private Date              currentDateTime;
      private SimpleDateFormat  dateFormat;

      public ClockComponent() {
        this.dateFormat = new SimpleDateFormat( "HH:mm");
      }

      @Override
      protected void paintComponent( Graphics g) {
        super.paintComponent( g);
        //...doing some paint stuff here...
      }

      public void setDateTime( Date dateTime) {
        this.currentDateTime = dateTime;
      }
    }
    // ############### CLOCK COMPONENT END ##############

    // ############### MAIN INFO COMPONENT ##############
    public class MainInfoComponent extends JComponent {   
      public MainInfoComponent() {
        this.setLayout( null);
      }

      @Override
      protected void paintComponent( Graphics g) {
        super.paintComponent( g);
        // ... just empty subcomponent ...
      }
    }

    // ############### MAIN INFO COMPONENT END ##############

    private ClockComponent    clock;
    private MainInfoComponent mainInfo;

    public MainPage( Module parent) {
      super( parent);
      this.clock = new ClockComponent();
      this.mainInfo = new MainInfoComponent();
      this.setLayout( new MyGridLayout( 2, 1));
      this.add( this.clock);
      this.add( this.mainInfo);
    }

    public void clockTick( Date date) {
      this.clock.setDateTime( date);
      this.repaint();
    }
  }

  // ############### MAIN PAGE COMPONENT END ##############    
  public Main( String name) {
    super( name);
  }

  @Override
  public void init() {
    // every module has pages, this module has only one page called 'main'
    MainPage mainPage = new MainPage( this);
    mainPage.setName( "main");
    this.addPage( mainPage);
  }
}

0 个答案:

没有答案