简体   繁体   English

将JScrollpane添加到包含多个JPanel的JPanel中

[英]Add JScrollpane to JPanel containing multiple JPanels

I have one JFrame (black) that contains one main JPanel (grey), this JPanel contains three other JPanels : North and South (red) and the Center one (blue). 我有一个JFrame(黑色),其中包含一个主JPanel(灰色),此JPanel包含其他三个JPanel:北和南(红色)和居中的一个(蓝色)。 The blue JPanel in the center will have lots of JPanels (green) added dynamically to it during the course of the program. 中间的蓝色JPanel将在程序执行过程中动态添加许多JPanels(绿色)。 When the Center JPanel is full I would like a JScrollbar to appear automatically to scroll down the Center Panel and see all the child (green) panels it contains. 当中心JPanel装满时,我希望JScrollbar自动出现以向下滚动中心面板并查看其中包含的所有子面板(绿色)。 Can somebody help me? 有人可以帮我吗? The problem is that the scrollbar isn't appearing at all, if i add 15 green JPanels to my blue JPanel container, i only see 10 and i can't scroll down. 问题是,滚动条根本不会出现,如果我将15个绿色的JPanel添加到我的蓝色JPanel容器中,我只会看到10个,并且无法向下滚动。

JFrame和JPanels的模板

This is the type of code i have tried so far... 到目前为止,这是我尝试过的代码类型...

    JPanel panelNorth = new JPanel(new GridBagLayout());
    panelNorth.setPreferredSize(new Dimension(1000,100);
    //add some labels and buttons

    JPanel panelCenter = new JPanel();
    panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
    panelCenter.setPreferredSize(new Dimension(1000,500)
    JPanel panel1 = new JPanel(new GridLayout(1, 10));
    panel1.setPreferredSize(new Dimension(1000,50));
    panelCenter.add(panel1);
    //...etc dynamically


    JPanel panelSouth = new JPanel(new GridBagLayout());
    panelSouth.setPreferredSize(new Dimension(1000,100);
    //add some labels and buttons

    JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //panelCenter must be scrollable when too many panels are added to panelCenter

    //add everything to the main panel container
    JPanel panelContainer = new JPanel(new BorderLayout(3,3));
    panelContainer.setBorder(new EmptyBorder(5,5,5,5));
    panelContainer.add(panelNorth,BorderLayout.NORTH);
    panelContainer.add(scrollPaneCenter,BorderLayout.CENTER);
    panelContainer.add(panelSouth,BorderLayout.SOUTH);

    //add everything to frame   
    JFrame frame = new JFrame();
    frame.add(panelContainer);

Thank you. 谢谢。

EDIT: I changed 编辑:我改变了

 JScrollPane scrollPaneCenter = new    JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

to this : 对此:

 JScrollPane scrollPane = new JScrollPane(panelCentre,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

And this is what i get: 这就是我得到的:

在此处输入图片说明

I can see it now, it's in the right place, i just can't scroll down to see my other components. 我现在可以看到它,它在正确的位置,我只是无法向下滚动以查看其他组件。

Fixed it by switching 通过切换固定

panelCenter.setPreferredSize(new Dimension(1000,500);

with

scrollPane.setPreferredSize(new Dimension(1000,500));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM