kooyee ‘s blog

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

『SWT』SWT的CTabFolder

Posted on 2008-06-06 20:12 kooyee 阅读(2270) 评论(0)  编辑  收藏 所属分类: GUI骨衣
这个例子是使用自定义选项卡的例子,实现的功能是最大,最小化。在运用中这种是很常见的,贴上来备忘。

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

        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};

or

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);
    }

(感谢pantian的帮助)






说明:

第一、状态说明,根据选项卡的状态右上角显示最大化还是最小化还是回复的按钮。设置最小化的方法:setMinimized(true),设置最大化:setMaximized(true),设置为既不是最大也不是最小的办法是:同时把setMinimized和setMaximized设置为false。

获取状态的方法是:是否最大化getMinimized(),是否最大化getMaximized()。

第二、限制选项卡文字的长度:

tabFolder.setminimumCharacters(int)设置选项卡文字的最小长度。

第三、设置右上角控件

 

Button button=new Button(tabFolder,SWT.ARROW|SWT.RIGHT);

tabFolder.setTopRight(button);

    
//tabFolder.setTopRight(button,SWT.FILL);//布满右侧区域 

 


第四,选项卡标签的下划线:

当tabFolder.setSimple(false);//设置圆角后 当选项卡被focus时(例如使用了 forcefocus())
会在选项卡标签下出现的下划线, 移除的方法是: 给选项卡添加selection event 然后设置 focus 到selected CTabItem里的控件上
cTabFolder.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
            
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                textbox.forceFocus();
            }

        }
);



TabFolder/TabItem vs. CTabFolder/CTabItem

CTabFolder and CTabItem add flexibility to the standard tabs.


Description                                   TabFolder/TabItem      CTabFolder/CTabItem

Tab position                                  On top or on bottom    On top or on bottom

Supports text                                 Yes                    Yes

Supports tool tips                            Yes                    Yes

Supports images                               Yes                    Yes

Supports disabled images                      No                     Yes

Supports flat look                            No                     Yes

Supports customizable margins                 No                     Yes

Supports a control in the top-right corner    No                     Yes

Supports a gradient background                No                     Yes

Supports an image background                  No                     Yes
    

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


网站导航: