如何拥有Scrollable JTextPane?

时间:2011-11-18 12:31:55

标签: java swing scroll jtextpane

我想有一个带滚动条的JTextPane,我该怎么办?谢谢。

5 个答案:

答案 0 :(得分:17)

要在新的JTextPane上插入滚动条,只需使用JScrollPane:

JTextPane txt = new JTextPane();

JScrollPane jsp = new JScrollPane(txt);

JTextPane API:http://download.oracle.com/javase/6/docs/api/javax/swing/JTextPane.html

JScrollPane API:http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html

如果您遇到一些问题,请查看以下问题: Java JTextPane JScrollPane Display Issue

或者看看:http://www.daniweb.com/software-development/java/threads/30283

答案 1 :(得分:5)

将其包装到JScrollPane中。阅读swing tutorial about scroll panes

答案 2 :(得分:2)

只需将JTextPane放入JScrollPane。

public class SomeFrame
{
  public static void main( String[] args )
  {
    JFrame frame = new JFrame( );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    JTextPane tp = new JTextPane();
    JScrollPane sp = new JScrollPane(tp);
    frame.getContentPane().add( sp );

    frame.pack( );
    frame.setVisible( true );
  }
}

答案 3 :(得分:1)

以下是将滚动条添加到TextBox

的代码
    JEditorPane edtDTWinfo = new JEditorPane();
    edtDTWinfo.setEditable(false);
    edtDTWinfo.setBorder(new LineBorder(Color.ORANGE, 2));
    edtDTWinfo.setForeground(Color.BLUE);
    JScrollPane spEditor = new JScrollPane(edtDTWinfo,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    spEditor.setBounds(0, 0, 200, 300);

将组件“spEditor”添加到JPanel

答案 4 :(得分:0)

在此之前,只需在设计中将ScrollPane添加到ContentPane,并将EditopPane作为子项添加到ScrollPane

JScrollPane sp = (JScrollPane)contentPane.getComponent(23);//this is in my hierarchy 23
JViewport vp = sp.getViewport();
JEditorPane ep = (JEditorPane)vp.getView();