Posted on 2008-06-11 22:58
kooyee 閱讀(1674)
評論(0) 編輯 收藏 所屬分類:
GUI骨衣
當存在多個容器或選項卡容器時,其中一個選項卡在激活(選擇時)與非激活時標簽的顏色變化。 例如eclipse中點擊周圍的小窗口上的選項卡后,主窗口的選項卡標簽由藍色變為白色,反之亦然。
我琢磨出來實現這個效果的方法是,首先給選項卡中的控件加入focusLost事件(如果表現背景使用了漸變色,在這里改變成新的背景色的話,也要使用同樣的Method和值為null的Color array來清空原來的背景色或定義新的顏色)

text.addFocusListener(new org.eclipse.swt.events.FocusAdapter()
{

public void focusLost(org.eclipse.swt.events.FocusEvent e)
{

Color[] c =
{null, null,null};

int[] i =
{10,100};
tab.setSelectionBackground(c,i);
tab.setSelectionForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
}
其次當再次選擇非激活的選項卡時,再改變會激活狀態的顏色。給選項卡中的控件加入focusGained事件并且選項卡加入selection和mouseDown事件。
selection和mouseDown事件分別是當選項卡被點擊和選擇時focus選項卡中的控件, focusGained事件改變標簽顏色

tab2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()
{

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
{
text.forceFocus();
}
});

tab2.addMouseListener(new org.eclipse.swt.events.MouseAdapter()
{

public void mouseDown(org.eclipse.swt.events.MouseEvent e)
{
text.forceFocus();
}
});


private void disactive (CTabFolder arg0)
{

Color[] c =
{null, null,null};

int[] i =
{10,100};
arg0.setSelectionBackground(c,i);
arg0.setSelectionForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
}

private void active (CTabFolder arg0)
{
Color[] color=new Color[4];
color[0]=new Color(Display.getCurrent(), 0, 78, 255);
color[1]=new Color(Display.getCurrent(), 0, 98, 255);
color[2]=Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);
color[3]=Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);

int[] intArray=new int[]
{50,70,100};
arg0.setSelectionBackground(color, intArray, true);
arg0.setSelectionForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
}
控件的focusGained事件

text.addFocusListener(new org.eclipse.swt.events.FocusAdapter()
{

public void focusGained(org.eclipse.swt.events.FocusEvent e)
{
active(tab2);
}
});