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

页面导航:首页 > 软件编程 > Java编程 > SWT(JFace) Menu、Bar...体验代码

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

来源:互联网 作者:脚本宝典 时间:2015-07-30 13:11 【

演示代码: 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 or

演示代码: 
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(); 

}


Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<