简体   繁体   English

Java Swing:当文本字段超出滚动窗格的视图范围时,允许滚动

[英]Java Swing: Allow scroll when textfields are out out of range of scrollpane's view

I have a tabbed pane tabbedPane_1 with scrollpane scrlPnAddServer , which has viewport view set to pnlAddServer . 我有一个标签面板tabbedPane_1与滚动面板scrlPnAddServer ,其设置为视景pnlAddServer How can i add textfields or other items below the viewable section of the scrollpane. 如何在滚动窗格的可视部分下方添加文本字段或其他项目。 (and allow the user to scroll down to view the items) Currently the items are just not shown when out of range. (并允许用户向下滚动以查看项目)当前,超出范围时不会显示这些项目。

        JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane_1.setBounds(672, 20, 350, 400);
        panel.add(tabbedPane_1);
        JPanel tab = new JPanel();
        tabbedPane_1.add("Build", tab);
        tab.setLayout(null);
        JPanel tab2 = new JPanel();
        tabbedPane_1.add("More...", tab2);
        tab2.setLayout(null);

        JScrollPane scrlPnAddServer = new JScrollPane();
        scrlPnAddServer.setBounds(0, 0, 350, 370);
        tab2.add(scrlPnAddServer);
        scrlPnAddServer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

        JPanel pnlAddServer = new JPanel();
        pnlAddServer.setBounds(0, 0, 350, 370);
        scrlPnAddServer.setViewportView(pnlAddServer);
        pnlAddServer.setLayout(null);

Then i add items that I want the user to scroll down to see: 然后,我添加我希望用户向下滚动以查看的项目:

    tfSaveAs = new JTextField("Save As");
    tfSaveAs.setBounds(10, 600, 125, 20);
    pnlAddServer.add(tfSaveAs);
    tfSaveAs.setColumns(10);

Make use of an appropriate layout manager which is capable of calculating the required information which is used the JScrollPane to make decisions whether it should display the scrollbars or not. 利用适当的布局管理器,该管理器能够计算所需的信息,使用JScrollPane可以决定是否显示滚动条。

This is just one of the many "auto magical" aspects that the layout management API provides 这只是布局管理API提供的许多“自动魔术”方面之一

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旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

See Laying Out Components Within a Container for more details 有关更多详细信息,请参见在容器中布置组件

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

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