JTextArea范例

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TextArea1 implements ActionListener
{
	JTextArea text;
	public static void main(String [] args)
	{
		TextArea1 gui = new TextArea1();
		gui.go();
	}
	public void go()
	{
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel =new JPanel();
		JButton button = new JButton("Just Click It");
		button.addActionListener(this);
		text=new JTextArea(10,20);//10代表10行高,20代表20字宽
		text.setLineWrap(true);//启动自动换行
		JScrollPane scroller = new JScrollPane(text);//把text赋值给新建立的JScrollPane
		//指定只能使用垂直滚动条
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		panel.add(scroller);//这很重要,加入的是带有文本域的滚动条而不是文本域
		frame.getContentPane().add(BorderLayout.CENTER,panel);
		frame.getContentPane().add(BorderLayout.SOUTH,button);
		frame.setSize(350,300);
		frame.setVisible(true);
	}
	public void actionPerformed(ActionEvent event)
	{
		/*text.append("button clicked \n");//在按下按钮是插入一行换行字符,不然的话会粘在一起
		try{
			Thread.sleep(1000);
		}catch(Exception e){}
		text.setText("Not all who are lost are wandering");*/
		//text.selectAll();//选取内容
		text.requestFocus();//把GUI目前焦点拉回到文本字段以便让用户进行输入操作
		
	}
}

你可能感兴趣的:(Head,first,java)