0

我在这里尝试做的是设置布局方案,一旦我按下一个按钮将所有按钮对齐到 contentPane 的左侧我有三个按钮这是我为那个特定按钮提供的代码

  leftButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
   cp.setLayout( new FlowLayout(FlowLayout.LEFT));

  }
 } );

它也只编译 find 但不向左对齐任何东西。我必须在此代码中添加更多内容还是应该这样做?

4

1 回答 1

1

设置好新的 LayoutManager 后,您需要在容器上调用 revalidate()。因此,从您的代码中,类似以下的内容应该可以工作。

leftButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        cp.setLayout( new FlowLayout(FlowLayout.LEFT));
        cp.revalidate();
    }
} );
于 2009-12-10T01:45:55.787 回答