我最近需要在单个JScrollPane的视口视图中放置几个组件,其中包含一个JTextPane。 (详情请见here) 我把所有组件(两个JPanel和JTextPane)放在另一个JPanel中,这个组件有一个BorderLayout LayoutManager,并将JPanel设置为ScrollPane的视口视图。
我立刻注意到的是:
即使JScrollPane设置为HORIZONTAL_SCROLLBAR_NEVER,JTextPane也不会根据JScrollPane的宽度调整其宽度。相反,它只是最大化其宽度以使整个文本内部适合一行(除了“\ n”换行符)。
以下是两张照片的图片:
这就是它应该是这样的:
现在,我找到this question here来回答问题的第一部分。 JPanel需要被子类化,子类需要实现Scrollable。
然而,出现了另一个问题。 JPanel内部的JTextPane不再支持JPanel的BorderLayout.CENTER了。如果文本没有填满窗口中的剩余空间,则JTextPane将不再拉伸自己。
我正在寻找的是解决方案。
我希望有一种可靠的方法让JTextPane再次尊重JPanel的BorderLayout并相应地拉伸自己。我不正在寻找一些解决方法,例如将背景设置为白色,以便JTextPane看起来在填充帧时实际上不是。
下面,我发布了一些代码,您可以尝试不同的配置。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;
public class MinimalExample extends JFrame {
private JPanel contentPane;
private JPanel outerPanel;
private JPanel firstHeadlinePanel;
private JLabel lblSomeJlabel;
private JPanel secondHeadlinePanel;
private JLabel lblAnotherJlabel;
private JPanel headlinesPanel;
private JTextPane textPane;
private JScrollPane scrollPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MinimalExample frame = new MinimalExample();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MinimalExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 230, 350);
contentPane = new JPanel();
contentPane.setBorder(null);
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
outerPanel = new ScrollablePanel();
//outerPanel = new JPanel();
outerPanel.setLayout(new BorderLayout(0, 0));
headlinesPanel = new JPanel();
headlinesPanel.setBorder(new EmptyBorder(5, 0, 0, 0));
outerPanel.add(headlinesPanel, BorderLayout.NORTH);
headlinesPanel.setLayout(new BorderLayout(0, 0));
firstHeadlinePanel = new JPanel();
firstHeadlinePanel.setBorder(new EmptyBorder(0, 0, 5, 0));
headlinesPanel.add(firstHeadlinePanel, BorderLayout.NORTH);
firstHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblSomeJlabel = new JLabel("Some JLabel");
firstHeadlinePanel.add(lblSomeJlabel, BorderLayout.CENTER);
secondHeadlinePanel = new JPanel();
headlinesPanel.add(secondHeadlinePanel, BorderLayout.SOUTH);
secondHeadlinePanel.setLayout(new BorderLayout(0, 0));
lblAnotherJlabel = new JLabel("Another JLabel");
lblAnotherJlabel.setBorder(new EmptyBorder(0, 0, 5, 0));
secondHeadlinePanel.add(lblAnotherJlabel, BorderLayout.NORTH);
textPane = new JTextPane();
textPane.setText(addSomeShortText());
//textPane.setText(addSomeLongText());
outerPanel.add(textPane);
scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportView(outerPanel);
contentPane.add(scrollPane);
}
private String addSomeShortText() {
return ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est.");
}
private String addSomeLongText() {
return (" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur facilisis "
+ "dictum ullamcorper. Nulla sed diam sit amet ante pellentesque ultricies in non "
+ "est. Suspendisse sit amet leo purus. Aenean lacinia ornare quam, a porttitor "
+ "tortor vulputate quis. Quisque suscipit porttitor nisl, mollis suscipit ligula "
+ "laoreet vitae. Nulla quis faucibus dui. Donec vestibulum quam sit amet "
+ "facilisis scelerisque. Pellentesque eu lacus eget nibh elementum venenatis in "
+ "congue magna. Donec et vestibulum magna, ut dapibus sapien.\n"
+ "Donec nec enim magna. Curabitur vitae est est. Etiam non tellus nec ipsum "
+ "dapibus ultricies eget ac lacus. Aliquam eu rhoncus leo. Suspendisse sodales "
+ "nibh eros, vel placerat velit bibendum ut. Vivamus feugiat purus at viverra "
+ "volutpat. Donec tincidunt quam quam, ut mattis enim rutrum sit amet. Integer "
+ "volutpat sit amet metus vitae elementum. Aenean ullamcorper iaculis vulputate. "
+ "Etiam venenatis mollis arcu quis interdum. Quisque et pharetra justo, et "
+ "ullamcorper augue. Phasellus id pellentesque massa. Proin suscipit, mauris et "
+ "congue commodo, mi eros fermentum ante, vel ultricies nisl neque a sem. Mauris "
+ "tristique quis erat vel pulvinar. ");
}
/* Original source of this is here:
* https://stackoverflow.com/questions/15783014/jtextarea-on-jpanel-inside-jscrollpane-does-not-resize-properly */
private static class ScrollablePanel extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return super.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation,
int direction) {
return 16;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
提前感谢你们给我的任何帮助!
答案 0 :(得分:4)
好的,基本上,您需要根据getScrollableTracksViewportHeight
和您组件的高度更改JViewport
的工作方式。
您基本上希望组件跟踪视口的高度(调整自身以匹配),而视口的高度大于组件的首选高度。当组件的首选高度大于视口时,您希望停止跟踪并允许组件过度填充"滚动窗格,类似......
@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = true;
Container parent = getParent();
if (parent instanceof JViewport) {
JViewport viewport = (JViewport) parent;
if (viewport.getHeight() < getPreferredSize().height) {
track = false;
}
}
return track;
}
这基本上是&#34;&#34; JTable
和JList
之类的工作方式