SWT的CTabFolder练习手记

这个例子是使用自定义选项卡的例子,实现的功能是最大,最小化。在运用中这种是很常见的,贴上来备忘。

Java代码
  1. package com.example.swt.ctabfoldersample;   
  2.   
  3. import org.eclipse.swt.SWT;   
  4. import org.eclipse.swt.custom.CTabFolder;   
  5. import org.eclipse.swt.custom.CTabFolder2Adapter;   
  6. import org.eclipse.swt.custom.CTabFolderEvent;   
  7. import org.eclipse.swt.custom.CTabItem;   
  8. import org.eclipse.swt.graphics.Color;   
  9. import org.eclipse.swt.graphics.Image;   
  10. import org.eclipse.swt.layout.GridData;   
  11. import org.eclipse.swt.layout.GridLayout;   
  12. import org.eclipse.swt.widgets.Display;   
  13. import org.eclipse.swt.widgets.Shell;   
  14. import org.eclipse.swt.widgets.Text;   
  15.   
  16. public class CTabFolderSample   
  17. {   
  18.     public static void main(String[] args)   
  19.         {   
  20.             final Display display = Display.getDefault();   
  21.             final Shell shell = new Shell();   
  22.             shell.setSize(296255);   
  23.             shell.setText("CTabFolder 练习");   
  24.             shell.setLayout(new GridLayout());   
  25.             //   
  26.   
  27.             shell.open();   
  28.   
  29.             final CTabFolder tabFolder = new CTabFolder(shell, SWT.NONE|SWT.CLOSE|SWT.BORDER);   
  30.             tabFolder.addCTabFolder2Listener(new CTabFolder2Adapter() {   
  31.                 public void minimize(CTabFolderEvent event) {   
  32.                         tabFolder.setMinimized(true);   
  33.                         tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));   
  34.                         shell.layout(true);//刷新布局   
  35.                 }   
  36.                 public void maximize(CTabFolderEvent event) {   
  37.                         tabFolder.setMaximized(true);   
  38.                         tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));   
  39.                         shell.layout(true);   
  40.                 }   
  41.                 public void restore(CTabFolderEvent event) {   
  42.                         tabFolder.setMinimized(false);   
  43.                         tabFolder.setMaximized(false);   
  44.                         tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));   
  45.                         shell.layout(true);   
  46.                 }   
  47.             });   
  48.             //tabFolder.setBounds(0, 0, 283, 211);   
  49.             tabFolder.setTabHeight(20);   
  50.             tabFolder.marginHeight = 5;   
  51.             tabFolder.marginWidth = 5;   
  52.             tabFolder.setMaximizeVisible(true);   
  53.             tabFolder.setMinimizeVisible(true);   
  54.             tabFolder.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));   
  55.             //下面两个是设置固定的背景色和前景色   
  56.             //tabFolder.setBackground(display.getSystemColor(SWT.COLOR_BLUE));   
  57.             //tabFolder.setForeground(display.getSystemColor(SWT.COLOR_WHITE));   
  58.             //下面是设置渐变色   
  59.             Color[] color=new Color[4];   
  60.             color[0]=display.getSystemColor(SWT.COLOR_DARK_BLUE);   
  61.             color[1]=display.getSystemColor(SWT.COLOR_BLUE);   
  62.             color[2]=display.getSystemColor(SWT.COLOR_DARK_GRAY);   
  63.             color[3]=display.getSystemColor(SWT.COLOR_WHITE);   
  64.             int[] intArray=new int[]{25,45,100};   
  65.             tabFolder.setSelectionBackground(color, intArray);   
  66.             //这是设置了背景颜色,但是如果同时设置了背景图片的话以背景图片优先   
  67.             tabFolder.setSimple(false);//设置圆角   
  68.             tabFolder.setUnselectedCloseVisible(true);   
  69.             for (int i = 1; i < 4; i++) {   
  70.                 CTabItem item = new CTabItem(tabFolder, SWT.None|SWT.MULTI|SWT.V_SCROLL);   
  71.                 item.setText("选项卡" + i);   
  72.                 Text t = new Text(tabFolder, SWT.None|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL|SWT.WRAP);   
  73.                 t.setText("这是选项卡可以控制的文字" + i+"\n\n世界第一等\n\n一路顺风");   
  74.                 item.setControl(t);   
  75.   
  76.             }   
  77.             Image image=new Image(display,"C:\\Documents and Settings\\Administrator\\桌面\\label.jpg");   
  78.             shell.setImage(image);     
  79.             shell.setSize(300200);   
  80.             shell.layout();   
  81.             while (!shell.isDisposed()) {   
  82.                 if (!display.readAndDispatch())   
  83.                     display.sleep();   
  84.             }   
  85.         }   
  86.   
  87. }   

大小: 12.15 K 尺寸: 298 x 197 浏览: 56 次 点击打开新窗口浏览全图

大小: 12.91 K 尺寸: 302 x 197 浏览: 51 次 点击打开新窗口浏览全图

说明:

第一、状态说明,根据选项卡的状态右上角显示最大化还是最小化还是回复的按钮。设置最小化的方法: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);//布满右侧区域

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzj0470

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值