这么解决不知道行不.
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); }}