如何创建面板的scrollPane

时间:2017-04-03 20:00:58

标签: java user-interface jpanel jscrollpane

我正在尝试以表格形式显示小面板并添加到ScrollPane。但滚动窗格永远不会滚动

是容器的布局还是大小?或者是什么 ?!

以下是我想做的一个例子:

public class ScrollPanePanels extends JFrame{

JPanel  container = new JPanel(null);
JScrollPane scroll = new JScrollPane(container);
public ScrollPanePanels()
{
    super();
    setLayout(null);
    setSize(600,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel Panel1 = new JPanel();
    JPanel Panel2= new JPanel();
    JPanel Panel3 = new JPanel();

    Panel1.setBounds(0,0,500,250);
    Panel2.setBounds(0,250,500,250);
    Panel3.setBounds(0,500,500,250);



    Panel1.setBackground(Color.BLUE);
    Panel2.setBackground(Color.RED);
    Panel3.setBackground(Color.GREEN);


    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    scroll.setBounds(50,50,500,500);

    container.setSize(500,750);
    container.add(Panel1);
    container.add(Panel2);
    container.add(Panel3);

    add(scroll);
    setVisible(true);


}

public static void main(String [] args)
{
    ScrollPanePanels s = new ScrollPanePanels();
}

}

0 个答案:

没有答案