文本框类Text——自动换行,拥有滚动条

本文介绍了一个使用Eclipse SWT实现的简单文本编辑器示例。该示例包括文本框的基本操作,如全选、复制、粘贴、删除及清空等功能,并通过按钮触发这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
public class B{
	public static void main(String[] args){
		//文本框(可以输入文字的文本框控件)
		Display display=new Display();
		Shell shell=new Shell(display);
		shell.setText("i am a shell");
		shell.setSize(400,400);
		shell.setLayout(new FillLayout(SWT.VERTICAL));//必须要有
		//shell.setLayout(new FillLayout(SWT.HORIZONTAL));//必须要有

		//(1)新建一个文本框,要想监听文本框是否被选中,需要将文本框的属性设置为final
		final Text content=new Text(shell,SWT.WRAP|SWT.V_SCROLL);
		content.setSize(400,100);
		
		//全选按钮
		Button selectAll=new Button(shell,SWT.PUSH);
		selectAll.setText("全选");
		selectAll.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				content.selectAll();
			}
		});
		
		//取消选择按钮
		Button cancel=new Button(shell,SWT.PUSH);
		cancel.setText("取消选择");
		cancel.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				if(content.getSelectionCount()>0){
					//有选中的内容
					content.clearSelection();
				}
			}
		});
		
		//复制按钮
		Button copy=new Button(shell,SWT.PUSH);
		copy.setText("复制选中");
		copy.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				if(content.getSelectionCount()>0){
					//如果有选中的文本,则复制到剪贴板
					content.copy();
				}
			}
		});
		
		//粘贴按钮
		Button paste=new Button(shell,SWT.PUSH);
		paste.setText("粘贴");
		paste.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				content.paste();
			}
		});
		
		//删除选中的文字
		Button delete=new Button(shell,SWT.PUSH);
		delete.setText("删除选中的文字(剪贴)");
		delete.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				content.cut();
			}
		});
		
		//清空文本框的内容
		Button clear=new Button(shell,SWT.PUSH);
		clear.setText("清空文本框");
		clear.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				content.setText("");
			}
		});
		
		//打开窗口,进行窗口的显示
		shell.open();
		while(!shell.isDisposed()){
			//当窗口没有被释放的时候
			if(!display.readAndDispatch()){
				display.sleep();
			}		
		}
		display.dispose();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值