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);
}
}
없이 'JTextField'를 원한다면 하나를 사용하십시오. 'JTextPane'을 원한다면 아마도'JScrollPane'을 사용해야 할 것입니다. 아마도 단어 감싸기가 없기 때문입니다. 그것에 대한 자세한 정보 [here] (http://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/). – 11684
창에 이미지와 텍스트를 삽입 할 수 있기 때문에 JTextPane을 사용하고 있습니다. – user1978786