微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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 itemtCtrl+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 itemtCtrl+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 itemtCtrl+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; IoUt.writeInt(order[i]);

System.out.println("Wrap indices: " + intArrayToString(coolBar.getWrapIndices()));

int[] wrapIndices = coolBar.getWrapIndices();

out.writeInt(wrapIndices.length);

for(int i=0; IoUt.writeInt(wrapIndices[i]);

Point[] sizes = coolBar.getItemSizes();

out.writeInt(sizes.length);

for(int i=0; IoUt.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; iorder[i] = in.readInt();

size = in.readInt();

int[] wrapIndices = new int[size];

for(int i=0; iwrapIndices[i] = in.readInt();

size = in.readInt();

Point[] sizes = new Point[size];

for(int i=0; isizes[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 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();

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐