【发布时间】: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/…
-
对不起,这个例子是每个滚动条的。我的错。
-
是的,但感谢您的评论。