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

 更新时间:2009年06月25日 11:32:20   作者:  
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();
}
}

相关文章

  • Springboot继承Keycloak实现单点登录与退出功能

    Springboot继承Keycloak实现单点登录与退出功能

    这篇文章主要介绍了Springboot继承Keycloak实现单点登陆与退出,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • 详谈java线程与线程、进程与进程间通信

    详谈java线程与线程、进程与进程间通信

    下面小编就为大家带来一篇详谈java线程与线程、进程与进程间通信。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • SpringAOP切点函数实现原理详解

    SpringAOP切点函数实现原理详解

    这篇文章主要介绍了SpringAOP切点函数实现原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot如何集成Token

    SpringBoot如何集成Token

    文章介绍了如何使用jjwt插件实现Token的生成和校验,该插件可以直接与SpringBoot集成,Token由三部分组成,分别是header、payload和signature,通过在请求头中传递Token,后端可以验证其合法性,从而提高安全性
    2025-01-01
  • java网上图书商城(8)订单模块3

    java网上图书商城(8)订单模块3

    这篇文章主要为大家详细介绍了java网上图书商城,订单模块第三篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Java读取其下所有文件夹与文件路径的方法

    Java读取其下所有文件夹与文件路径的方法

    这篇文章主要为大家详细介绍了Java读取其下所有文件夹与文件路径的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Java实现对象转CSV格式

    Java实现对象转CSV格式

    CSV是一种逗号分隔值格式的文件,一般用来存储数据的纯文本格式文件。Java对象转CSV,有现成的工具包,commons-lang3 的ReflectionToStringBuilder 就可以简单的解决的对象转CSV,快跟随小编一起学习一下吧
    2022-06-06
  • java中的常用集合类整理

    java中的常用集合类整理

    在本篇文章里小编给大家整理的是关于java中的常用集合类的相关知识点内容,有兴趣的朋友们学习下。
    2019-12-12
  • java使用JOptionPane猜数字游戏

    java使用JOptionPane猜数字游戏

    这篇文章主要为大家详细介绍了java使用JOptionPane猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • spring中EventListener的使用方式

    spring中EventListener的使用方式

    这篇文章主要介绍了spring中EventListener的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论