【问题标题】:JScrollPane with fixed width固定宽度的 JScrollPane
【发布时间】:2016-03-22 19:23:47
【问题描述】:

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

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

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

我的初始化代码:

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) 对 SOUTH 没有限制,所以可以这样长大。

问题:

  • JTextPane 仅在向其中插入一些文本后才会调整大小。
  • 垂直滚动条不起作用。

【问题讨论】:

  • 也许这会有所帮助。我刚刚对其进行了测试,提供的解决方案也适用于 JTextPane。你只需要稍微调整一下stackoverflow.com/questions/11178994/…
  • 对不起,这个例子是每个滚动条的。我的错。
  • 是的,但感谢您的评论。

标签: java swing


【解决方案1】:

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

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

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

实现此功能的一种简单方法是使用Scrollable Panel。该类实现了 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%,文本窗格会随着框架大小的调整而增大/缩小。

【讨论】:

  • 要使面板垂直滚动也设置panel.setScrollableHeight( ScrollablePanel.ScrollableSizeHint.STRETCH);
猜你喜欢
  • 1970-01-01
  • 2018-04-23
  • 2011-10-29
  • 2012-02-21
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
相关资源
最近更新 更多