swt获取list列表框是否选择了项
时间: 2023-06-18 19:03:22 浏览: 65
在 SWT 中,可以通过 `List` 控件的 `getSelectionIndex()` 方法来获取当前选择的项的索引,如果没有选择任何项,则返回 -1。例如:
```java
List list = new List(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
list.setItems(new String[] {"Item 1", "Item 2", "Item 3"});
list.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int index = list.getSelectionIndex();
if (index == -1) {
System.out.println("No item selected");
} else {
System.out.println("Selected item: " + list.getItem(index));
}
}
});
```
在上面的示例中,我们通过监听 `SelectionEvent` 事件来获取选择的项的索引,并根据索引来判断是否选择了项。如果选择了项,则输出选择的项的文本;如果没有选择任何项,则输出提示信息。
相关问题
swt 获取list中指定内容的索引
你可以使用 Java SWT 中的 List 控件的 `indexOf(String string)` 方法来获取 List 中指定内容的索引。这个方法将返回一个整数,表示指定字符串在 List 中的索引位置。以下是一个使用示例:
```java
List list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
list.setItems(new String[] {"Item 1", "Item 2", "Item 3", "Item 4"});
int index = list.indexOf("Item 2");
System.out.println("Item 2 的索引为:" + index);
```
在这个示例中,List 控件中包含了四个字符串,然后我们使用 `indexOf` 方法来获取字符串 "Item 2" 在 List 中的索引位置,并将结果输出到控制台上。
SWT 设置Text边框大小
你可以使用 SWT 的 StyleConstants 类来设置 Text 控件的边框大小。以下是一个示例代码:
```
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextBorderExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
StyledText text = new StyledText(shell, SWT.BORDER);
text.setText("Hello World!");
// 设置边框大小
StyleRange styleRange = new StyleRange();
styleRange.borderStyle = SWT.BORDER_SOLID;
styleRange.borderColor = new Color(display, 0, 0, 255);
styleRange.borderWidth = 2;
styleRange.start = 0;
styleRange.length = text.getText().length();
text.setStyleRange(styleRange);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
```
在上面的示例代码中,我们通过为 StyleRange 对象设置 borderWidth 和 borderStyle 属性来设置 Text 控件的边框大小和样式。然后,我们将该 StyleRange 对象应用到整个文本范围中,以便将边框应用于整个文本。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)