简体   繁体   English

使用GridLayout时如何在JPanel的底部添加ScrollBar?

[英]How to add a ScrollBar at the bottom of the JPanel when using GridLayout?

Hello i wanted to know how to add a ScrollBar at the bottom of a JPanel if i use GridLayout in my desktop app, as far as i know GridLayout only accept as parameter quantity of colums, rows and the horizontal and vertical gap. 您好,我想知道如何在桌面应用程序中使用GridLayout时在JPanel的底部添加ScrollBar,据我所知,GridLayout仅接受列,行以及水平和垂直间隙的参数数量。 So how can i add a scroll bar and use it to see the information that's in the JPanel? 那么,如何添加滚动条并使用它来查看JPanel中的信息?

Put the JPanel with GridLayout into a JScrollPane . 将带有GridLayoutJPanel放入JScrollPane EG as seen with the two column GridLayout that displays the labels added to the nested layout example . 从两列GridLayout中可以看到EG,该列显示添加到嵌套布局示例中的标签。

If you want the JSrollBar to scroll the JPanel with the gridlayout, then put the grid layout into a scrollpane (remember to extend the scrollable interface) 如果您希望JSrollBar使用gridlayout滚动JPanel,则将网格布局放入滚动窗格中(记住要扩展可滚动界面)

Read this page of the tutorial to learn how to. 阅读教程的此页面以了解如何。

IF you want to use the events from the JScrollBar to change the visible area of your panel then put the panel inside another panel with a JScrollbar on the bottom. 如果要使用JScrollBar中的事件来更改面板的可见区域,然后将面板放在另一个面板中,且JScrollbar在底部。

This is an example with a green panel and a scrollbar at the bottom 这是一个带有绿色面板和底部滚动条的示例

public class Window extends JFrame {

    public Window() {
        setPreferredSize(new Dimension(500, 500));
        setMinimumSize(new Dimension(500, 500));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(100, 100));
        panel.setBackground(Color.GREEN);
        getContentPane().add(panel, BorderLayout.CENTER);

        JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
        scrollBar.setMinimum(0);
        scrollBar.setMaximum(100);
        scrollBar.setBlockIncrement(30);
        scrollBar.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                 System.out.println("Adjustment changed");
            }
        });
        getContentPane().add(scrollBar, BorderLayout.SOUTH);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Window();
    }
}

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

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