亲。我改成如下,可是两个按钮之间的距离太大了。不符合我的要求。要如何修改。谢谢。下面是我修改后的代码: public class Test extends JFrame { public Test() { GridLayout gridlayout = new GridLayout(); gridlayout.setColumns(1); gridlayout.setRows(0); gridlayout.setVgap(2); // 两个按钮之间的垂直距离为2px
JButton jb = new JButton("TTTTT"); JPanel jp_1 = new JPanel(); jp_1.add(jb);
JButton jb1 = new JButton("BBBBBB"); JPanel jp_2 = new JPanel(); jp_2.add(jb1);
JPanel jpanel = new JPanel(); jpanel.setLayout(gridlayout); jpanel.add(jp_1); jpanel.add(jp_2); JScrollPane jsp = new JScrollPane(jpanel);
在JScrollPane中建2个JPanel,再将你的按钮分别放入JPanel中,一个JPanel中放一个按钮,JPanel采用流布局
public class Test extends JFrame {
public Test() {
GridLayout gridlayout = new GridLayout();
gridlayout.setColumns(1);
gridlayout.setRows(0);
gridlayout.setVgap(2); // 两个按钮之间的垂直距离为2px
JButton jb = new JButton("TTTTT");
JPanel jp_1 = new JPanel();
jp_1.add(jb);
JButton jb1 = new JButton("BBBBBB");
JPanel jp_2 = new JPanel();
jp_2.add(jb1);
JPanel jpanel = new JPanel();
jpanel.setLayout(gridlayout);
jpanel.add(jp_1);
jpanel.add(jp_2);
JScrollPane jsp = new JScrollPane(jpanel);
this.add(jsp);
this.setSize(500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
不要用gridlayout,这是平均分配大小的。可以试试Boxlayout
double p = TableLayout.PERFERRED;
Container con = getContentPane();
con.setLayout(new TableLayout(new double[]{f}, new double[] {p, p, f});
con.add(new JButton("One"), "0, 0, c, c");
con.add(new JButton("Two"), "0, 1, c, c");搞定
点击右边那个 "jar" 链接