读书人

测试后JScrollPane 一部分用法

发布时间: 2013-01-04 10:04:18 作者: rapoo

测试后,JScrollPane 部分用法!

1、在使用了JScrollPane的JTextArea中,当指定JTextArea大小后,如何使当一行的数据过长时,自动折行,而不是出现横滚动条;
2、在使用了JScrollPane的JTable中,如何指定JTable的宽度,当大于JScrollPane的显示区域时,自动显示横滚动条,而不是压缩JTable中的每一列的宽度。
可以参照java 图形核心卷2的介绍http://ecapital.myetang.com/swing/tutorial/s02/index.html
第一个问题:
mport java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextArea2{
public static void main(String[] args){
JFrame f=new JFrame("JTextArea2");
Container contentPane=f.getContentPane();
contentPane.setLayout(new BorderLayout());

JPanel p1=new JPanel();
p1.setLayout(new GridLayout(1,1));
p1.setBorder(BorderFactory.createTitledBorder("构造TextArea-使用GridLayout,加ScrollBar"));

JTextArea t1=new JTextArea(5,25);
t1.setTabSize(10);
t1.setFont(new Font("标楷体",Font.BOLD,16));
t1.setLineWrap(true);//激活自动换行功能
t1.setWrapStyleWord(true);//激活断行不断字功能

p1.add(new JScrollPane(t1));//将JTextArea放入JScrollPane中,这样就能利用滚动的效果看到输入超过JTextArea高度的
//文字.
contentPane.add(p1);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
其实在Document中你可以更灵活的控制

第二个问题,把JTable的AUTO_RESIZE_OFF
、在使用了JScrollPane的JTable中,如何指定JTable的宽度,当大于JScrollPane的显示区域时,自动显示横滚动条,而不是压缩JTable中的每一列的宽度
add the following line:
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

读书人网 >JavaScript

热点推荐