我有以下代码:
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
shell.setText("One Potato, Two Potato");
// Create the focus listener
FocusListener listener = new FocusListener() {
public void focusGained(FocusEvent event) {
Button button = (Button) event.getSource();
button.setText("I'm It!");
}
public void focusLost(FocusEvent event) {
Button button = (Button) event.getSource();
button.setText("Pick Me!");
}
};
// Create the buttons and add the listener to each one
for (int i = 0; i < 6; i++) {
Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
button.setText("Pick Me!");
button.addFocusListener(listener);
}
// Display the window
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}但它似乎不起作用。我在Mac机器上开发Eclipse IDE。我做的事有什么问题吗?
发布于 2014-09-16 20:32:07
这可能不是你想要的答案,但我也注意到一些监听器在mac上不能工作( focus监听器在shell上也不能工作,其他的监听器在mac上也有but )。你最好的办法就是尝试用文本替换Button。让它看起来很像。即使在mac上,focus listener也可以处理文本。因此,您的代码将如下所示:
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
shell.setText("One Potato, Two Potato");
// Create the focus listener
FocusListener listener = new FocusListener() {
public void focusGained(FocusEvent event) {
Text text = (Text) event.getSource();
text.setText("I'm It!");
}
public void focusLost(FocusEvent event) {
Text text = (Text) event.getSource();
text.setText("Pick Me!");
}
};
// Create the buttons and add the listener to each one
for (int i = 0; i < 6; i++) {
Text text = new Text(shell, SWT.PUSH);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setText("Pick Me!");
text.addFocusListener(listener);
}
// Display the window
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}现在,您唯一需要做的就是更改字段,使其看起来更像一个按钮。希望能有所帮助。祝你好运!
附言:应该有人打开一个关于这个问题的bug。
https://stackoverflow.com/questions/25716610
复制相似问题