向SourceView增加垂直行号标注

本文介绍了一个使用Eclipse SWT构建的简单文本编辑器示例。该编辑器集成了行号显示、自动换行、自定义背景及前景颜色等功能,并通过SourceViewer组件增强了编辑体验。

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

dd

package test;

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.source.*;
import org.eclipse.jface.window.*;

public class TextSourceViewerClass extends ApplicationWindow {

    public TextSourceViewerClass() {
        super(null);
    }

    public void run() {
        setBlockOnOpen(true);
        open();
        Display.getCurrent().dispose();
    }

    protected Control createContents(Composite parent) {
        getShell().setText("TextViewer实例");
        getShell().setSize(400, 200);
        Display display = getShell().getDisplay();
        Composite compoiste = new Composite(parent, SWT.NONE);
        compoiste.setLayout(new FillLayout());
        SashForm sashForm = new SashForm(compoiste, SWT.HORIZONTAL);
        Document doc = new Document();
        // 设置垂直行号标注
        CompositeRuler ruler = new CompositeRuler();

        LineNumberRulerColumn lineCol = new LineNumberRulerColumn();
        lineCol.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
        ruler.addDecorator(0, lineCol);

        SourceViewer sourceViewer = new SourceViewer(sashForm, ruler, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);

        sourceViewer.setDocument(doc);
        TextViewer text = new TextViewer(sashForm, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
        text.setDocument(doc);
        final StyledText styledText = text.getTextWidget();

        // 设置自动换行
        styledText.setWordWrap(true);

        // 设置背景颜色为黑色
        text.getTextWidget().setBackground(display.getSystemColor(SWT.COLOR_BLACK));
        // 设置前景颜色为白色
        text.getTextWidget().setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        // TextViewer与SourceViewer所占窗口的比例为1:1
        sashForm.setWeights(new int[] { 1, 1 });

        return compoiste;
    }

    public static void main(String[] args) {
        new TextSourceViewerClass().run();

    }
}

 

http://www.iteye.com/problems/13646

 

转载于:https://my.oschina.net/uniquejava/blog/208603

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值