kooyee ‘s blog

开源软件, 众人努力的结晶, 全人类的共同财富
posts - 103, comments - 55, trackbacks - 0, articles - 66
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

『SWT』选项卡的激活与非激活状态

Posted on 2008-06-11 22:58 kooyee 阅读(1666) 评论(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 
= {nullnull,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 
= {nullnull,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(), 078255);
        color[
1]=new Color(Display.getCurrent(), 098255);  
        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);
            }

        }
);

只有注册用户登录后才能发表评论。


网站导航: