java 怎么在JtextArea里设置滚动条
这个要借助JScrollPane 定义是要这样的: JPanel p=new JPanel(); JTextArea text = new JTextArea(); JScrollPane text2=new JScrollPane(text); 然后把text2加入面板中就ok了 p.add(text2);
方法一:
import java.awt.*;
import javax.swing.*;
import java.awt.Dimension;
public class JScrollPaneTest extends JFrame{
public JScrollPaneTest()
{
this.setLayout(null);
JPanel jp=new JPanel();
jp.setPreferredSize(new Dimension(200,100));
JScrollPane sp=new JScrollPane(jp);
this.setBounds(100,100,300,200);
sp.setBounds(10,10,175,70);
this.getContentPane().add(sp);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JScrollPaneTest();
}
}
运行的结果:
方法二:
import java.awt.*;
import javax.swing.*;
import java.awt.Dimension;
public class JScrollPaneTest extends JFrame{
public JScrollPaneTest()
{
Container c=getContentPane();
JPanel jp=new JPanel();
JTextArea ta=new JTextArea(20,50);
ScrollPane sp=new ScrollPane();
c.add(jp);
jp.add(sp);
sp.add(ta);
sp.setSize(300,180);
setBounds(100,100,300,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new JScrollPaneTest();
}
}
运行的结果: