2013-01-16 1 views
1

JTextPane으로 작업하고 있으며 JTextField와 같은 줄 바꿈 대신 텍스트를 옆으로 이동하려고합니다. JTextPane의 API를 검색하고 살펴 보았지만 유용한 것은 없습니다. 어떤 사람이 나에게 도움이 될 수있는 몇 가지 유형의 메소드 또는 프로세스 (기본 클래스에서 사용될 수 있음)를 보여줄 수 있습니까? 감사합니다!JTextPane의 텍스트를 새 줄 대신 옆으로 이동

P. JScrollPane을 사용하는 것에 대해 알고 있습니다 만 JTextPane을 가능한 한 많이 보이게하고 싶습니다.

+0

없이 'JTextField'를 원한다면 하나를 사용하십시오. 'JTextPane'을 원한다면 아마도'JScrollPane'을 사용해야 할 것입니다. 아마도 단어 감싸기가 없기 때문입니다. 그것에 대한 자세한 정보 [here] (http://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/). – 11684

+0

창에 이미지와 텍스트를 삽입 할 수 있기 때문에 JTextPane을 사용하고 있습니다. – user1978786

답변

2
JTextPane textPane = new JTextPane(); 
JPanel noWrapPanel = new JPanel(new BorderLayout()); 
noWrapPanel.add(textPane); 
JScrollPane scrollPane = new JScrollPane(noWrapPanel); 

Solution Source

플러스 :

JScrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER); 
JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER); 

편집 :

솔루션 JScrollPane

import java.awt.Component; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextPane; 
import javax.swing.plaf.ComponentUI; 
import javax.swing.text.StyledDocument; 

public class NonWrappingTextPane extends JTextPane { 

    public NonWrappingTextPane() { 
     super(); 
    } 

    public NonWrappingTextPane(StyledDocument doc) { 
     super(doc); 
    } 

    // Override getScrollableTracksViewportWidth 
    // to preserve the full width of the text 
    @Override 
    public boolean getScrollableTracksViewportWidth() { 
     Component parent = getParent(); 
     ComponentUI ui = this.getUI(); 

     return parent != null ? (ui.getPreferredSize(this).width <= parent.getSize().width) : true; 
    } 

    // Test method 
    public static void main(String[] args) { 
     String content = "The plaque on the Apollo 11 Lunar Module\n" 
       + "\"Eagle\" reads:\n\n" 
       + "\"Here men from the planet Earth first\n" 
       + "set foot upon the Moon, July, 1969 AD\n" 
       + "We came in peace for all mankind.\"\n\n" 
       + "It is signed by the astronauts and the\n" 
       + "President of the United States."; 
     JFrame f = new JFrame("Non-wrapping Text Pane Example"); 
     JPanel p = new JPanel(); 

     NonWrappingTextPane nwtp = new NonWrappingTextPane(); 
     nwtp.setText(content); 

     p.add(nwtp); 
     f.getContentPane().setLayout(new GridLayout(2, 1)); 
     f.getContentPane().add(p); 

     f.setSize(300, 200); 
     f.setVisible(true); 

     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 
+0

JTextPane의 자동 동작은 무엇입니까? – 11684

+0

@ 11684 연결된 소스를 확인하십시오, 거기에 설명되어 있습니다. –

+0

나는 JScrollPane을 사용할 것입니다 ... – user1978786

관련 문제