当垂直滚动条可见时,Swing BoxLayout水平滚动条

时间:2011-10-04 11:50:46

标签: swing scrollbar visibility

在下面的SSCCE中,您会看到即使可以使用垂直滚动条也会出现水平滚动条。这只是一个例子。我使用自己的LayoutManager而不是BoxLayout,但如果你有解决方案,我可以在我自己的LayoutManager中使用它。

我已经尝试使用实现Scrollable的自定义JPanel,然后覆盖TrackScrollableViewportWidth并返回true,但是没有得到我想要的。然后,文本在右侧被切断。

另一件事,那并不重要,但仍然让我烦恼:如果你增加窗口的宽度,水平滚动条就会消失,JTextArea的宽度也会增加。但是如果减小窗口的宽度,JTextArea就不会相应地调整其宽度。

提前致谢。这是SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class MyFrame extends JFrame
{
    private static final long serialVersionUID = 1L;

    public static void main(final String[] args)
    {
        new MyFrame();
    }

    private MyFrame()
    {

        setTitle("Scrollbars and width");
        setSize(800, 200);
        setResizable(true);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel container = new JPanel();
        final BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
        container.setLayout(layout);

        final JTextArea area1 = new JTextArea();
        area1.setText("1sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf ");
        area1.setLineWrap(true);
        area1.setWrapStyleWord(true);
        area1.setEditable(false);
        area1.setForeground(Color.RED);
        container.add(area1);

        final JTextArea area2 = new JTextArea();
        area2.setText("2sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf sdh jsdfk laskdf hdkf hsjdf ");
        area2.setLineWrap(true);
        area2.setWrapStyleWord(true);
        area2.setEditable(false);
        area2.setBackground(Color.RED);
        container.add(area2);

        final JScrollPane scrollbox = new JScrollPane(container);

        this.getContentPane().add(scrollbox, BorderLayout.CENTER);

        this.getContentPane().invalidate();
        this.getContentPane().validate();

    }
}

1 个答案:

答案 0 :(得分:1)

与BoxLayout无关,更改为单列GridLayout以进行验证。

原因(很可能)是文本区域稍微有点奇怪的大小调整行为 - 在执行实际操作之前需要将其推入内部初始化(即通过LayoutManager调整大小)。添加假大小调用(在区域配置期间的某个地方),确切的值是无关的

  area1.setSize(1, Short.MAX_VALUE);