📄 othercontrolsample.java
字号:
package rcpbook.swtjface.sample;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class OtherControlSample {
static Image author;
static Image about;
static Image dataBak;
static Image dataConfig;
static Image logoff;
static Image user;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(490, 330);
shell.setText("其他常用组件示例");
GridLayout layout = new GridLayout(3, false);
shell.setLayout(layout);
// 创建菜单
createMenus(shell);
// 创建工具栏
createToolBar(shell);
// CLabel
CLabel cl = new CLabel(shell, SWT.CENTER);
GridData gridData = new GridData();
gridData.horizontalSpan = 3;
cl.setLayoutData(gridData);
cl.setText("作者:独孤求败(CLabel标签示例)");
author = new Image(display, OtherControlSample.class
.getResourceAsStream("images/author.gif"));
cl.setImage(author);
// CCombo
String[] city = { "湖北武汉", "湖南长沙", "江西南昌", "江苏南京", "浙江杭州", "河南郑州",
"广东广州", "广西南宁" };
new CCombo(shell, SWT.FLAT | SWT.READ_ONLY).setItems(city);
new CCombo(shell, SWT.BORDER).setItems(city);
// 滚动条
Slider slider = new Slider(shell, SWT.HORIZONTAL);
slider.setMinimum(0);
slider.setMaximum(100);
slider.setIncrement(2);
slider.setSelection(50);
// 分组框
Group group1 = new Group(shell, SWT.SHADOW_IN);
group1.setText("个人爱好");
group1.setLayout(new RowLayout(SWT.VERTICAL));
new Button(group1, SWT.CHECK).setText("足球");
new Button(group1, SWT.CHECK).setText("电影");
new Button(group1, SWT.CHECK).setText("艺术");
new Button(group1, SWT.CHECK).setText("电脑");
// 进度条
final ProgressBar progressbar = new ProgressBar(shell, SWT.HORIZONTAL
| SWT.SMOOTH);
GridData gd = new GridData();
gd.horizontalSpan = 2;
progressbar.setLayoutData(gd);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
new Thread() {
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(150);
} catch (InterruptedException e) {
}
display.asyncExec(new Runnable() {
public void run() {
progressbar
.setSelection(progressbar.getSelection() + 1);
}
});
}
}
}.start();
// 选项卡TabFolder
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
tabItem1.setText("数据查看");
Label label1 = new Label(tabFolder, SWT.NONE);
label1.setText("基础数据");
tabItem1.setControl(label1);
TabItem tabItem2 = new TabItem(tabFolder, SWT.NONE);
tabItem2.setText("文档材料");
Label label2 = new Label(tabFolder, SWT.NONE);
label2.setText("打开文档");
tabItem2.setControl(label2);
// 定制选项卡CTabFolder
CTabFolder ctabFolder = new CTabFolder(shell, SWT.TOP);
ctabFolder.setBorderVisible(true);
ctabFolder.setSimple(false);
final CTabItem ctabItem1 = new CTabItem(ctabFolder, SWT.NONE);
ctabItem1.setText("修改用户密码");
final CTabItem ctabItem2 = new CTabItem(ctabFolder, SWT.NONE);
ctabItem2.setText("增加新用户");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
if (author != null)
author.dispose();
if (about != null)
about.dispose();
if (dataBak != null)
dataBak.dispose();
if (dataConfig != null)
dataConfig.dispose();
if (logoff != null)
logoff.dispose();
if (user != null)
user.dispose();
display.dispose();
}
// 创建菜单的具体方法
private static void createMenus(Shell shell) {
Menu menu = new Menu(shell, SWT.BAR);
MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);
fileItem.setText("文件(&F)");
MenuItem editItem = new MenuItem(menu, SWT.CASCADE);
editItem.setText("编辑(&E)");
MenuItem sourceItem = new MenuItem(menu, SWT.CASCADE);
sourceItem.setText("源代码(&S)");
MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
helpItem.setText("帮助(&H)");
Menu helpMenu = new Menu(menu);
helpItem.setMenu(helpMenu);
MenuItem welcome = new MenuItem(helpMenu, SWT.NONE);
welcome.setText("欢迎使用");
new MenuItem(helpMenu, SWT.SEPARATOR);
MenuItem intro = new MenuItem(helpMenu, SWT.NONE);
intro.setText("系统说明");
MenuItem about = new MenuItem(helpMenu, SWT.NONE);
about.setText("关于系统");
shell.setMenuBar(menu);
}
// 创建工具栏
private static void createToolBar(Shell shell) {
Display display = shell.getDisplay();
about = new Image(display, OtherControlSample.class
.getResourceAsStream("images/about.gif"));
dataBak = new Image(display, OtherControlSample.class
.getResourceAsStream("images/dataBak.gif"));
dataConfig = new Image(display, OtherControlSample.class
.getResourceAsStream("images/dataConfig.gif"));
logoff = new Image(display, OtherControlSample.class
.getResourceAsStream("images/logoff.gif"));
user = new Image(display, OtherControlSample.class
.getResourceAsStream("images/user.gif"));
ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.HORIZONTAL);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setToolTipText("数据备份");
item.setImage(dataBak);
item = new ToolItem(toolBar, SWT.PUSH);
item.setToolTipText("数据源配置");
item.setImage(dataConfig);
new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setToolTipText("用户注销");
item.setImage(logoff);
item = new ToolItem(toolBar, SWT.PUSH);
item.setToolTipText("用户管理");
item.setImage(user);
new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setToolTipText("关于系统");
item.setImage(about);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -