JFrame中做好布局BorderLayout,加入控件后,按某个按钮,我要改变JFrame的尺寸,于是用setSize的方法。结果除了整个JFrame尺寸变化之外,JFrame里的控件都没有随之改变位置。我用了repaint方法也没用。
只有先hide,再show才起作用。还有没有什么好办法让JFrame setSize之后里面的控件也相应改变位置?

解决方案 »

  1.   

    revalidate()方法我试过了,也不行啊。
      

  2.   

    // validate();
    import java.awt.BorderLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;public class SetSizeTest extends JFrame
    {
    /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[])
    {
    try
    {
    SetSizeTest frame = new SetSizeTest();
    frame.setVisible(true);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } /**
     * Create the frame
     */
    public SetSizeTest()
    {
    super();
    setTitle("http://www.boasoft.com");
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton httpwwwboasoftcomButton = new JButton();
    httpwwwboasoftcomButton.setText("http://www.boasoft.com");
    getContentPane().add(httpwwwboasoftcomButton, BorderLayout.NORTH);
    final JButton button_1 = new JButton();
    button_1.setText("New JButton");
    getContentPane().add(button_1, BorderLayout.WEST);
    final JButton button_2 = new JButton();
    button_2.setText("New JButton");
    getContentPane().add(button_2, BorderLayout.EAST);
    final JButton httpwwwboasoftcomButton_1 = new JButton();
    httpwwwboasoftcomButton_1.setText("http://www.boasoft.com");
    getContentPane().add(httpwwwboasoftcomButton_1, BorderLayout.SOUTH);
    final JButton setsizeTestButton = new JButton();
    setsizeTestButton.addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent e)
    {
    setSize(800, 600);
    validate();
    }
    });
    setsizeTestButton.setText("SetSize 测试");
    getContentPane().add(setsizeTestButton, BorderLayout.CENTER);
    //
    }
    }
      

  3.   

    给一个简单的:
    import javax.swing.*;
    import java.awt.BorderLayout;
    public class JustTest extends JFrame
    {
    public JustTest()
    {
    super("Test");
    setSize(400,300);
    this.setLayout(new BorderLayout());
    this.add(new JButton("button"),BorderLayout.WEST);
    setVisible(true);
    setResizable(true);
    }
    public static void main(String[] args)
    {
    new JustTest();
    }
    }
      

  4.   

    JFrame中用BorderLayout;里面的控件当然会随Frame的变化而变化的了。
    你可以设置Frame的Layout 为null;即JFrame.setLayout(null);
    这样里面的按纽什么的就不会随Frame的变化而变化了。
    其它的FlowLayout都是随Frame的变化而变化的。