2011-09-19 2 views
2

JTextPane을 스크롤하려면 어떻게해야합니까? 이 예제에서는 JScrollPane이 사용되었지만 코드를 실행하면 스크롤 막대가 표시 될 수는 있지만 작동하지 않습니다. 내가스크롤 할 JTextPane 가져 오기

f.setVisible(true); 

후 조각

for (int i = 0; i < 100; i++) { 
     thing.append("nouns verbs adjectives \n"); 
} 

을 배치하면

import java.awt.BorderLayout; 
import javax.swing.*; 
import javax.swing.text.*; 

public class JTextPaneTester 
{ 
    JTextPane jtp = new JTextPane(); 
    StyledDocument doc; 
    Style style; 
    JScrollPane jsp = new JScrollPane(jtp); 

    JTextPaneTester() 
    { 
     doc = (StyledDocument)jtp.getDocument(); 
     style = doc.addStyle("fancy", null); 
     jsp.setVerticalScrollBarPolicy(
       ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    } 

    void append(String s) 
    { 
     try 
     { 
      doc.insertString(doc.getLength(), s, style); 
     } 
     catch (BadLocationException e) { assert false: "problem"; } 
    } 

    public static void main(String[] args) 
    { 
     JTextPaneTester thing = new JTextPaneTester(); 

     for (int i = 0; i < 100; i++) 
      thing.append("nouns verbs adjectives \n"); 

     JFrame f = new JFrame(); 
     JPanel center = new JPanel(); 
     f.add(center, BorderLayout.CENTER); 

     center.add(thing.jsp); 
     f.setSize(400, 400); 
     f.setVisible(true); 
    } 
} 

답변

1

작동하는 것 같다.

관련 문제