
常用软件类: |
|杀毒安全 | |联络聊天 | |网络软件 | |多媒体类 | |系统工具 | |图形图像 | |系统工具 | |应用软件 | |行业软件 |
开发设计类: |
|动画制作 | |图像处理 | |3D设计 | |操作系统 | |站长学院 | |网络相关 | |WEB设计 | |数据库类 | |程序开发 |
还需要指出,通过添加绘制监听器来实现个性化的外观,并在调用影响外观的操作(比如setEnable)时调用redraw方法强制组件重绘,这是自定义组件常用的实现手段。你会看到接下来的很多方法会经常调用redraw通知组件重绘。
除了setEnabled方法,还有一些方法需要补充,一并列出:
| public void setEditable(boolean editable) { inputText.setEditable(editable); } public String getText() { return inputText.getText(); } public void setText(String text) { inputText.setText(text); } public void setTextLimit(int limit) { inputText.setTextLimit(limit); } |
| inputText.addSelectionListener(new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { commit(); } }); |
| protected void commit() {}; |
| addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { if (e.x > getBounds().width - COMBO_ICON.getBounds().width - 15 && e.x < getBounds().width && e.y > 0 && e.y < getBounds().height) { selectorMenu.setLocation(getScreemLocation().x + 3, getScreemLocation().y + getSize().y + 23); selectorMenu.setVisible(true); } } }); |
| private Vector dataSet = new Vector(); |
| public void loadMenuItems(Object[] objects) { dataSet.clear(); MenuItem[] items = selectorMenu.getItems(); for (MenuItem item : items) { item.removeSelectionListener(this); item.dispose(); } for (int i = 0; i < objects.length; i++) { dataSet.add(objects[i]); MenuItem item = new MenuItem(selectorMenu, SWT.PUSH); item.setText(objects[i].toString()); item.setData(objects[i]); item.addSelectionListener(this); } } |
| public final void widgetDefaultSelected(SelectionEvent e) public final void widgetSelected(SelectionEvent e) |
| public final void widgetSelected(SelectionEvent e) { MenuItem item = (MenuItem) e.getSource(); selectedItem = item.getData(); String text = item.getData().toString(); inputText.setText(text); inputText.setSelection(0, text.length()); selected(item.getData()); } |
| protected void selected(Object object) {}; |
| public void select(int index) { MenuItem[] items = selectorMenu.getItems(); if (index < 0 || index >= items.length) { throw new ArrayIndexOutOfBoundsException( "the index value must between " + 0 + "and " + (items.length - 1)); } selectedItem = items[index].getData(); inputText.setText(items[index].getText()); } |
| package swt.custom; public class Person { private String userName; private String password; public Person(String userName, String password) { this.userName = userName; this.password = password; } public String getPassword() { return password; } public String getUserName() { return userName; } @Override public String toString() { return userName; } } |
| final ComboSelector selector = new ComboSelector(this) { @Override protected void commit() { System.out.println("current data is " + ((Person) getSelectedItem()).getUserName()); } @Override protected void selected(Object object) { System.out.println(((Person) object).getPassword()); } }; selector.setBounds(114, 78, 200, 20); Person[] persons = new Person[] { new Person("play_station3@sina.com", "111111"), new Person("rehte@hotmail.com", "222222"), new Person("yitong.liu@bea.com", "password"), new Person("使用其他Windows Live ID 登录", "no") }; selector.loadMenuItems(persons); selector.select(1); |