Draw2d 中 Label Clickable ButtonModel的使用
程序员文章站
2024-03-24 11:16:10
...
摘要:Draw2d 实现按钮和标签一样的小控件。
public class Draw2D_Example {
public static void main(String args[]) {
final Label label = new Label("Press a button!");
Shell shell = new Shell();
LightweightSystem lws = new LightweightSystem(shell);
Figure parent = new Figure();
parent.setLayoutManager(new XYLayout());
lws.setContents(parent);
Clickable above = new CheckBox("I'm above!");
parent.add(above, new Rectangle(10,10,80,20));
ButtonModel aModel = new ToggleModel();
aModel.addChangeListener(new ChangeListener() {
public void handleStateChanged(ChangeEvent e) {
System.out.println(e.getPropertyName());label.setText("Above");
}
});
above.setModel(aModel);
Clickable below = new CheckBox("I'm below!");
parent.add(below, new Rectangle(10,40,80,20));
ButtonModel bModel = new ToggleModel();
bModel.addChangeListener(new ChangeListener() {
public void handleStateChanged(ChangeEvent e) {
label.setText("Below");
}
});
below.setModel(bModel);
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(aModel);
bGroup.add(bModel);
bGroup.setDefault(aModel);
parent.add(label, new Rectangle(10,70,80,20));
shell.setSize(130,120);
shell.open();
shell.setText("Example");
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep ();
}
}
}
图片:
结果 写道
mouseover
armed
pressed
selected
pressed
armed
mouseover
mouseover
armed
pressed
selected
pressed
armed
mouseover
armed
pressed
selected
pressed
armed
mouseover
mouseover
armed
pressed
selected
pressed
armed
mouseover
ChangeListener可以捕捉各种各样的鼠标操作。
上一篇: 摇杆是如何实现的