JScrollPane不会滚动;尺寸错了吗?

时间:2014-04-21 12:01:26

标签: java swing jscrollpane

我一直试图找到如何让我的JScrollPane滚动的答案,但我真的不明白如何修复它。希望有人能指出我正确的方向。

我的MainArea是一个JPanel,它向它添加了一个JScrollPane。这反过来又添加了一个JPanel作为视口,并且这个JPanel根据ArrayList中的每个玩家信息添加了一些JLabel。我遇到的问题是JScrollPane只显示前8个左右的名字,然后它会离开页面"不允许滚动。

我一直在四处搜寻,发现这可能与我的容器(视口JPanel)没有正确的尺寸有关;如果是这种情况我该怎么办呢?在搜索答案时,我发现使用setPreferredDimension是一个禁忌(woops - 可能需要在那里更改一些代码),但我完全不知道如何使它成为正确的尺寸。有人还提到使用正确的布局管理器应该解决它 - 我为我的JPanel设置了SpringLayout;为什么不做这个工作?

一直在研究这一点,所以也许我只是愚蠢但我的思绪已经空白。干杯,为你提供帮助。

以下代码:

public class PlayerManagerMain extends MainArea {

    private JScrollPane scroll;
    private SpringLayout layout;
    private JPanel panel;

    public PlayerManagerMain(ArrayList<Player> players) {
        super();
        scroll = new JScrollPane();
        layout = new SpringLayout();
        panel = new JPanel();

        panel.setLayout(layout);
        scroll.setPreferredSize(new Dimension(700,300));
        //panel.setBackground(null);
        //panel.setOpaque(false);
        this.add(scroll);

        /*
         * Sort out the scroll bar.
         */
        scroll.setViewportView(panel);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        for(int i=0; i<players.size();i++) {

            /*
             * Creates the name box and sets visual aspects.
             */
            JLabel name = new JLabel(players.get(i).getName()); //Creates a new name JLabel.
            name.setFont(new Font("verdana",Font.PLAIN,15)); //Sets the font.
            name.setPreferredSize(new Dimension(400,35)); //Sets the size of the box.
            name.setBorder(BorderFactory.createLineBorder(new Color(0x222222))); //Creates the border for the JLabel.
            name.setHorizontalAlignment(SwingConstants.CENTER); //Centers the text. 

            /*
             * Creates the status box and sets visual aspects.
             */
            JLabel status = new JLabel(); //Creates a new name JLabel.
            status.setFont(new Font("verdana",Font.PLAIN,15)); //Sets the font.
            status.setPreferredSize(new Dimension(200,35)); //Sets the size of the box.
            status.setBorder(BorderFactory.createLineBorder(new Color(0x222222))); //Creates the border for the JLabel.
            status.setHorizontalAlignment(SwingConstants.CENTER); //Centers the text.

            if(players.get(i).getActive()==true) {
                status.setText("ACTIVE");
                name.setForeground(new Color(0xFF9900));
                status.setForeground(new Color(0xFF9900));
            }
            else {
                status.setText("INACTIVE");
                name.setForeground(new Color(0x990000));
                status.setForeground(new Color(0x990000));
            }
            panel.add(name);
            panel.add(status);

            layout.putConstraint(SpringLayout.WEST, name, 20, SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.NORTH, name, 20+40*i, SpringLayout.NORTH, this);
            layout.putConstraint(SpringLayout.WEST, status, 5, SpringLayout.EAST, name);
            layout.putConstraint(SpringLayout.NORTH, status, 0, SpringLayout.NORTH, name);


        }
        panel.revalidate();
    }

}

0 个答案:

没有答案