当有足够的空间来显示内容时,JScrollPane不会对齐

时间:2016-03-11 13:19:17

标签: java swing jscrollpane layout-manager

我有一个显示项目列表的JScrollPane。如果屏幕上没有足够的空间来显示列表,我希望用户能够滚动列表,如果有足够的空间可以显示,我希望列表对齐到可用空间的顶部列表。我可以做其中一个但似乎无法兼顾两者。如果列表大于可用空间但未与顶部对齐,则下面显示的代码实现了允许列表可滚动的目标,如下所示。我已经尝试过将JScrollPane放在borderlayout的北部,但是当我这样做时,列表不再可滚动(即borderlayout为滚动窗板提供了所需的所有空间,列表现在不可滚动)。

以下是以下代码创建的内容:

enter image description here

enter image description here

这是代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

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

public class Example {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(400, 200);
        // main panel
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(1, 2));
        pan.setBackground(Color.BLUE);
        jFrame.getContentPane().add(pan, BorderLayout.CENTER);
        jFrame.show();
        // left panel
        JPanel left = getContentPanel();
        left.setBackground(Color.ORANGE);
        pan.add(left);
        // right panel (with scroll pane)
        JPanel right = getContentPanel();
        right.setBackground(Color.YELLOW);
        JScrollPane scr = new JScrollPane(right);
        scr.setBackground(Color.CYAN);
        scr.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        scr.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
        pan.add(scr);
    }

    private static JPanel getContentPanel() {
        JPanel rtn = new JPanel();
        rtn.setLayout(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        cs.gridx = 0;
        cs.weightx = 1;
        cs.anchor = GridBagConstraints.NORTHWEST;
        for (int i = 0; i < 20; i++) {
            JLabel label = new JLabel("Item " + (i + 1));
            label.setBackground(Color.DARK_GRAY);
            cs.gridy = i;
            rtn.add(label, cs);
        }
        rtn.setBackground(Color.GREEN);
        return rtn;
    }

}

1 个答案:

答案 0 :(得分:2)

您可以使用Boxlayout。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Example {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(400, 200);
        // main panel
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(1, 2));
        pan.setBackground(Color.BLUE);
        jFrame.getContentPane().add(pan, BorderLayout.CENTER);
        jFrame.show();
        // left panel
        JPanel left = getContentPanel();
        left.setBackground(Color.ORANGE);
        pan.add(left);
        // right panel (with scroll pane)
        JPanel right = getContentPanel();
        right.setBackground(Color.YELLOW);
        JScrollPane scr = new JScrollPane(right);
        scr.setBackground(Color.CYAN);
        scr.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        scr.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
        pan.add(scr);
    }

    private static JPanel getContentPanel() {
        JPanel rtn = new JPanel();

        rtn.setLayout(new BoxLayout(rtn, BoxLayout.Y_AXIS));

        for (int i = 0; i < 20; i++) {
            JLabel label = new JLabel("Item " + (i + 1));
            label.setBackground(Color.DARK_GRAY);
            rtn.add(label);
        }

        rtn.add(Box.createVerticalGlue());

        rtn.setBackground(Color.GREEN);
        return rtn;
    }

}