我是靠谱客的博主 霸气马里奥,最近开发中收集的这篇文章主要介绍SWT(JFace)体验之List演示汇总第1/2页,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

代码如下:
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();
}
}

12下一页阅读全文

最后

以上就是霸气马里奥为你收集整理的SWT(JFace)体验之List演示汇总第1/2页的全部内容,希望文章能够帮你解决SWT(JFace)体验之List演示汇总第1/2页所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(163)

评论列表共有 0 条评论

立即
投稿
返回
顶部