简体   繁体   中英

Java Swing Scrollpane not scrolling

I want to be able to scroll down a dynamically generated list of movies. I tried adding a Scrollpane.

我的应用程序看起来如何

I have a navigation bar at the page start and in the center a jpanel with all the movies.

You can recreate this example by using this code:

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;
}

What I'm trying to do is to scroll down this list of movies, using my mouse wheel without seeing any kind of scrollbar. It should look exactly how it looks now, but I want to be able to scroll down and see all the movies.

I don't know why it isn't working that's why I'm asking here in hope someone can explain to me what I'm doing wrong.

Your usage of a scroll pane is incorrect.

A Swing component can only have a single parent. The following code is creating the scroll pane with a child component. However you then remove the moviePanel from the scroll pane when you add it to the content pane.

So the scroll pane has no child component and will never work.

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

However, even that won't solve your problem because you are now using a FlowLayout on your top level panel so all the child components will always be displayed at their preferred size so there is no need for scroll bars.

Get rid of all the scroll pane logic in your createMoviePanel() method.

Instead you probably want to add the scroll pane to the content pane:

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

Now the scroll pane will dynamically resize as the frame size changes. Scrollbars will then appear as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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