我现在这个画面打开以后最下面的4个button是可以随着画面大小移动的,但是上面的几个panel只有纵向拉升才会变化,横向好像就变化不了,这是怎么回事?还有就是我想button固定大小,但是要随着窗体变大变小而移动位置。import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
public class MyFrameNew extends JFrame {

private JPanel pnlMain = null;

private JPanel jp1= null;

private JPanel jp2= null;

private JPanel jp3= null;

private JPanel jp4= null;

private JPanel pnl1 = null;

private JPanel pnl2 = null;

private JPanel pnl3 = null;

private JPanel pnl4 = null;

private JPanel pnl5 = null;

private JPanel pnl6 = null;

private JPanel pnlIns1 = null;

private JPanel pnlIns2 = null;

private JPanel pnlIns3 = null;

private JPanel pnlIns4 = null;

private JPanel pnlIns5 = null;

private JPanel pnlIns6 = null;

private JPanel pnlS1 = null;

private JPanel pnlS2 = null; private JPanel pnlS3 = null;

private JButton btn1 = null;

private JButton btn2 = null;

private JButton btn3 = null;

private JButton btn4 = null;

JSplitPane spltCenter1 = null; JSplitPane spltCenter2 = null;

JSplitPane spltCenter3 = null;

JSplitPane spltCenter4 = null;

JSplitPane spltCenter5 = null;

JSplitPane spltCenter6 = null;

private JPanel pnlWset = null;

private JPanel pnlEast = null;

private JLabel lblConditionName = null;

private JTextField txtCrrCode = null;

private JComboBox cboSpecificProblemID = null;

private JButton btnSelect = null;

public MyFrameNew() {
init();
}

public void init() {
pnlMain = new JPanel();
jp1=new JPanel(); 
jp2=new JPanel(); 
jp3=new JPanel(); 
jp4=new JPanel(); 
pnl1= new JPanel();
pnl2= new JPanel();
pnl3= new JPanel();
pnl4= new JPanel();
pnl5= new JPanel();
pnl6= new JPanel();
pnlIns1= new JPanel();
pnlIns2= new JPanel();
pnlIns3= new JPanel();
pnlIns4= new JPanel();
pnlIns5= new JPanel();
pnlIns6= new JPanel();
pnlS1= new JPanel();
pnlS2= new JPanel();
pnlS3= new JPanel();
pnlWset= new JPanel();
pnlEast= new JPanel();
btn1 = new JButton("バージョン取得");
btn1.setPreferredSize(new Dimension(105,10));
btn2 = new JButton("表示更新");
btn2.setPreferredSize(new Dimension(105,10));
btn3 = new JButton("実行");
btn3.setPreferredSize(new Dimension(105,10));
btn4 = new JButton("中止");
btn4.setPreferredSize(new Dimension(105,10));
spltCenter1 = new JSplitPane(0);
spltCenter2 = new JSplitPane(0);
spltCenter3 = new JSplitPane(0);
spltCenter4 = new JSplitPane(0);
spltCenter5 = new JSplitPane(0);
spltCenter6 = new JSplitPane(0);
JSplitPane   js=new     JSplitPane(0); 
JSplitPane   js1=new     JSplitPane(0);
lblConditionName = new JLabel();
btnSelect = new JButton();


lblConditionName = new JLabel("条件名称");


cboSpecificProblemID = new JComboBox();

btnSelect = new JButton("選択");
btnSelect.setPreferredSize(new Dimension(65,20));

this.setLayout(new BorderLayout());
pnlS1.setLayout(new BorderLayout());
pnlS2.setLayout(new BorderLayout());
pnlS3.setLayout(new BorderLayout());
spltCenter1.setLayout(new BorderLayout());
spltCenter2.setLayout(new BorderLayout());
spltCenter3.setLayout(new BorderLayout());
spltCenter4.setLayout(new BorderLayout());
spltCenter5.setLayout(new BorderLayout());
spltCenter6.setLayout(new BorderLayout());
pnl1.setLayout(new BorderLayout());
pnl2.setLayout(new BorderLayout());
pnl3.setLayout(new BorderLayout());
pnl4.setLayout(new BorderLayout());
pnl5.setLayout(new BorderLayout());
pnl6.setLayout(new BorderLayout());

pnlMain.setLayout(new BorderLayout());

//バージョン取得、表示更新、実行、中止 start
btn1.setPreferredSize(new Dimension(130,10));
btn2.setPreferredSize(new Dimension(130,10));
btn3.setPreferredSize(new Dimension(130,10));
btn4.setPreferredSize(new Dimension(130,10));
pnl1.add(btn1,BorderLayout.WEST);
pnl1.add(pnlIns1,BorderLayout.CENTER);
pnlIns1.setPreferredSize(new Dimension(12,20));
pnl1.add(btn2,BorderLayout.EAST);


pnl3.add(pnl1,BorderLayout.WEST);
pnl3.add(pnlIns2,BorderLayout.CENTER);
pnlIns2.setPreferredSize(new Dimension(50,20));
pnl3.add(btn3,BorderLayout.EAST);

pnl4.add(pnl3,BorderLayout.WEST);
pnl4.add(pnlIns3,BorderLayout.CENTER);
pnlIns3.setPreferredSize(new Dimension(12,20));
pnl4.add(btn4,BorderLayout.EAST);
//バージョン取得、表示更新、実行、中止 end


//NE/RET選択、ファイルリスト、転送リスト start
jp1.setBorder(BorderFactory.createTitledBorder("NE/RET選択"));
jp1.setPreferredSize(new Dimension(700,200));
jp2.setBorder(BorderFactory.createTitledBorder("ファイルリスト"));
jp2.setPreferredSize(new Dimension(700,200));
jp3.setBorder(BorderFactory.createTitledBorder("転送リスト"));
jp3.setPreferredSize(new Dimension(700,200));
pnlS2.setPreferredSize(new Dimension(700,100));
js1.setLeftComponent(jp1); 
js1.setRightComponent(jp2); 
js.setLeftComponent(js1); 
js.setRightComponent(jp3); 

pnlS2.add(pnl4,BorderLayout.EAST);
pnlS3.add(pnlIns4,BorderLayout.NORTH);
pnlS3.add(pnlS2,BorderLayout.CENTER);
pnlS3.add(pnlIns5,BorderLayout.SOUTH);

pnlS1.add(js,BorderLayout.NORTH);
pnlS1.add(pnlS3,BorderLayout.CENTER);

pnlMain.add(pnlWset,BorderLayout.WEST);
pnlMain.add(pnlEast,BorderLayout.EAST);
pnlMain.add(pnlS1,BorderLayout.CENTER);
this.setTitle("AISGダウンロード");
this.add(pnlMain,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(800, 700);
this.setMaximumSize(new Dimension(800,700));
this.setMinimumSize(new Dimension(800,700));
//NE/RET選択、ファイルリスト、転送リスト end
this.addWindowListener(
             new WindowAdapter(){
                 public void windowClosing(WindowEvent e){
                    System.exit(0);        
                 }        
             }        
        );     

}

public static void main(String args[]) {
MyFrameNew mf = new MyFrameNew();
}}

解决方案 »

  1.   

    把你的JFrame 布局设置成GridLayout这样你设置的按钮也好还是其它的也好,不会随着你拖动窗口大小的改变而移动位置,我自己的项目里面用过能行,你上面的代码我没时间帮你测试,希望这点小建议对你有用。
      

  2.   

    开源组件 JFormLayout   Swing布局
      

  3.   

    AWT自带的几个layout manager功能还是有限,你说的需求不一定能被它们实现。
    可以自定义一个LayoutManager的子类,然后再setLayout()设置你定义的layout, 这样可以达到到你的要求的
      

  4.   

    定义两个变量 获取 窗口大小。把窗口布局清空.按钮就自定义坐标添加.这个坐标可以根据窗口的长和宽来计算 .比如1/3.或者1/4 的位置  这样不管你窗口怎么改变按钮的位置 始终是按照窗口的比例来的.当然也可以使用 GridLayout() 布局 这样就简单多了...