textarea = new JTextArea();
textarea.setBounds(20, 60, 555, 370);
JScrollPane scroller = new JScrollPane(textarea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textarea.setLineWrap(true);//启动自动换行
this.setLayout(null);
this.add(scroller);这样不仅不显示滚动条,而且textarea也不显示
请问改怎么做
网上有很多关于这方面的资料,希望楼主的技术能够更进一层楼。
http://topic.csdn.net/u/20080901/20/90755567-8072-4c02-b5c9-3034152b7bf5.html
============================
改成:
scroller.setBounds(20, 60, 555, 370);
对象弄错了
//www.xinshuguang.org
import java.awt.*;
import javax.swing.*;
public class xsgWenBenKuang {
private JTextArea WenBenKuang;
public void xsgAdd(JFrame frame) {
frame.setTitle("文本框");
frame.setLayout(null);
//关掉布局管理器
//以绝对像素定位
WenBenKuang = new JTextArea
("自动换行的文本域",4,5);
WenBenKuang.setLineWrap(true);
//到达文本框边界自动换行
JScrollPane WenBenKuangScrollPane = new JScrollPane(WenBenKuang);
Dimension WenBenKuangSize = WenBenKuang.getPreferredSize();
WenBenKuangScrollPane.setBounds(0, 0, WenBenKuangSize.width,
WenBenKuangSize.height);
//虽然是滚动条,但它决定了文本框的位置
frame.add(WenBenKuangScrollPane);
//注意添上的是滚动条而不是文本框
}
public static void main(String[] args) {
JFrame frame = new JFrame("新曙光培训@西安");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
xsgWenBenKuang Demo = new xsgWenBenKuang();
Demo.xsgAdd(frame);
frame.setVisible(true);
}
}
视频讲解