Une interface SWT complexe avec ProgressBar, ToolBar, StatusBar, MenuBar et StyledText.
Le résultat et comme suit:
Le code source se trouve ci dessous:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
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.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Main {
static ToolBar toolBar;
static Shell shell;
static StyledText styledText;
static StyledText styledText2;
static ProgressBar bar;
/**
*
*/
public Main() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
shell = new Shell(display);
final Image image = new Image(display, "ressources/images/logo32.png");
shell.setImage(image);
new WpiMenu(shell);
// ToolBar toolBar;
FormLayout layout = new FormLayout();
shell.setLayout(layout);
toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
// toolBar.setBackground(new Color(display, 2, 2, 2));
ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);
Image icon = new Image(shell.getDisplay(),
"ressources/images/document-open-data.png");
itemPush.setImage(icon);
ToolItem itemPush2 = new ToolItem(toolBar, SWT.PUSH);
Image icon2 = new Image(shell.getDisplay(),
"ressources/images/document-save.png");
itemPush2.setImage(icon2);
ToolItem itemPush3 = new ToolItem(toolBar, SWT.PUSH);
Image icon3 = new Image(shell.getDisplay(),
"ressources/images/logo16.png");
itemPush3.setImage(icon3);
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");
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());
bar.setSelection(60);
}
};
// StatusBar
final Label label = new Label(shell, SWT.BOTTOM);
label.setText("Hello");
label.update();
// label.setSize(800, 20);
FormData labelData = new FormData();
labelData.left = new FormAttachment(0);
labelData.right = new FormAttachment(100);
labelData.bottom = new FormAttachment(100);
labelData.height = 20;
label.setLayoutData(labelData);
// ProgressBar
bar = new ProgressBar(shell, 1);
bar.setSize(100, 20);
FormData labelData3 = new FormData();
labelData3.left = new FormAttachment(0);
labelData3.right = new FormAttachment(100);
labelData3.bottom = new FormAttachment(label, 0);
labelData3.height = 20;
bar.setLayoutData(labelData3);
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);
FormData labelData0 = new FormData();
labelData0.left = new FormAttachment(0);
labelData0.right = new FormAttachment(100);
labelData0.top = new FormAttachment(0);
toolBar.setLayoutData(labelData0);
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));
}
});
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
// styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
FormData labelData2 = new FormData();
labelData2.left = new FormAttachment(0);
labelData2.right = new FormAttachment(100);
labelData2.top = new FormAttachment(toolBar, 0);
labelData2.bottom = new FormAttachment(50);
// labelData2.bottom=new FormAttachment(50);
styledText.setLayoutData(labelData2);
Font font = new Font(shell.getDisplay(), "Book Antiqua", 12, SWT.NORMAL);
styledText.setFont(font);
styledText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
}
});
styledText2 = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
// styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
FormData labelData4 = new FormData();
labelData4.left = new FormAttachment(0);
labelData4.right = new FormAttachment(100);
labelData4.top = new FormAttachment(50);
labelData4.bottom = new FormAttachment(bar, 0);
styledText2.setLayoutData(labelData4);
Font font2 = new Font(shell.getDisplay(), "Book Antiqua", 12,
SWT.NORMAL);
styledText2.setFont(font2);
styledText2.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
}
});
shell.setSize(800, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Pour plus d'informations, veuillez vous référer aux sites web suivant:
http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html
http://www.eclipse.org/swt/snippets/
Pour publier du code source dans blogger, l'outil suivant est très pratique:
http://www.accessify.com/tools-and-wizards/developer-tools/quick-escape/default.php

Aucun commentaire:
Enregistrer un commentaire