繁体   English   中英

Java Swing 滚动窗格不滚动

[英]Java Swing Scrollpane not scrolling

我希望能够向下滚动动态生成的电影列表。 我尝试添加一个滚动窗格。

我的应用程序看起来如何

我在页面开始处有一个导航栏,在中心有一个包含所有电影的 jpanel。

您可以使用以下代码重新创建此示例:

private static JFrame frame;

public static void main(String[] args) throws HeadlessException {
    frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.setBackground(new Color(32, 32, 32));
    
    JPanel navigationPanel = createNavigationBar();
    frame.add(navigationPanel, BorderLayout.PAGE_START);
    
    JPanel moviePanel = createMoviePanel();
    frame.add(moviePanel, BorderLayout.CENTER);
    
    frame.setPreferredSize(new Dimension(1920, 1080));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Example App");
    frame.pack();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);
}

// MoviePanel Class
public static JPanel createMoviePanel() {
    JPanel parentMoviePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 25));
    JPanel contentPanel = new JPanel(new GridLayout(0, 1));
    JPanel moviePanel = new JPanel(new GridLayout(0, 9, 8, 5));
    
    JScrollPane scrollPane = new JScrollPane(moviePanel);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
    scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    
    parentMoviePanel.setBackground(new Color(32, 32, 32));
    contentPanel.setBackground(new Color(32, 32, 32));
    moviePanel.setBackground(new Color(32, 32, 32));

    final File root = new File("");

    for (int i = 0; i < 70; i++) {
        // Get the image and scale it down
        BufferedImage movieCover=new BufferedImage(150,200,BufferedImage.TYPE_INT_RGB); 
           Graphics2D g2d=(Graphics2D)movieCover.getGraphics();  
           g2d.setColor(Color.GRAY);  
           g2d.fillRect(0,0,movieCover.getWidth(),movieCover.getHeight());

        // Create button and change settings
        JButton movieButton = new JButton("Movie " + i, new ImageIcon(movieCover));
        movieButton.setMargin(new Insets(0, 0, 0, 0));
        movieButton.setPreferredSize(new Dimension(180, 250));
        movieButton.setForeground(Color.WHITE);
        movieButton.setContentAreaFilled(false);
        movieButton.setBorderPainted(false);
        movieButton.setFocusPainted(false);
        movieButton.setHorizontalTextPosition(JButton.CENTER);
        movieButton.setVerticalTextPosition(JButton.BOTTOM);

        moviePanel.add(movieButton);
        scrollPane.revalidate();
    }
    
    contentPanel.add(moviePanel);
    contentPanel.add(scrollPane);
    parentMoviePanel.add(contentPanel);
    
    return parentMoviePanel;
}

// Navbar Class
public static JPanel createNavigationBar() {
    JPanel navBar = new JPanel();
    navBar.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 20));
    navBar.setBackground(new Color(25, 25, 25));
    
    JButton homeButton = new JButton("Home");
    homeButton.setContentAreaFilled(false);
    homeButton.setBorderPainted(false);
    homeButton.setFocusPainted(false);
    
    JButton movieButton = new JButton("Movies");
    movieButton.setContentAreaFilled(false);
    movieButton.setBorderPainted(false);
    movieButton.setFocusPainted(false);
    
    // Add all the buttons to the navbar
    navBar.add(homeButton);
    navBar.add(movieButton);
    
    return navBar;
}

我想要做的是使用我的鼠标滚轮向下滚动这个电影列表,而不会看到任何类型的滚动条。 它看起来应该和现在的样子一模一样,但我希望能够向下滚动并查看所有电影。

我不知道为什么它不起作用,这就是为什么我在这里问,希望有人能向我解释我做错了什么。

您对滚动窗格的使用不正确。

一个 Swing 组件只能有一个父组件。 以下代码正在创建带有子组件的滚动窗格。 但是,当您将 moviePanel 添加到内容窗格时,您会从滚动窗格中删除它。

所以滚动窗格没有子组件,永远不会工作。

JScrollPane scrollPane = new JScrollPane(moviePanel);
...    
contentPanel.add(moviePanel);
contentPanel.add(scrollPane);

但是,即使那样也不能解决您的问题,因为您现在在顶层面板上使用 FlowLayout,因此所有子组件将始终以其首选大小显示,因此不需要滚动条。

摆脱 createMoviePanel() 方法中的所有滚动窗格逻辑。

相反,您可能希望将滚动窗格添加到内容窗格:

//frame.add(moviePanel, BorderLayout.CENTER);
frame.add(new JScrollPane(moviePanel), BorderLayout.CENTER);

现在,滚动窗格将随着框架大小的变化而动态调整大小。 然后将根据需要显示滚动条。

暂无
暂无

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

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