java swt demo_SWT(JFace)体验之List演示汇总

代码如下:

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

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 ? "" : 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值