JScrollPane 固定宽度 [英] JScrollPane with fixed width

查看:74
本文介绍了JScrollPane 固定宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java Swing 的新手,我对下一个代码感到困惑.

我的目标是制作垂直滚动面板,其中包含 2 个 JTextPane(s).第一个 JTextPane 固定宽度为父面板的 70%,第二个 JTextPane 固定宽度为 30%.因为两个 JTextPane(s) 都有固定的宽度,所以它们只会在垂直方向上扩展更多的文本.

我使用这个解决方案,因为我希望这 2 个 JTextPane 只有一个滚动条.

我的初始化代码:

private void initialize() {框架 = 新 JFrame();frame.setBounds(100, 100, 616, 374);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new BorderLayout(0, 0));JScrollPane scrollPane = new JScrollPane();scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_​​ALWAYS);frame.getContentPane().add(scrollPane);JPanel 面板 = new JPanel();scrollPane.setViewportView(panel);SpringLayout sl_panel = new SpringLayout();panel.setLayout(sl_panel);JTextPane leftTextPane = new JTextPane();sl_panel.putConstraint(SpringLayout.NORTH, leftTextPane, 10, SpringLayout.NORTH, panel);sl_panel.putConstraint(SpringLayout.WEST, leftTextPane, 10, SpringLayout.WEST, panel);panel.add(leftTextPane);JTextPane rightTextPane = new JTextPane();sl_panel.putConstraint(SpringLayout.NORTH, rightTextPane, 10, SpringLayout.NORTH, panel);sl_panel.putConstraint(SpringLayout.WEST, rightTextPane, 10, SpringLayout.EAST, leftTextPane);sl_panel.putConstraint(SpringLayout.EAST, rightTextPane, -10, SpringLayout.EAST, panel);panel.add(rightTextPane);scrollPane.addComponentListener(new ComponentAdapter(){public void componentResized(ComponentEvent evt) {sl_panel.putConstraint(SpringLayout.EAST, leftTextPane, (int)(scrollPane.getWidth() * 0.7), SpringLayout.WEST, (Component)(evt.getSource()));}});}

JTextPane(s) 对 SOUTH 没有约束,所以他们可以这样成长.

问题:

  • JTextPane 仅在插入一些文本后调整大小.
  • 垂直滚动条不起作用.

解决方案

问题在于滚动窗格会以其首选大小显示组件,然后根据需要添加滚动条.

在您的情况下,您希望宽度受到滚动窗格视口的限制.

因此,您需要在添加到视口的组件上实现 Scrollable 接口.Scrollable 接口将允许您强制组件宽度与视口的宽度相匹配,这反过来将限制每个 JTextPane 的宽度,导致文本换行.

实现此功能的一种简单方法是使用可滚动面板.此类实现了 Scrollable 接口,并允许您使用参数覆盖 Scrollable 方法.

所以基本代码是:

ScrollablePanel panel = new ScrollablePanel( new BorderLayout());panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

<块引用>

第一个 JTextPane 固定宽度为父面板的 70%,第二个 JTextPane 固定宽度为 30%

执行此操作的一种方法可能是使用 JSplitPane,这样您在两个文本窗格之间就有一个分隔符,并且文本不会合并为一个.

JSplitPane splitPane = new JSplitPane();splitPane.setLeftComponent(new JTextPane());splitPane.setRightComponent(new JTextPane());splitPane.setResizeWeight(0.7);splitPane.setDividerLocation(.7);

然后您只需将所有内容添加到框架中:

panel.add(splitPane);frame.add(new JScrollPane(panel), BorderLayout.CENTER);

现在分隔线位置将保持在 70%,文本窗格将随着框架的大小调整而增大/缩小.

I am newbie in Java Swing and I am confused about next code.

My goal is make vertical scrollable panel with 2 JTextPane(s) inside it. The first JTextPane with fixed width 70 % of parent panel and the second JTextPane with fixed width 30 %. Because the both JTextPane(s) have fixed width, they expand with more text only vertically.

I use this solution, because I want to have only one scrollbar for this 2 JTextPane(s).

My init code:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 616, 374);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout(0, 0));

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(scrollPane);

    JPanel panel = new JPanel();
    scrollPane.setViewportView(panel);
    SpringLayout sl_panel = new SpringLayout();
    panel.setLayout(sl_panel);

    JTextPane leftTextPane = new JTextPane();
    sl_panel.putConstraint(SpringLayout.NORTH, leftTextPane, 10, SpringLayout.NORTH, panel);
    sl_panel.putConstraint(SpringLayout.WEST, leftTextPane, 10, SpringLayout.WEST, panel);
    panel.add(leftTextPane);

    JTextPane rightTextPane = new JTextPane();
    sl_panel.putConstraint(SpringLayout.NORTH, rightTextPane, 10, SpringLayout.NORTH, panel);
    sl_panel.putConstraint(SpringLayout.WEST, rightTextPane, 10, SpringLayout.EAST, leftTextPane);
    sl_panel.putConstraint(SpringLayout.EAST, rightTextPane, -10, SpringLayout.EAST, panel);
    panel.add(rightTextPane);

    scrollPane.addComponentListener(new ComponentAdapter() 
    {
        public void componentResized(ComponentEvent evt) {
            sl_panel.putConstraint(SpringLayout.EAST, leftTextPane, (int)(scrollPane.getWidth() * 0.7), SpringLayout.WEST, (Component)(evt.getSource()));
        }
    });
}

JTextPane(s) have no constraint for SOUTH, so they can grow up in this way.

Problems:

  • JTextPane(s) resize only after insert some text into them.
  • The vertical scrollbar is not working.

解决方案

The problem is that a scrollpane will display a component at its preferred size and then add scroll bars as required.

In your case you want the width to be constrained by the viewport of the scrollpane.

So therefore you need to implement the Scrollable interface on the component you add to the viewport. The Scrollable interface will allow you to force the component width to match the width of the viewport which in turn will limit the width of each JTextPane causing the text to wrap.

An easy way to implement this functionality is to use the Scrollable Panel. This class implements the Scrollable interface and allows you to override the Scrollable methods by using parameters.

So the basic code would be:

ScrollablePanel panel = new ScrollablePanel( new BorderLayout());
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

The first JTextPane with fixed width 70 % of parent panel and the second JTextPane with fixed width 30 %

One way to do this might be to use a JSplitPane so you have a divider between the two text panes and the text doesn't merge into one.

JSplitPane splitPane = new JSplitPane();
splitPane.setLeftComponent(new JTextPane());
splitPane.setRightComponent(new JTextPane());
splitPane.setResizeWeight(0.7);
splitPane.setDividerLocation(.7);

Then you just add everything to the frame:

panel.add(splitPane);
frame.add(new JScrollPane(panel), BorderLayout.CENTER);

Now the divider location will remain at 70% and the text panes will grow/shrink as the frame is resized.

这篇关于JScrollPane 固定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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