熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

開發Eclipse下的自定義控件[3]

2022-06-13   來源: Java開源技術 

addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
int row = (ey cy) / lineHeight; //計算選中的行
if (row >= ) {
oldRowSel = rowSel;
rowSel = row;
}
if (oldRowSel != rowSel) { // 重畫舊的和新的選擇項
((Canvas) egetSource())redraw(cx (ey / lineHeight)
* lineHeight maxX lineHeight false);
((Canvas) egetSource())redraw(cx (oldRowSel + cy
/ lineHeight)
* lineHeight maxX lineHeight false);
}
selectionChanged();
}
public void mouseUp(MouseEvent e) {
}
});

  當我們的控件獲得焦點時選中的列表項需要有虛框表示控件得到焦點當獲得或失去焦點是我們這裡只需要簡單的通知選中的項重畫

addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
((Canvas) egetSource())redraw(cx rowSel * lineHeight maxX
lineHeight true);
}
public void focusLost(FocusEvent e) {
((Canvas) egetSource())redraw(cx rowSel * lineHeight maxX
lineHeight true);
}
});

  我們在繪制每一個列表項時可以加入判斷當前控件是否得到焦點如果控件得到了焦點我們就在選中的項目上畫一個虛框下面是我們繪制一個列表項的代碼注意在代碼的最後繪制焦點的虛框

void onPaint(GC gc int row int beginx int beginy boolean isSelected) {
Color initColor = gcgetBackground();
Color initForeColor = gcgetForeground();
if (isSelected) {
gcsetBackground(DisplaygetCurrent()getSystemColor(
SWTCOLOR_LIST_SELECTION));
gcfillRectangle(beginx beginy maxX lineHeight);
gcsetForeground(DisplaygetCurrent()getSystemColor(
SWTCOLOR_LIST_SELECTION_TEXT));
} else {
gcsetBackground(initColor);
}
gcdrawString((String) colorNamesget(row) beginx + beginy);
Color color = DisplaygetCurrent()getSystemColor(
((Integer) colorsget(row))intValue());
gcsetBackground(color);
gcfillRectangle(beginx + beginy + lineHeight );
gcsetBackground(initColor);
gcsetForeground(initForeColor);
if (isFocusControl() && isSelected)
gcdrawFocus(cx beginy maxX lineHeight);
}

[]  []  []  []  []  []  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28984.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.