列表框(List)的基本樣式與基礎應用

本次實驗要求設計一個小程序來說明列表框的一些常用方法。

程序的主要功能:

1. 單擊 “>” 按鈕,將左側選中的選項轉移到右側;

2. 單擊 “

3. 單擊 “>>” 按鈕,將左側所有選項都轉移到右側;

4. 單擊 “<

5. 單擊 "Up" 按鈕,將所選的選項(右側)向上移動一個位置;

6. 單擊 “Down” 按鈕,將所選的選項(右側)向下移動一個位置;

在代碼中,包含:

1. 創建事件監聽類,為內部類;

2. 選項上移與下移過程中考慮邊界;

代碼(無static void main(String[] args))如下:(《EclipseSWT/JFace 核心應用》清華大學出版社)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class ListSample {
private Shell shell;
public ListSample(Shell sShell) {
shell = new Shell(sShell, SWT.CLOSE);
shell.setSize(350, 250);
shell.setText("ListSample");
String itemLeft[] = new String[20];// 定義保存左側列表框中的數據
String itemright[] = new String[0];// 定義保存右側列表框中的數據
for(int i = 0; i < itemLeft.length; i++)
itemLeft[i] = "item" + i;// 初始化左側字符串數組
final List left = new List(shell, SWT.MULTI|SWT.V_SCROLL);
// 定義左側列表框,可選擇多個且帶有滾動條
left.setBounds(10, 10, 100, 180);// 設置位置和大小
left.setItems(itemLeft);// 設置選項數據
left.setToolTipText("Choosed List");// 設置提示
final List right = new List(shell, SWT.MULTI|SWT.V_SCROLL);
// 定義右側列表框,可選擇多個且帶有滾動條
right.setBounds(170, 10, 100, 180);
right.setItems(itemright);
right.setToolTipText("Choosed List");
// 創建事件監聽類,為內部類
SelectionAdapter listener = new SelectionAdapter() {
// 按鈕單擊事件處理的方法
public void widgetSelected(SelectionEvent e) {
// 取得觸發事件的空間對象(按鈕)
Button bt = (Button)e.widget;
if(bt.getText().equals(">"))// 如果是“>”按鈕
verifyValue(left.getSelection(), left, right);
else if(bt.getText().equals(">>"))// 如果是“>>”按鈕
verifyValue(left.getItems(), left, right);
else if(bt.getText().equals("
verifyValue(right.getSelection(), right, left);
else if(bt.getText().equals("<
verifyValue(right.getItems(), right, left);
else if(bt.getText().equals("Up"))// 如果是“Up”按鈕
{
int index = right.getSelectionIndex();
// 獲得當前選中選項的索引值
if(index <= 0)// 如未選中,則返回
return ;
String currentValue = right.getItem(index);
// 如果選中了選項值,獲得當前選項的值
right.setItem(index, right.getItem(index - 1));
right.setItem(index - 1, currentValue);
// 將選中的選項與上一個選項交換值
right.setSelection(index - 1);// 設定上一選項為選中狀態
}
else if(bt.getText().equals("Down"))// 如果是“Down”按鈕
{
// 與“Up”按鈕邏輯相同
int index = right.getSelectionIndex();
if(index >= right.getItemCount() - 1)
return ;
String currentValue = right.getItem(index);
right.setItem(index, right.getItem(index + 1));
right.setItem(index + 1, currentValue);
right.setSelection(index + 1);
}
}
};
// 定義按鈕
// 定義(單)右移按鈕
Button bt1 = new Button(shell, SWT.NONE);
bt1.setText(">");
bt1.setBounds(130, 20, 25, 20);
bt1.addSelectionListener(listener);// 為按鈕注冊事件,其他的按鈕類似
// 定義(多)右移按鈕
Button bt2 = new Button(shell, SWT.NONE);
bt2.setText(">>");
bt2.setBounds(130, 55, 25, 20);
bt2.addSelectionListener(listener);
// 定義(多)左移按鈕
Button bt3 = new Button(shell, SWT.NONE);
bt3.setText("<
bt3.setBounds(130, 90, 25, 20);
bt3.addSelectionListener(listener);
// 定義(單)左移按鈕
Button bt4 = new Button(shell, SWT.NONE);
bt4.setText("
bt4.setBounds(130, 125, 25, 20);
bt4.addSelectionListener(listener);
// 定義上移按鈕
Button bt5 = new Button(shell, SWT.NONE);
bt5.setText("Up");
bt5.setBounds(285, 70, 40, 20);
bt5.addSelectionListener(listener);
// 定義下移按鈕
Button bt6 = new Button(shell, SWT.NONE);
bt6.setText("Down");
bt6.setBounds(285, 105, 40, 20);
bt6.addSelectionListener(listener);
shell.open();
}
private void verifyValue(String[] selected, List from, List to) {
for(int i = 0; i < selected.length; i++)
{
from.remove(selected[i]);// 從一個列表中移除該選項值
to.add(selected[i]);// 添加到另一列表中
}
}
}

List 類所使用的樣式常量

樣式常量

描述

SWT.SINGLE

只能選中一個選項,為默認樣式

SWT.MULTI

可同時選擇多個選項的列表框,按住 Ctrl 鍵即可多選

SWT.H_SCROLL

帶有水平滾動條的多行列表框

SWT.V_SCROLL

帶有垂直滾動條的多行列表框

方法·向列表框中添加選項

方法

含義

add(String string)

向列表框的末尾添加選項

add(String string, int index); setItem(int index, String string)

向列表指定位置添加選項

setItems(String[] items)

將字符串數組添加到列表框

方法·刪除列表框中的選項

方法

含義

remove(int index)

刪除指定一個索引出的選項

remove(int[] indices)

刪除多個索引的選項

remove(int start, int end)

刪除指定開始和結束索引值間的選項

remove(String string)

刪除指定字符串的選型

removeAll()

刪除列表中所有的選項

方法·獲得列表框的狀態

方法

含義

String getItem(int index)

獲得指定索引的選項值

getItemCount()

獲得選項的總數

String[] getItems()

獲得所有的選項值

getTopIndex()

獲得第一個選項的索引值

indexOf(String string)

查找是否存在指定的選項,如果存在,測返回所在的索引值

indexOf(String string, int start)

查找指定開始索引后是否存在指定的選項,如存在,則放回索引值

方法·對列表框中已選中的選項操作方法

方法

含義

String[] getSelection()

獲取當前所選中的所有選項

getSelectionCount()

獲得當前選中的選項數目

getSelectionIndex()

獲得當前選中選項的索引值

int[] getSelectionIndices

獲得當前選中選項的索引值數組

isSelected(int index)

判斷某個索引值的選項是否已被選中:true, false

select(int index); setSelection(int index)

設置指定索引值的選項為選中狀態

select(int[] indices); setSelection(int[] indices)

設置指定索引值數組的選項為選中狀態

select(int start, int end); setSelection(int start, int end)

設置開始索引到結束索引的選項為選中狀態

selectAll()

全選

setSelection(String[] items)

設置指定字符串數組中值為選中狀態

showSelection()

顯示選中的方法

deselect(int index)

反選指定索引的選項

deselect(int[] indices)

反選指定索引數組的選項

deselect(int start, int end)

反選開始索引到結束索引的選項

deselectAll()

全部反選