有滚动条的新建窗口

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;




public class Test extends JFrame{

public Test(){
this.setSize(400, 300);
JPanel jp = new JPanel();


JScrollPane jsp = new JScrollPane();
//jsp.add(jp);
//jsp.setLayout(null);


//jp.setSize(400, 300);

jsp.getViewport().add(jp);
jsp.setVerticalScrollBarPolicy(JScrollPane.
               VERTICAL_SCROLLBAR_AS_NEEDED);  //设置竖滚动条  需要时出现
   
jsp.setHorizontalScrollBarPolicy(JScrollPane.
             HORIZONTAL_SCROLLBAR_NEVER); //横滚动条,永远不出现
jp.setPreferredSize(new Dimension(jsp.getWidth(),150));
this.getContentPane().add(jsp);

JButton jb = null;
for(int i = 0;i<10;i++){
jb = new JButton();
jb.setText("button"+i);
jp.add(jb);
}
}

public static void main(String[] args) {
Test t = new Test();
t.setVisible(true);
}




}

你可能感兴趣的:(有滚动条的新建窗口)