JTextPane的问题

synclock 2003-09-13 10:02:54
怎么设置不要自动换行,就像jtextarea中的setTextWrap(好像是这个)那样

谢谢
...全文
47 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
YuLimin 2003-09-13
  • 打赏
  • 举报
回复
应当可以吧,不错;)
liad 2003-09-13
  • 打赏
  • 举报
回复
可以参考http://forum.java.sun.com/thread.jsp?forum=57&thread=326017
Keepers 2003-09-13
  • 打赏
  • 举报
回复
以上两个是sun网站的解决方案
Keepers 2003-09-13
  • 打赏
  • 举报
回复
这么解决不知道行不.
import java.awt.*;import javax.swing.*;import javax.swing.text.*;public class TestNoWrap extends JFrame{ JTextPane textPane; JScrollPane scrollPane; public TestNoWrap() { JPanel panel = new JPanel(); panel.setLayout( new BorderLayout() ); setContentPane( panel ); // no wrap by adding text pane to a panel using border layout textPane = new JTextPane(); textPane.setText("1234567890 1234567890 1234567890"); JPanel noWrapPanel = new JPanel(); noWrapPanel.setLayout( new BorderLayout() ); noWrapPanel.add( textPane ); scrollPane = new JScrollPane( noWrapPanel ); scrollPane.setPreferredSize( new Dimension( 200, 100 ) ); panel.add( scrollPane, BorderLayout.NORTH ); // no wrap by overriding text pane methods textPane = new JTextPane() { public void setSize(Dimension d) { if (d.width < getParent().getSize().width) d.width = getParent().getSize().width; super.setSize(d); } public boolean getScrollableTracksViewportWidth() { return false; } }; textPane.setText("1234567890 1234567890 1234567890"); scrollPane = new JScrollPane( textPane ); scrollPane.setPreferredSize( new Dimension( 200, 100 ) ); panel.add( scrollPane ); } public static void main(String[] args) { TestNoWrap frame = new TestNoWrap(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); }}



办法2
import java.awt.*;import javax.swing.*;import javax.swing.text.*;public class TextPaneNoWrap extends JFrame{ JTextPane textPane; JScrollPane scrollPane; public TextPaneNoWrap() { JPanel panel = new JPanel(); panel.setLayout( new BorderLayout() ); setContentPane( panel ); // no wrap by adding text pane to a panel using border layout textPane = new JTextPane(); textPane.replaceSelection(" spaces: no wrap - 0 1 2 3 4 5 6 7 8 9"); textPane.replaceSelection("\t\ttabs : wrap - 0 1 2 3 4 5 6 7 8 9\n"); JPanel noWrapPanel = new JPanel(); noWrapPanel.setLayout( new BorderLayout() ); noWrapPanel.add( textPane ); scrollPane = new JScrollPane( noWrapPanel ); scrollPane.setPreferredSize( new Dimension( 200, 100 ) ); panel.add( scrollPane, BorderLayout.NORTH ); // no wrap by overriding text pane methods textPane = new JTextPane() { public void setSize(Dimension d) { if (d.width < getParent().getSize().width) d.width = getParent().getSize().width; super.setSize(d); } public boolean getScrollableTracksViewportWidth() { return false; } }; textPane.replaceSelection("\t\ttabs : wrap - 0 1 2 3 4 5 6 7 8 9"); textPane.replaceSelection(" spaces: no wrap - 0 1 2 3 4 5 6 7 8 9\n"); scrollPane = new JScrollPane( textPane ); scrollPane.setPreferredSize( new Dimension( 200, 100 ) ); panel.add( scrollPane ); } public static void main(String[] args) { TextPaneNoWrap frame = new TextPaneNoWrap(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); }}
pqds 2003-09-13
  • 打赏
  • 举报
回复
guanzhu!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧