import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class JscrollPaneText extends JFrame {
public static void main(String[] args) {
new JscrollPaneText();
}
private JButton[] jbs={
new JButton(“向下拉滚条”),
new JButton(“向上拉滚动条”)
};
private JPanel jp=new JPanel();
private JScrollPane jspg=new JScrollPane(jp,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
public JscrollPaneText() {
super();
this.add(jspg);
this.jp.setLayout(null);
/*
* 设置panel的首选大小,同时保证宽高大于JScrollPane的宽高,这样下面的
* JScrollPane才会出现滚动条
*/
this.jp.setPreferredSize(new Dimension(800,2000));//去了这一条滚动条没有反应
for (int i = 0; i < jbs.length; i++) {
jp.add(jbs[i]);
jbs[i].setBounds(50, 50+1500*i, 150, 40);
}
this.setTitle("为面板加滚动条");
this.setBounds(200, 200, 400, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
借鉴:https://www.cnblogs.com/fdzang/p/9601010.html