我无法显示水平滚动条。我已经尝试过使用JTextPane.setSize(),JTextPane.setPreferredSize()和JTextPane没有大小的方法。我也在使用JScrollPane.setPreferredSize()。
出现垂直滚动条,但水平滚动条不显示。
以下是一个例子:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
private int textPaneWidth = 500;
private int textPaneHeigth = 200;
private int scrollPaneWidth = 100;
private int scrollPaneHeigth = 100;
private JTextPane textPane;
private JButton button;
private JFrame frame;
private JScrollPane scrollPane;
public static void main(String[] args) {
Test gui = new Test();
gui.go();
}
private void go() {
frame = new JFrame("Test");
button = new JButton("button");
textPane = new JTextPane();
scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ButtonListener());
textPane.setFont(new Font("Courier", Font.PLAIN, 12));
// Sizes:
// textPane.setSize(textPaneWidth, textPaneHeigth); // ???
// textPane.setPreferredSize(new Dimension(textPaneWidth, textPaneHeigth)); // ???
scrollPane.setPreferredSize(new Dimension(scrollPaneWidth, scrollPaneHeigth));
frame.add(button);
frame.add(scrollPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
textPane.setText("==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n" +
"==================================================================== \n");
}
}
}
按下该按钮时,该字符串将添加到JTextPane。有垂直滚动,但没有水平滚动。
这是按下按钮后我看到的:
我做错了什么?
答案 0 :(得分:1)
似乎需要扩展JTextPane以避免包装
https://forums.oracle.com/thread/1210688
class JTextWrapPane extends JTextPane {
boolean wrapState = true;
JTextArea j = new JTextArea();
JTextWrapPane() {
super();
}
public JTextWrapPane(StyledDocument p_oSdLog) {
super(p_oSdLog);
}
public boolean getScrollableTracksViewportWidth() {
return wrapState;
}
public void setLineWrap(boolean wrap) {
wrapState = wrap;
}
public boolean getLineWrap(boolean wrap) {
return wrapState;
}
}
编辑:
// instead of private JTextPane jtp = new JTextPane( sdLog );
private JTextWrapPane jtp = new JTextWrapPane( sdLog );
//and set LineWrap = (false):
jtp.setLineWrap(false);
答案 1 :(得分:1)
经过一番挖掘后,我发现了问题。
JTextPane
不只显示文档,而是显示样式文档。引用java tutorials:“一个JTextComponent子类JTextPane,要求其文档是StyledDocument而不仅仅是Document。”
当您使用默认构造函数创建JTextPane
时,就像您一样,自动创建DefaultStyledDocument
。来自documentation for DefaultStyledDocument
:
这些样式运行被映射到段落元素结构(可能存在于某些其他结构中)。由于逻辑样式被分配给段落边界,因此样式在段落边界处断开。
这意味着JTextPane
实际上正在为你做。一种解决方案是尝试使用JTextArea
代替。
编辑,每条评论:
另一个解决方案可能是创建自己的StyledDocument
(可能从AbstractDocument
扩展),按照您想要的方式设置它,并将其传递给JTextPane
构造函数。
答案 2 :(得分:1)
只有没有滚动条的垂直滚动
progressTextArea = new JTextPane();
JScrollPane scroller = new JScrollPane()
{
public Dimension getPreferredSize()
{
return new Dimension(300, 100);
}
public float getAlignmentX()
{
return LEFT_ALIGNMENT;
}
};
scroller.getViewport().add(progressTextArea);