📄 simpleeditor3.java
字号:
package cn.com.chengang.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.Combo;
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 SimpleEditor3 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
// ---------创建窗口中的其他界面组件-------------
shell.setLayout(new FillLayout());
// 用ViewForm做底层容器,并用充满型布局
final ViewForm viewForm = new ViewForm(shell, SWT.NONE);
viewForm.setLayout(new FillLayout());
// 在ViewForm容器里创建文本框
final Text text = new Text(viewForm, SWT.BORDER | SWT.V_SCROLL);
viewForm.setContent(text);// 设置文本框为ViewForm的主组件
// 在ViewForm容器里创建CoolBar,并接着在CoolBar里添加两条子工具栏
CoolBar coolBar = new CoolBar(viewForm, SWT.NONE);
{ // 创建工具栏1,并添加两个按钮
ToolBar toolBar = new ToolBar(coolBar, SWT.NONE);
ToolItem getItem = new ToolItem(toolBar, SWT.PUSH);
getItem.setText("取得");
getItem.setImage(new Image(display, "icons/selectall.gif"));
ToolItem clearItem = new ToolItem(toolBar, SWT.PUSH);
clearItem.setText("清除");
clearItem.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 size = toolBar.getSize();
coolItem.setSize(size);// 设定coolItem尺寸
coolItem.setMinimumSize(size);// 设定coolItem的最小尺寸,否则按钮会被其他子工具栏遮住
}
{// 创建工具栏2
Combo combo = new Combo(coolBar, SWT.BORDER);
combo.add("http://www.chengang.com.cn");
combo.select(0);
CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
coolItem.setControl(combo);
combo.pack();
Point size = combo.getSize();
coolItem.setSize(size);
coolItem.setMinimumSize(size);
}
viewForm.setTopLeft(coolBar);
// 监听coolBar中子工具栏的改变,如有改变则重排viewForm中组件的位置
coolBar.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
viewForm.layout();
}
});
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -