当有足够的空间来显示内容时,JScrollPane不会顶部对齐 [英] JScrollPane doesn't top align when there is more than enough space to show the content

查看:65
本文介绍了当有足够的空间来显示内容时,JScrollPane不会顶部对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JScrollPane,它显示项目列表.如果屏幕上没有足够的空间来显示列表,我希望用户能够滚动浏览列表;如果显示的空间足够,我希望列表与可用空间的顶部对齐.列表.我可以做一个或另一个,但似乎无法同时工作.如下所示的代码实现了以下目的:如果列表大于可用空间,但列表未滚动到顶部,则该列表可以滚动,如下所示.我已经尝试过将JScrollPane放在borderlayout的北部的操作,但是当我这样做时,列表不再是可滚动的(即borderlayout为滚动窗格提供了它所需的所有空间,并且列表现在不可滚动).

I have a JScrollPane that show a list of items. I would like the user to be able to scroll through the list if there is not enough space on the screen to show the list and I would like the list to align to the top of the available space if there is more than enough space to show the list. I can do one or the other but can't seem to get both to work. The code shown below accomplishes the goal of allowing the list to be scrollable if the list is larger than the available space but does not align to the top as shown below. I've tried things like putting the JScrollPane in the north section of a borderlayout but when I do this the list is no longer scrollable (i.e. borderlayout gives the scrollpane all of the space it needs and the list is now not scrollable).

这是下面的代码创建的:

Here's what the code below creates:

这是代码:

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

}

推荐答案

您可以使用Boxlayout.

You can use 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;
    }

}

这篇关于当有足够的空间来显示内容时,JScrollPane不会顶部对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆