JTextArea text=new JTextArea();
text.setLineWrap(true);//设置自动换行,之后则不需要设置水平滚动条
JScrollPane scroll=new JScrollPane(text);
//分别设置水平和垂直滚动条自动出现
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//分别设置水平和垂直滚动条总是出现
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//分别设置水平和垂直滚动条总是隐藏
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_NEVER);
Container container=mynote.getContentPane();
container.add(scroll,BorderLayout.EAST);
//注意:将滚动条scoll添加到窗体后就不用再添加scoll的textarea,否则将无法显示
myframe.add(scroll);
//获取TextArea最后最后一行焦点
JTextarea textarea = new JTextarea();
textarea.append("dfasfsadfasfd");
textarea.setCaretPosition(textarea.getText().length());