简体   繁体   English

垂直滚动条未出现

[英]Vertical ScrollBar isn't appearing

I just began Swing. 我才刚开始摇摆。 I'm trying to make a gallery app. 我正在尝试制作图库应用。 I'm getting images that I manually imported and I display them. 我正在获取手动导入的图像并显示它们。 Depending on the amount of columns I put in parameters, the images' dimensions are calculated to be displayed properly. 根据我在参数中输入的列数,可以计算出图像的尺寸以正确显示。 But, after a certain amount of rows, I want a scrollbar that could scroll and show the rest of images. 但是,经过一定数量的行之后,我想要一个可以滚动并显示其余图像的滚动条。

The images are displayed properly, like I wished but I tried to implement the scrollbar and it isn't appearing. 图像可以正确显示,如我所愿,但是我尝试实现滚动条,但是它没有出现。

Could you tell me what's wrong in my code ? 你能告诉我我的代码有什么问题吗?

    GUI(String title, int width, int height, int columns) {
        this.frame = new JFrame();
        this.frameWidth = width;
        this.columns = columns;

        // set Frame's size
        frame.setSize(width, height);

        // set Frame's action on close button clicked
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // set Frame's title
        frame.setTitle(title);
        frame.setResizable(false);

        // set Frame's position at screen's center
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(dim.width / 2 - frame.getSize().width / 2, (dim.height / 2 - frame.getSize().height / 2));

        panel = new JPanel();
        frame.setLayout(new GridLayout());

        this.scrollBar = new JScrollPane();
        scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(scrollBar, BorderLayout.EAST);

        displayImages();

        frame.setContentPane(panel);
    }

displayImages() : displayImages():

    private void displayImages() {
        for (File currentImage : getImagesList()) {
            // 5 is flowlayout's default borders
            // 2 because we got left and right borders
            int totalBorders = (columns * 5) * 2;
            int buttonWidth = (frameWidth - totalBorders) / columns;

            ImageIcon image = new ImageIcon(new ImageIcon(currentImage.getAbsolutePath()).getImage().getScaledInstance(buttonWidth - 20, buttonWidth - 50, Image.SCALE_DEFAULT));
            JButton button = new JButton(currentImage.getName());

            button.setPreferredSize(new Dimension(buttonWidth, buttonWidth));
            button.setIcon(image);
            button.setHorizontalTextPosition(AbstractButton.CENTER);
            button.setVerticalTextPosition(AbstractButton.BOTTOM);

            button.addActionListener(e -> onImageClicked(currentImage.getName()));
            panel.add(button);
        }
    }

Thanks, best regards 谢谢,最好的问候

    panel = new JPanel();
    frame.setLayout(new GridLayout());

    this.scrollBar = new JScrollPane();
    scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(scrollBar, BorderLayout.EAST);

    displayImages();

    frame.setContentPane(panel);

The above code is mostly wrong. 上面的代码主要是错误的。 You need to: 你需要:

  1. set the layout of the panel and add the buttons to the panel 设置面板的布局并将按钮添加到面板
  2. add the panel containing the buttons to the scroll pane 将包含按钮的面板添加到滚动窗格

The basic code should be something like: 基本代码应类似于:

//panel = new JPanel( new GridLayout(0, 1) );
layout = new GridLayout(0, 1);
panel = new JPanel( layout );
displayImages();
//frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane(panel);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollBar, BorderLayout.EAST);
//displayImages();
//frame.setContentPane(panel);

Also, there is no need to play with the sizes of each component on the panel. 同样,也无需考虑面板上每个组件的尺寸。 The GridLayout will make all the buttons the same size. GridLayout将使所有按钮具有相同的大小。

Edit: 编辑:

When you want to increase the columns in the GridLayout you then just do: 如果要增加GridLayout中的列,则只需执行以下操作:

layout.setColumns( layout.getColumns() + 1 );
panel.revalidate(); // this invokes the layout manager
//panel.repaint(); // sometimes needed to force repainting of panel.

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

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