package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Change  extends JFrame implements ActionListener
{
  private JButton button1,button2,button3;
  Container c;
  public Change()
  {
    super("Change");
    button1 = new JButton("Left");
    button2 = new JButton("Center");
    button3 = new JButton("Right");
    c =getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT));
    c.add(button1);
    c.add(button2);
    c.add(button3);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    setSize(400,400);
    show();
  }
  public void actionPerformed(ActionEvent e)
  {
    String command = e.getActionCommand();
    if (command.equals("Left"))
     { c.setLayout(new FlowLayout(FlowLayout.LEFT));
     
      }
    if (command.equals("Center"))
      c.setLayout(new FlowLayout(FlowLayout.CENTER));
    if (command.equals("Right"))
      c.setLayout(new FlowLayout(FlowLayout.RIGHT));
  }
  public static void main(String args[])
  {
    Change cpp = new Change();
  cpp.addWindowListener(new MyWindowListener());  }
}

解决方案 »

  1.   

    if (command.equals("Left"))
         { c.setLayout(new FlowLayout(FlowLayout.LEFT));
         //这里加个this.validate;看看
          }
      

  2.   

    cpp.addWindowListener(new MyWindowListener());这句有问题,但我不知道要怎么改
      

  3.   

    不是的,我可以运行的,但点击按钮时没有反应,只有把框的大小拉一下就动了,可能是缺少一个刷新的语句,我用REPAINT()试过了,没有用的
      

  4.   

    你把REPAINT()的贴出来。REPAINT()是有点麻烦的东西。一般都要线程来实现。
      

  5.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Change extends JFrame implements ActionListener {
    private JButton button1, button2, button3; Container c; public Change() {
    super("Change");
    button1 = new JButton("Left");
    button2 = new JButton("Center");
    button3 = new JButton("Right");
    c = getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.LEFT));
    c.add(button1);
    c.add(button2);
    c.add(button3);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    setSize(400, 400);
    show();
    } public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("Left")) {
    c.setLayout(new FlowLayout(FlowLayout.LEFT));
    }
    if (command.equals("Center")) {
    c.setLayout(new FlowLayout(FlowLayout.CENTER));
    }
    if (command.equals("Right")) {
    c.setLayout(new FlowLayout(FlowLayout.RIGHT));
    }

    ((JComponent) c).revalidate();
    } public static void main(String args[]) {
    Change cpp = new Change();
    }
    }