⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleeditor1.java

📁 Eclipse从入门到精通源代码/第二篇 SWT_JFace篇(6-16章)
💻 JAVA
字号:
/**
 * @作者:陈刚
 * @Email:glchengang@yeah.net
 * @Blog:http://blog.csdn.net/glchengang
 */
package swt;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class SimpleEditor1 {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(327, 253);
        shell.setText("SWT Application");
        //------------------新插入的界面核心代码------------------------

        shell.setLayout(new FillLayout());
        //用ViewForm做底层容器,并用充满型布局
        ViewForm viewForm = new ViewForm(shell, SWT.NONE);
        viewForm.setLayout(new FillLayout());
        //在ViewForm上建立工具栏和文本框
        ToolBar toolBar = new ToolBar(viewForm, SWT.NONE);
        final Text text = new Text(viewForm, SWT.BORDER | SWT.V_SCROLL);
        //设置工具栏和文本框在ViewForm上的位置
        viewForm.setContent(text);
        viewForm.setTopLeft(toolBar);
        /*
         * 为工具栏添加按钮及相应事件
         */
        ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
        toolItem.setText("取得");
        //按钮图片
        toolItem.setImage(new Image(display, "icons/selectAll.gif"));
        toolItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                String str = text.getText(); //取得文本框的文字
                MessageDialog.openInformation(null, null, str);
            }
        });

        ToolItem toolItem2 = new ToolItem(toolBar, SWT.PUSH);
        toolItem2.setText("清除");
        toolItem2.setImage(new Image(display, "icons/remove.gif"));
        toolItem2.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                text.setText("");//清除文本框中的文字
            }
        });

        //------------------END---------------------------------------------
        shell.layout();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -