我是Java Basic的初学者,并且遇到了getSelectedItem()的问题。 之前发布了同样的问题但是它总是返回时无法解决我的问题:
对于类型Combo
,方法getSelectedItem()未定义
我的完整代码:
public class FormObjects {
protected Shell shell;
private Text txtComboBoxItem;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
FormObjects window = new FormObjects();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Combo comboOne = new Combo(shell, SWT.NONE);
comboOne.setItems(new String[] {"C Sharp", "Java", "PHP", "Visual Basic", ".NET"});
comboOne.setBounds(30, 50, 91, 23);
Button btnComboBox = new Button(shell, SWT.NONE);
btnComboBox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String itemText = (String)comboOne.getSelectedItem();
txtComboBoxItem.setText(itemText);
}
});
btnComboBox.setBounds(147, 48, 130, 25);
btnComboBox.setText("Get Drop Down Item");
txtComboBoxItem = new Text(shell, SWT.BORDER);
txtComboBoxItem.setBounds(304, 50, 101, 21);
}
}
答案 0 :(得分:0)
我可以使用setText()来解决我的问题。谢谢大家!