简体   繁体   中英

Showing an unknown amount of JButtons in a JPanel with a fixed size

So I am doing a small Uno Game, and Swing seems to like kicking my butt. The problem I'm getting is when a player gets to see and choose which of his cards to put down. I have a given Panel with a fixed size, and a dynamic amount of cards(JButtons with ImageIcons). I am currently using a FlowLayout to get those cards appearing nicely, but if there are too many cards, they start disappearing at the bottom.

The elegant solution I wanted to write consists in having one line with all the cards and just calculating the distance between the cards depending on the amount, so that they'd be spread evenly in the Panel. If there are alot of cards, they'd be overlapping, and that could be maybe solved cleanly with a JLayeredPane. Now, I didn't really get far there, but if you have a solution for that, I would highly appreciate it.

Now the second and more important question is probably me not realizing I have made some dumb mistake. At this Point I'd be happy with the cards disappearing at the bottom and just having a Scrollbar to go down and select those, but that doesn't seem to be working either.

SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneTest {

    public static void main(String[] args) {
        new ScrollPaneTest().init();
    }

    public void init() {

        JFrame frame = new JFrame();

        JPanel panel2 = new JPanel();
        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(200, 100));
        panel.setBackground(Color.GREEN);
        panel.setLayout(new FlowLayout());

        for (int i = 0; i < 15; i++) {
            panel.add(new JButton("Hi :)"));
        }

        JScrollPane scroll = new JScrollPane(panel);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        scroll.add(panel);

        panel2.add(panel, BorderLayout.WEST);

        frame.getContentPane().add(panel2, BorderLayout.NORTH);
        frame.setVisible(true);
        frame.pack();

    }
}

Thanks in advance.

JScrollPane scroll = new JScrollPane(panel);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.add(panel);
panel2.add(panel, BorderLayout.WEST);

That is not how you use a JScrollPane. Comments for each of the 4 lines of code:

  1. the first line is correct. This create a JScrollPane and adds the panel to the viewport of the scroll pane.

  2. you don't need to set the scrollbar policy. The scrollbar will appear automatically, if needed.

  3. don't add a component directly to the scroll pane. The components needs to be added to the JViewport of the scroll pane (which is done for you in the first line of code)

  4. Don't add the "panel" to another panel. A Swing component can only have a single parent. That statement will remove the "panel" from the scroll pane

Instead you need to add the scroll pane to the parent panel:

panel2.add(scroll, BorderLayout.WEST);

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