SWT中shell窗口中有一个按钮和一个canvas控件,怎样保证在点击按钮后,还可以让键盘监听保持在canvas上
SWT中shell窗口中有一个按钮和一个canvas控件,怎样保证在点击按钮后,还可以让键盘监听保持在canvas上,即在点击按钮后,还可通过键盘控制canvas上的动作...
SWT中shell窗口中有一个按钮和一个canvas控件,怎样保证在点击按钮后,还可以让键盘监听保持在canvas上,即在点击按钮后,还可通过键盘控制canvas上的动作,而不用点击一下canvas?
请高手指点 展开
请高手指点 展开
2个回答
展开全部
我写了个测试代码,你看看吧。希望会有帮助
public class FocusTest {
/**
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setText("canvas测试");
shell.setSize(300, 400);
final Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(100, 100, 100, 50);
final Button button = new Button(shell, SWT.NONE);
button.setFocus();
button.setText("聚焦到Canvas");
button.setBounds(100, 160, 100, 20);
button.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
canvas.setFocus();
button.setText("请按下任意键");
}
});
canvas.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
Canvas can = (Canvas) e.getSource();
can.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_BLUE));
}
public void keyReleased(KeyEvent e) {
}
});
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
public class FocusTest {
/**
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setText("canvas测试");
shell.setSize(300, 400);
final Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(100, 100, 100, 50);
final Button button = new Button(shell, SWT.NONE);
button.setFocus();
button.setText("聚焦到Canvas");
button.setBounds(100, 160, 100, 20);
button.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
canvas.setFocus();
button.setText("请按下任意键");
}
});
canvas.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
Canvas can = (Canvas) e.getSource();
can.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_BLUE));
}
public void keyReleased(KeyEvent e) {
}
});
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询