SWT(JFace) Menu、Bar...体验代码

演示代码:
MenuExamples.java


代码如下:

package swt_jface.demo5;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MenuExamples {

Display display = new Display();
Shell shell = new Shell(display);
public MenuExamples() {
Menu menuBar = new Menu(shell, SWT.BAR);
MenuItem itemHello = new MenuItem(menuBar, SWT.PUSH);
itemHello.setText("&Hello");
itemHello.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("HELLO");
}
});
MenuItem itemCascade = new MenuItem(menuBar, SWT.CASCADE);
itemCascade.setText("&CASCADE item");
Menu menu = new Menu(itemCascade);
MenuItem itemPush = new MenuItem(menu, SWT.PUSH);
itemPush.setText("&PUSH item\tCtrl+P");
itemPush.setAccelerator(SWT.CTRL + 'P');
Image icon = new Image(shell.getDisplay(), "C:/icons/new.gif");
itemPush.setImage(icon);
itemPush.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: PUSH item");
}
});
final MenuItem itemCheck = new MenuItem(menu, SWT.CHECK);
itemCheck.setText("CHEC&K item\tCtrl+K");
itemCheck.setAccelerator(SWT.CTRL + 'K');
itemCheck.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: CHECK item");
System.out.println("Selection: " + itemCheck.getSelection());
}
});
new MenuItem(menu, SWT.SEPARATOR);
final MenuItem itemRadio = new MenuItem(menu, SWT.RADIO);
itemRadio.setText("&RADIO item\tCtrl+R");
itemRadio.setAccelerator(SWT.CTRL + 'R');
itemRadio.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: RADIO item");
System.out.println("Selection: " + itemRadio.getSelection());
}
});
itemCascade.setMenu(menu);
//shell.setMenu(menuBar);
shell.setMenuBar(menuBar);
menuBar.setDefaultItem(itemCascade);
shell.setSize(300, 120);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new MenuExamples();
}
}

CoolBarExamples.java


代码如下:

package swt_jface.demo5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CoolBarExamples {

Display display = new Display();
Shell shell = new Shell(display);
public CoolBarExamples() {

shell.setLayout(new GridLayout());
final CoolBar coolBar = new CoolBar(shell, SWT.NONE);
coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
CoolItem textItem = new CoolItem(coolBar, SWT.NONE);
Text text = new Text(coolBar, SWT.BORDER | SWT.DROP_DOWN);
text.setText("TEXT");
text.pack();
Point size = text.getSize();
textItem.setControl(text);
textItem.setSize(textItem.computeSize(size.x, size.y));
CoolItem labelItem = new CoolItem(coolBar, SWT.NONE);
Label label = new Label(coolBar, SWT.NONE);
label.setText("LABEL");
label.pack();
size = label.getSize();
labelItem.setControl(label);
labelItem.setSize(textItem.computeSize(size.x, size.y));
CoolItem buttonItem = new CoolItem(coolBar, SWT.NONE | SWT.DROP_DOWN);
Composite composite = new Composite(coolBar, SWT.NONE);
composite.setLayout(new GridLayout(2, true));
Button button1 = new Button(composite, SWT.PUSH);
button1.setText("Button 1");
button1.pack();
Button button2 = new Button(composite, SWT.PUSH);
button2.setText("Button 2");
button2.pack();
composite.pack();
size = composite.getSize();
buttonItem.setControl(composite);
buttonItem.setSize(buttonItem.computeSize(size.x, size.y));
// // Test cool item adding method.
// Label label2 = new Label(coolBar, SWT.NONE);
// label2.setText("label2");
// addControlToCoolBar(label2, SWT.DROP_DOWN, coolBar);
try {
setState(coolBar, new File("coolbar.state"));
} catch (IOException e1) {
e1.printStackTrace();
}
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
try {
saveState(coolBar, new File("coolbar.state") );
} catch (IOException e) {
e.printStackTrace();
}
}
});
shell.setSize(300, 120);
// shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static CoolItem addControlToCoolBar(
Control control,
int coolItemStyle,
CoolBar coolBar) {
CoolItem coolItem = new CoolItem(coolBar, coolItemStyle);
Point size = control.getSize();
if (size.x == 0 && size.y == 0) {
control.pack();
size = control.getSize();
}
coolItem.setControl(control);
coolItem.setSize(coolItem.computeSize(size.x, size.y));
return coolItem;
}

private void saveState(CoolBar coolBar, File file) throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
try {
System.out.println("Item order: " + intArrayToString(coolBar.getItemOrder()));
int[] order = coolBar.getItemOrder();
out.writeInt(order.length);
for(int i=0; i<order.length; i++)
out.writeInt(order[i]);
System.out.println("Wrap indices: " + intArrayToString(coolBar.getWrapIndices()));
int[] wrapIndices = coolBar.getWrapIndices();
out.writeInt(wrapIndices.length);
for(int i=0; i<wrapIndices.length; i++)
out.writeInt(wrapIndices[i]);
Point[] sizes = coolBar.getItemSizes();
out.writeInt(sizes.length);
for(int i=0; i<sizes.length; i++) {
out.writeInt(sizes[i].x);
out.writeInt(sizes[i].y);
}
} finally {
out.close();
}
}

private void setState(CoolBar coolBar, File file) throws IOException {
if(! file.exists())
throw new IOException("File does not exist: " + file);
DataInputStream in = new DataInputStream(new FileInputStream(file));
try {
int size = in.readInt();
int[] order = new int[size];
for(int i=0; i<order.length; i++)
order[i] = in.readInt();
size = in.readInt();
int[] wrapIndices = new int[size];
for(int i=0; i<wrapIndices.length; i++)
wrapIndices[i] = in.readInt();
size = in.readInt();
Point[] sizes = new Point[size];
for(int i=0; i<sizes.length; i++)
sizes[i] = new Point(in.readInt(), in.readInt());

coolBar.setItemLayout(order, wrapIndices, sizes);
} finally {
in.close();
}
}
public static String intArrayToString(int values[]) {
StringBuffer sb = new StringBuffer();
sb.append("{");
for (int i = 0; values != null && i < values.length; i++) {
sb.append(values[i]);
if (i != values.length - 1)
sb.append(", ");
}
sb.append("}");
return sb.toString();
}
public static void main(String[] args) {
new CoolBarExamples();
}
}

ToolBarExamples.java


代码如下:

package swt_jface.demo5;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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 ToolBarExamples {

Display display = new Display();
Shell shell = new Shell(display);
ToolBar toolBar;

public ToolBarExamples() {

toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);
itemPush.setText("PUSH item");
Image icon = new Image(shell.getDisplay(), "icons/new.gif");
itemPush.setImage(icon);
ToolItem itemCheck = new ToolItem(toolBar, SWT.CHECK);
itemCheck.setText("CHECK item");
ToolItem itemRadio1 = new ToolItem(toolBar, SWT.RADIO);
itemRadio1.setText("RADIO item 1");
ToolItem itemRadio2 = new ToolItem(toolBar, SWT.RADIO);
itemRadio2.setText("RADIO item 2");
ToolItem itemSeparator = new ToolItem(toolBar, SWT.SEPARATOR);
Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE);
text.pack();
itemSeparator.setWidth(text.getBounds().width);
itemSeparator.setControl(text);

final ToolItem itemDropDown = new ToolItem(toolBar, SWT.DROP_DOWN);
itemDropDown.setText("DROP_DOWN item");
itemDropDown.setToolTipText("Click here to see a drop down menu ...");
final Menu menu = new Menu(shell, SWT.POP_UP);
new MenuItem(menu, SWT.PUSH).setText("Menu item 1");
new MenuItem(menu, SWT.PUSH).setText("Menu item 2");
new MenuItem(menu, SWT.SEPARATOR);
new MenuItem(menu, SWT.PUSH).setText("Menu item 3");

itemDropDown.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if(event.detail == SWT.ARROW) {
Rectangle bounds = itemDropDown.getBounds();
Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
menu.setLocation(point);
menu.setVisible(true);
}
}
});
Listener selectionListener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem)event.widget;
System.out.println(item.getText() + " is selected");
if( (item.getStyle() & SWT.RADIO) != 0 || (item.getStyle() & SWT.CHECK) != 0 )
System.out.println("Selection status: " + item.getSelection());
}
};
itemPush.addListener(SWT.Selection, selectionListener);
itemCheck.addListener(SWT.Selection, selectionListener);
itemRadio1.addListener(SWT.Selection, selectionListener);
itemRadio2.addListener(SWT.Selection, selectionListener);
itemDropDown.addListener(SWT.Selection, selectionListener);
toolBar.pack();

shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = shell.getClientArea();
toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT));
}
});
shell.setSize(500, 100);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new ToolBarExamples();
}
}

(0)

相关推荐

  • SWT(JFace) Menu、Bar...体验代码

    演示代码: MenuExamples.java 复制代码 代码如下: package swt_jface.demo5; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; impo

  • SWT(JFace) 文本编辑器 实现代码

    代码如下: 实现1: BasicEditor.java 复制代码 代码如下: package swt_jface.demo5; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.eclipse.jface.resource.ImageDescriptor; i

  • SWT(JFace) 图片浏览器 实现代码

    代码如下: ImageViewer.java 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image;

  • SWT(JFace)体验之GridLayout布局

    GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

  • SWT(JFace) 打印功能

    演示代码如下: 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.printing.PrintDialog; import org.eclipse.swt.printing.Printer; import org.eclipse.swt.printing.PrinterData; import org.eclipse.swt.widgets.Display

  • ubuntu16.04登录后无dash,无启动栏launch,无menu bar只有桌面背景的快速解决办法

    今天打开电脑,与往常一样输入用户名密码登录后,发现桌面上空空如也,启动栏launch,menu bar什么的都消失了,桌面上文件可以打开,但是无法拖动位置,无法关闭(因为menu bar没了,无法鼠标点击关闭), 经过苦苦搜索几个小时之后找到解决方案如下,记录下来方便有相同问题的人: 问题原因:unity Plugin 被误删或禁用了 解决方案: 1.尝试用 ctrl + alt + t 打开命令行 2.若 ctrl + alt + t 不起作用,则可在桌面右键选择打开终端 3.若上述方法仍不起

  • SWT(JFace)体验之StyledText类

    WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

  • SWT(JFace)体验之ApplicationWindow

    测试代码如下: 复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.GridLayou

  • SWT(JFace) 体验之FontRegistry

    复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.ecli

  • SWT(JFace)体验之复制粘贴

    演示代码如下: 复制代码 代码如下: package swt_jface.demo11; import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.RTFTransfer; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.sw

随机推荐