简体   繁体   English

将JScrollPane添加到JPanel

[英]Add JScrollPane to JPanel

I try to make a scrollbar for a JPanel but when I RUN, the scrollbar not even show up. 我尝试为JPanel制作滚动条,但是当我运行时,滚动条甚至没有显示出来。

EDITED: This is the latest code, I updated the contentPane layout. 编辑:这是最新的代码,我更新了contentPane布局。

This is main Panel: 这是主面板:

contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setPreferredSize(new Dimension(1200, 400));
setContentPane(contentPane);

This is JPanel inside the main Panel: 这是主面板中的JPanel:

bigPanel = new JPanel();
bigPanel.setPreferredSize(new Dimension(1137, 520));

bigPanel.setBackground(new Color(224, 255, 255));

JScrollPane scrollPane = new JScrollPane(bigPanel);
scrollPane.setPreferredSize(new Dimension(600, 600));

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);   
contentPane.add(scrollPane,"1, 2, 6, 1, center, default");   
bigPanel.setLayout(new FormLayout(new ColumnSpec[] {...}
bigPanel.add(BUTTON1);
bigPanel.add(BUTTON2);.....

Above code was written under: 上面的代码写在:

public UI2(){...}

This is the launch application code: 这是启动应用程序代码:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {

                UI2 frame = new UI2();                      
                frame.pack();
                frame.setDefaultCloseOperation(UI2.EXIT_ON_CLOSE);
                frame.setSize(1376,788);                
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

bigPanel has many buttons. bigPanel有很多按钮。 So I need a vertical and horizontal scrollbar within bigPanel. 所以我需要在bigPanel中使用垂直和水平滚动条。

This is the screenshot after I RUN but no scrollbar shown: 这是我运行后的屏幕截图,但未显示滚动条: bigPanel具有绿色背景

EDITED: This is the latest screenshot, the bigPanel shows empty after I wrote the code as above. 编辑:这是最新的屏幕截图,在我编写上述代码后,bigPanel显示为空。 在此处输入图片说明

I believe the problem is with contentPane.setLayout(null); 我相信问题在于contentPane.setLayout(null); , which means that the JScrollPane has not definable size and is not been shown because of it. ,这意味着JScrollPane没有可定义的大小,因此没有显示出来。

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

Also contentPane.add(BigPanel,"1, 2, 6, 1, center, default"); 同样contentPane.add(BigPanel,"1, 2, 6, 1, center, default"); should be contentPane.add(scrollPane,"1, 2, 6, 1, center, default"); 应该为contentPane.add(scrollPane,"1, 2, 6, 1, center, default"); . Once you correct for the layout issue, the scroll pane should become visible 解决布局问题后,滚动窗格应可见

See Laying Out Components Within a Container for more ideas 有关更多想法,请参见在容器中布置组件

To force a JScrollPane to always display scroll bars, set the visibility policy: 要强制JScrollPane始终显示滚动条,请设置可见性策略:

ScrollPane sp = new JScrollPane(panel);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

Instead of bigPanel.setBounds(162, 157, 1137, 520); 代替bigPanel.setBounds(162, 157, 1137, 520); call bigPanel.setPreferredSize(1137, 520); 调用bigPanel.setPreferredSize(1137, 520); then JScrollPane can use the content size 然后JScrollPane可以使用内容大小

UPDATE bigPanel.setPreferredSize(new Dimension(1137, 520)); 更新bigPanel.setPreferredSize(new Dimension(1137, 520));

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

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