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

📄 simpleeditor2.java

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
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 SimpleEditor2 {
    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(500, 375);
        shell.setText("SWT Application");
        //-----------------------------
        shell.setLayout(new FillLayout());
        //用ViewForm做底层容器,并设为充满型布局
        final ViewForm viewForm = new ViewForm(shell, SWT.NONE);
        viewForm.setLayout(new FillLayout());
        //在ViewForm上建立CoolBar和文本框
        CoolBar coolBar = new CoolBar(viewForm, SWT.NONE);
        Text text = new Text(viewForm, SWT.BORDER | SWT.V_SCROLL);
        //设置CoolBar和文本框在ViewForm上的位置
        viewForm.setContent(text);
        viewForm.setTopLeft(coolBar);
        /*
         * 为CoolBar添加两条子工具栏
         */
        {
            ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
            /*
             * 为子工具栏1添加两按钮
             */
            ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setText("取得");
            toolItem.setImage(new Image(display, "icons/selectAll.gif"));
            ToolItem toolItem2 = new ToolItem(toolBar, SWT.PUSH);
            toolItem2.setText("清除");
            toolItem2.setImage(new Image(display, "icons/remove.gif"));
            /*
             * 创建一个coolItem来控制此子工具栏
             */
            CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
            coolItem.setControl(toolBar);
            /*
             * 调整toolBar为合适的尺寸。并用toolBar的尺寸来调整coolItem
             */
            toolBar.pack();//调整toolBar尺寸
            Point point = new Point(toolBar.getSize().x, toolBar.getSize().y);
            coolItem.setSize(point);//设定coolItem尺寸
            //设定coolItem最小尺寸。如不设,则按钮会被其他子工具栏遮住
            coolItem.setMinimumSize(point);
        }
        {//以同样方式再创建一个子具栏2
            ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
            CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
            coolItem.setControl(toolBar);

            ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
            toolItem.setImage(new Image(display, "icons/next.gif"));

            toolBar.pack();
            Point point = new Point(toolBar.getSize().x, toolBar.getSize().y);
            coolItem.setSize(point);
            coolItem.setMinimumSize(point);
        }
        //-----------------------------------------
        shell.layout();
        /*
         * (1)监听coolBar中子工具栏的改变,如有改变则重排viewForm中组件的位置。
         * 不加这个监听器,则当子工具栏由一行拖拉变成二行时,文本框将被遮住一部份。
         * (2)如果此监听器写在shell.layout()前面,则窗口打开时,此事件会首先
         * 触发一次,这完全没有必要。由此可见,在Eclipse中监听器的代码位置常常是 优化程序执行效率时要考虑和检查的地方。
         */
        coolBar.addControlListener(new ControlAdapter() {
            public void controlResized(ControlEvent e) {
                viewForm.layout();
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
}

⌨️ 快捷键说明

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