SWT(JFace)体验之List演示汇总第1/2页_Java编程

代码如下:
DropDownAndSimple.java

复制代码 代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DropDownAndSimple {

Display display = new Display();
Shell shell = new Shell(display);
public DropDownAndSimple() {
RowLayout rowLayout = new RowLayout();
rowLayout.spacing = 15;
rowLayout.marginWidth = 15;
rowLayout.marginHeight = 15;

shell.setLayout(rowLayout);

Combo comboDropDown = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
Combo comboSimple = new Combo(shell, SWT.SIMPLE | SWT.BORDER);

for(int i=0; i<3; i++) {
comboDropDown.add("item " + i);
comboSimple.add("item " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DropDownAndSimple();
}
}

SampleCombo.java

复制代码 代码如下:

package swt_jface.demo3;
import java.util.Arrays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SampleCombo {

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

shell.setLayout(new GridLayout(2, false));
(new Label(shell, SWT.NULL)).setText("Select your favorite programming language: ");
//final CCombo combo = new CCombo(shell, SWT.FLAT);
final Combo combo = new Combo(shell, SWT.NULL);
String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
Arrays.sort(languages);
for(int i=0; i<languages.length; i++)
combo.add(languages[i]);
//combo.add("Perl", 5);
//combo.setItem(5, "Perl");

combo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: " + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: " + combo.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println("Default selected index: " + combo.getSelectionIndex() + ", selected item: " + (combo.getSelectionIndex() == -1 ? "<null>" : combo.getItem(combo.getSelectionIndex())) + ", text content in the text field: " + combo.getText());
String text = combo.getText();
if(combo.indexOf(text) < 0) { // Not in the list yet.
combo.add(text);
// Re-sort
String[] items = combo.getItems();
Arrays.sort(items);
combo.setItems(items);
}
}
});
shell.pack();
shell.open();

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

SampleList.java

复制代码 代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SampleList {

Display display = new Display();
Shell shell = new Shell(display);
public SampleList() {
RowLayout rowLayout = new RowLayout();
shell.setLayout(rowLayout);
(new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
final List list = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
for(int i=0; i<languages.length; i++)
list.add(languages[i]);
list.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.err.println(list.getSelectionIndex());
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
public void widgetDefaultSelected(SelectionEvent e) {
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Default selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
});

list.selectAll();
//list.select(1);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(String[] args) {
new SampleList();
}
}

SingleMultiLists.java

复制代码 代码如下:

package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SingleMultiLists {

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

GridLayout gridLayout = new GridLayout(2, true);
shell.setLayout(gridLayout);
(new Label(shell, SWT.NULL)).setText("SINGLE");
(new Label(shell, SWT.NULL)).setText("MULTI");
List singleSelectList = new List(shell, SWT.BORDER);
List mutliSelectList = new List(shell, SWT.MULTI | SWT.BORDER);
String[] items = new String[]{"Item 1", "Item 2", "Item 3", "Item 4"};
for(int i=0; i<items.length; i++) {
singleSelectList.add(items[i]);
mutliSelectList.add(items[i]);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SingleMultiLists();
}
}

当前1/2页 12下一页阅读全文

时间: 2024-09-20 22:28:58

SWT(JFace)体验之List演示汇总第1/2页_Java编程的相关文章

SWT(JFace) 简易浏览器 制作实现代码第1/2页_Java编程

代码如下: BrowserExample.java 复制代码 代码如下: package swt_jface.demo5; import java.text.MessageFormat; import java.util.MissingResourceException; import org.eclipse.swt.SWT; import org.eclipse.swt.SWTError; import org.eclipse.swt.browser.Browser; import org.e

SWT(JFace)体验之GridLayout布局_Java编程

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

SWT(JFace)体验之ViewForm的使用_Java编程

代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ViewForm; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.s

SWT(JFace)体验之StyledText类_Java编程

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) 体验之FontRegistry_Java编程

复制代码 代码如下: 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)体验之圆环状(戒指型)_java

演示代码: 复制代码 代码如下: package swt_jface.demo1; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.graphics.Point; impor

SWT(JFace)体验之Slider,Scale_Java编程

Slider: 复制代码 代码如下: package swt_jface.demo8; 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.Event; import org.eclipse

SWT(JFace)体验之Sash(活动控件)_Java编程

演示代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt

SWT(JFace)体验之复制粘贴_Java编程

演示代码如下: 复制代码 代码如下: 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