Java Swing中怎么让JTextPane不自动换行啊,我的数据大约有1500列左右。
2个回答
展开全部
需要重写JTextPane中的两个方法,
自己定义一个类,继承JTextPane 重写两个方法,就可以了
------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
public class App extends JFrame {
public App() {
setSize(650, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(14, 39, 616, 374);
getContentPane().add(scrollPane);
JTextPane textPane = new MyJTextPane();
scrollPane.setViewportView(textPane);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
class MyJTextPane extends JTextPane {
public boolean getScrollableTracksViewportWidth() {
return false;
}
public void setSize(Dimension d) {
if (d.width < getParent().getSize().width) {
d.width = getParent().getSize().width;
}
d.width += 100;
super.setSize(d);
}
}
自己定义一个类,继承JTextPane 重写两个方法,就可以了
------------------------------------------------------------------------------------------------------------------------------------------------
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
public class App extends JFrame {
public App() {
setSize(650, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(14, 39, 616, 374);
getContentPane().add(scrollPane);
JTextPane textPane = new MyJTextPane();
scrollPane.setViewportView(textPane);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
class MyJTextPane extends JTextPane {
public boolean getScrollableTracksViewportWidth() {
return false;
}
public void setSize(Dimension d) {
if (d.width < getParent().getSize().width) {
d.width = getParent().getSize().width;
}
d.width += 100;
super.setSize(d);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询