Java JTextPane-在不使用JScrollPane的情况下滚动可见文本
因此,在我的应用程序中,我使用了一个自定义组件,它是JTextPane的扩展。我需要扩展这个样式。我已经成功地关闭了单词包装,因为这个组件打算充当JTextField,即不是多行的 然而,我面临的问题是,如果文本跨越可见区域之外,JTextPane在JScrollpane内部不使用时,似乎不能很好地处理非单词包装的文本。使用JTextField时,无法将可见区域外的文本移动到 我不想使用JScrollpane,因为我有很多采用JTextComponent的遗留代码 所以我的问题是,有没有一种方法可以使可见区域与插入符号位置保持一致,这样当键入内容到达可见边缘或试图选择跨越可见区域之外的文本时,该文本就会像JTextField一样移动到视图中 下面是我的问题的一个工作示例。它在JTextPane上方显示一个JTextField。如果在文本字段中键入,您将看到可见区域随文本移动。如果在文本窗格中键入,则不会发生这种情况 另外,我用Java7做这件事 提前谢谢Java JTextPane-在不使用JScrollPane的情况下滚动可见文本,java,swing,jtextpane,Java,Swing,Jtextpane,因此,在我的应用程序中,我使用了一个自定义组件,它是JTextPane的扩展。我需要扩展这个样式。我已经成功地关闭了单词包装,因为这个组件打算充当JTextField,即不是多行的 然而,我面临的问题是,如果文本跨越可见区域之外,JTextPane在JScrollpane内部不使用时,似乎不能很好地处理非单词包装的文本。使用JTextField时,无法将可见区域外的文本移动到 我不想使用JScrollpane,因为我有很多采用JTextComponent的遗留代码 所以我的问题是,有没有一种方法
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class NoAutoScroll {
public NoAutoScroll() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField();
textField.setText("This text field can show the text outside the visible area.");
textField.setPreferredSize(new Dimension(150, 30));
JTextPane textPane = new JTextPane();
textPane.setEditorKit(new MyEditorKit());
textPane.setText("This text pane cannot show the text outside the visible area.");
textPane.setPreferredSize(new Dimension(150, 30));
textPane.setBorder(textField.getBorder());
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
mainPanel.add(new JLabel("JTextField"), constraints);
constraints.gridx = 1;
constraints.gridy = 0;
mainPanel.add(textField, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
mainPanel.add(new JLabel("JTextPane"), constraints);
constraints.gridx = 1;
constraints.gridy = 1;
mainPanel.add(textPane, constraints);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public class MyViewFactory implements ViewFactory {
private final class NonBreakingLabelView extends LabelView {
private NonBreakingLabelView(Element elem) {
super(elem);
}
@Override
public int getBreakWeight(int axis, float pos, float len){
return BadBreakWeight;
}
}
@Override
public View create(final Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new NonBreakingLabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
@SuppressWarnings("serial")
public class MyEditorKit extends StyledEditorKit {
@Override
public ViewFactory getViewFactory() {
return new MyViewFactory();
}
}
public static void main(String[] args) {
new NoAutoScroll();
}
}
正道
- 如果没有
,则无法隐藏这两个JScrollPane
jscrollbar
- 按住两个
s,第二个和可见的将仅是从添加到Document
的鼠标事件生成的JTextPane
,但是没有理由使用scrollIncremet
,而是使用JTextPane
JLabel