SWT的CTabFolder 自定义选项卡

此文章转载自 http://www.blogjava.net/Javawind/archive/2008/06/06/206397.html

package com.example.swt.ctabfoldersample;    
   
import org.eclipse.swt.SWT;    
import org.eclipse.swt.custom.CTabFolder;    
import org.eclipse.swt.custom.CTabFolder2Adapter;    
import org.eclipse.swt.custom.CTabFolderEvent;    
import org.eclipse.swt.custom.CTabItem;    
import org.eclipse.swt.graphics.Color;    
import org.eclipse.swt.graphics.Image;    
import org.eclipse.swt.layout.GridData;    
import org.eclipse.swt.layout.GridLayout;    
import org.eclipse.swt.widgets.Display;    
import org.eclipse.swt.widgets.Shell;    
import org.eclipse.swt.widgets.Text;    
   
public class CTabFolderSample    
{    
    public static void main(String[] args)    
        {    
            final Display display = Display.getDefault();    
            final Shell shell = new Shell();    
            shell.setSize(296, 255);    
            shell.setText("CTabFolder 练习");    
            shell.setLayout(new GridLayout());    
            //    
   
            shell.open();    
   
            final CTabFolder tabFolder = new CTabFolder(shell, SWT.NONE|SWT.CLOSE|SWT.BORDER);    
            tabFolder.addCTabFolder2Listener(new CTabFolder2Adapter() {    
                public void minimize(CTabFolderEvent event) {    
                        tabFolder.setMinimized(true);    
                        tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));    
                        shell.layout(true);//刷新布局    
                }    
                public void maximize(CTabFolderEvent event) {    
                        tabFolder.setMaximized(true);    
                        tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));    
                        shell.layout(true);    
                }    
                public void restore(CTabFolderEvent event) {    
                        tabFolder.setMinimized(false);    
                        tabFolder.setMaximized(false);    
                        tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));    
                        shell.layout(true);    
                }    
            });    
            //tabFolder.setBounds(0, 0, 283, 211);    
            tabFolder.setTabHeight(20);    
            tabFolder.marginHeight = 5;    
            tabFolder.marginWidth = 5;    
            tabFolder.setMaximizeVisible(true);    
            tabFolder.setMinimizeVisible(true);    
            tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));    
            //下面两个是设置固定的背景色和前景色    
            //tabFolder.setBackground(display.getSystemColor(SWT.COLOR_BLUE));    
            //tabFolder.setForeground(display.getSystemColor(SWT.COLOR_WHITE));    
            //下面是设置渐变色    
            Color[] color=new Color[4];    
            color[0]=display.getSystemColor(SWT.COLOR_DARK_BLUE);    
            color[1]=display.getSystemColor(SWT.COLOR_BLUE);    
            color[2]=display.getSystemColor(SWT.COLOR_DARK_GRAY);    
            color[3]=display.getSystemColor(SWT.COLOR_WHITE);    
            int[] intArray=new int[]{25,45,100};    
            tabFolder.setSelectionBackground(color, intArray);    
            //这是设置了背景颜色,但是如果同时设置了背景图片的话以背景图片优先    
            tabFolder.setSimple(false);//设置圆角    
            tabFolder.setUnselectedCloseVisible(true);    
            for (int i = 1; i < 4; i++) {    
                CTabItem item = new CTabItem(tabFolder, SWT.None|SWT.MULTI|SWT.V_SCROLL);    
                item.setText("选项卡" + i);    
                Text t = new Text(tabFolder, SWT.None|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL|SWT.WRAP);    
                t.setText("这是选项卡可以控制的文字" + i+"\n\n世界第一等\n\n一路顺风");    
                item.setControl(t);    
   
            }    
            Image image=new Image(display,"C:\\Documents and Settings\\Administrator\\桌面\\label.jpg");    
            shell.setImage(image);      
            shell.setSize(300, 200);    
            shell.layout();    
            while (!shell.isDisposed()) {    
                if (!display.readAndDispatch())    
                    display.sleep();    
            }    
        }    
   
}
其中
cfolder.setBackground中的参数为

colors - 一个Color array定义渐变中从左到右的几个颜色。  null 可以用来清除背景色。
percents - 数字数列(值为0-100)定义了渐变中各个颜色的宽度, 这个数列的大小要比color数列小1位。
vertical - 定义渐变的方向, True表示垂直,而false表示水平。 Eclipse中蓝色选项卡的配色(类似)


Eclipse中蓝色选项卡的配色(类似)
 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};

or

Eclipse中蓝色选项卡的配色(类似)
private void setCTabFolderBackground(final CTabFolder folder)
    
{
        Display display = Display.getDefault();

        final Color titleFore = display.getSystemColor(SWT.COLOR_TITLE_FOREGROUND);

        final Color titleBack = display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND);
        final Color titleBackGrad = display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT);

        final Color titleInactiveBackGrad = display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT);
        final Color titleInactiveBack = display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND);

        Listener listener = new Listener()
        {
            public void handleEvent(Event e)
            {
                switch (e.type)
                {
                    case SWT.Activate:
                        folder.setSelectionForeground(titleFore);
                        folder.setSelectionBackground(new Color[] { titleBack, titleBackGrad }new int[] { 100 }true);
                        break;

                    case SWT.Deactivate:
                        folder.setSelectionForeground(titleFore);
                        folder.setSelectionBackground(new Color[] { titleInactiveBack, titleInactiveBackGrad }new int[] { 100 }true);
                        break;
                }

            }

        }
;
        folder.addListener(SWT.Activate, listener);
        folder.addListener(SWT.Deactivate, listener);
    }






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值