件_控_本_基

Shell:带有标题栏,最大化,最小化,关闭按钮的窗口
Button:普通按钮:push
单选按钮: radio
复选框: check

Text:文本框:单行,多行,滚动行
样式:SWT.BORDER:下陷
SWT.PASSWORD:密码
SWT.READ_ONLY:只读
SWT.NONE:无样式
SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL:多行文本框样式
SWT.WRAP | SWT.V_SCROLL | SWT.BORDER
//只准输数字
Text text4 = new Text(parent, SWT.BORDER);
text4.setText("123");
text4.addVerifyListener(new VerifyListener()
{
@Override
public void verifyText(VerifyEvent e)
{
try
{
Integer.parseInt(e.text);
}
catch (Exception ex)
{
e.doit = false;
}
}
});
//将用户输入编程大写
Text text4 = new Text(parent, SWT.BORDER);
text4.setText("abc");
text4.addVerifyListener(new VerifyListener()
{
@Override
public void verifyText(VerifyEvent e)
{
e.text = e.text.toUpperCase();
}
});

************************基本控件***********************************************
Combo:下拉列表框
Group:分组框

--------------------------------------------------------------------------------------------
Label:标签:显示文字或图片
Label:若指定为SWT.seperator则显示为一个分隔线,组合SWT.Horizonal或SWT.Vertical则可以显示水平或垂直线。


--------------------------------------------------------------------------------------------
Button常用的方法:

setImage(); //设置按钮图片
setText(); //设置按钮文字
setBounds(); //设置按钮位置
addSelectionListener(); //添加监听事件
SWT.RADIO(单选框),SWT.CHECK(复选框),SWT.NONE(普通按钮),SWT.PUSH(自动弹回),SWT.TOGGLE(不能自动弹回)


--------------------------------------------------------------------------------------------
Table:显示二维表格,可以在列头指定列名
List:能够进行一览显示,可以从一览中指定任意一行或几行
Tree:可以显示文件路径等带有亲子关系的要素
ProgressBar:显示进度条


--------------------------------------------------------------------------------------------
Menu:在Menu bar中显示Menu】,[help]等
Menu/MenuItem:一个窗体只支持一个菜单,用Shell.setMenuBar()设置菜单。
Menu bar=new Menu(shell,SWT.BAR);
shell.setMenuBar(bar);
MenuItem item=new MenuItem(bar,SWT.NONE);//添加菜单项
...
Menu fileMenu=new Menu(item);//添加菜单
item.setMenu(fileMenu)
...
反复实现多级菜单
样式有CASCAD、PUSGH、CHECK、RADIO、SEPERATOR样式
响应Selection和Arm、Help事件
一个下拉菜单由一个使用了CASCADE的菜单项和Menu实例组成


--------------------------------------------------------------------------------------------
ToolBar:显示工具栏
CoolBar:显示可以使用鼠标拖动的工具栏
Composite
Canvas:显示任意表格
TabFolder:通过Tab换页
SashForm:把area一分为二,可以利用鼠标移动边界
Slider:

MessageBox:显示Message
FileDialog:指定文件
DirectoryDialog:指定路径
ColorDialog:指定颜色
FontDialog:指定字体
PrintDialog:指定打印机


--------------------------------------------------------------------------------------------
容器composite、shell
Composite:最基本的容器类,可以嵌套Composite。
composite.setBackground(): 设置容器背景色
composite.setbackgroundImage(): 设置背景图片

setBackgroundMode()设置背景模式,
SWT.Inherite_none(指定子控件不继承背景色设置),
SWT.Inherite_default(只有也指定了default的子控件才会继承),
SWT.Inherite_force(强制继承),

setCursor();设置光标
Cursor cursor=new Cursor(display,SWT>CURSOR_CROSS);
composite.setcursor(cursor);
...
cursor.dispose();
图片鼠标:Image得到ImageData,然后cursor=new Cursor(display,imageData,0,0),最后两个参数指定了图片上箭头端点位置。

Shell样式:SHELL_TRIM(默认)样式有最大最小关闭按钮,可调尺寸,由CLOSE\TITLE\MIN\MAX\RESIZE\Border组成。resize样式没有标题栏,可调尺寸,close样式只有关闭按钮,不能调尺寸。ON_Top保证最前端,No_trim去掉所有修饰,

Shell的模态:MODELESS,PRIMARY_MODEL,APPLICATION_MODEL,SYSTEM_MODEL。modeless不对任何消息进行阻截,primary_model子窗口阻断所有发送到其父窗口的消息,若不关掉子窗口则无法对父窗口操作,application_mode子窗口阻断与其共享一个display的窗口,若为单display则实际阻断了整个应用程序,system_model阻断所有桌面应用程序,最好不要用。

shell支持5种事件:
activate(成为活动窗口时),
close,deactivate(变成非活动),
deiconify(从最小化恢复),
iconify(最小化),采用ShellListener来监听所有上述事件。

为容器添加上下文菜单
Menu menu=new Menu(shell, SWT.POP_UP);
container.setMenu(menu);


************************基本事件***********************************************
addSelectionListener:
widgetSelect:组件被选择(单击鼠标,按回车键)时出发此方法的处理程序
widgetDefaultSelected:文本框回车事件,列表框双击事件

addKeyListener:按键
keyPressed:焦点停在组件上,按任何键触发(按钮)
keyReleased:按键弹起时触发

addFocusListener:焦点
focusGained:聚焦时触发
focusLost:失焦时触发

addMouseListener:鼠标
mouseDown:鼠标按下时触发
mouseUp:鼠标放开时触发
mouseDoubleClick:鼠标双击时触发

addMouseMoveListener:鼠标移动事件

ControlListener:
controlMoved():控件的位置改变时
controlResized():控件的大小改变时

KeyListener:
keyPressed():按键按下时
keyReleased():按键释放时
menuHidden():显示菜单时
menuShown():隐藏菜单时

ModifyListener:
modifyText:文本被修改过时

MouseTrackListener:
mouseEnter():鼠标进入控件区域时
mouseExit():鼠标离开控件区域时
mouseHover():鼠标在该空间区域时

DisposeListener:监听控件被销毁的事件

ShellListener:
shellActivated():窗口被激活时
shellClosed():窗口关闭时
shellDeactivated():窗口为非激活状态时
shellDeiconified():当窗口不是最小化时
shellIconified():当窗口最小化时

TraverseListener:
keyTraversed():按下Tab键切换时触发该事件

TreeListener:
treeCollapsed():折叠树节点
treeExpanded():展开树节点
verifyText():改变文本时触发该事件


***********************键盘事件***********************************************

上 SWT.ARROW_DOWN
下 SWT.ARROW_UP
左 SWT.ARROW_LEFT
右 SWT.ARROW_RIGHT
Alt SWT.ALT
空格键 SWT.BS
Enter键 SWT.CR
Ctrl SWT.CTRL
End键 SWT.END
Esc键 SWT.ESC
F1-F12键 SWT.F1 - SWT.F12
Home键 SWT.HOME
Insert键 SWT.INSERT
Page Down键 SWT.PAGE_DOWN
page UP键 SWT.PAGE_UP
Shift键 SWT.SHIFT
Tab键 SWT.TAB
换行键 SWT.LF

***********************SWT主要布局管理方式***********************************************


FillLayout:充满式布局管理器,组件的大小会尽量充满容器
RowLayout:行列式布局管理器,简单的排列组件
GridLayout:网格式布局管理器,非常强大,可以胜任所有布局
FormLayout:表格式布局管理器,通过组件各个边的距离来布局组件,和上一样强大
StackLayout:堆栈式布局管理器,像书页一样,只显示最前面的组件

layoutData(布局管理数据): 控制布局属性的数据:一个容器控件的setLayout设置的是它对它所包含的子控件的布局管理方式。
layout (布局管理器) : 布局管理方式:一个控件(容器或者非容器控件)setLayoutData 设置的是控件本身在包含它的布局管理器的中的布局属性

***********************控件属性**********************************************************
网格式布局

//表格布局方式
GridLayout gridLayout = new GridLayout();
//GridLayout gridLayout = new GridLayout(1,false); //列数,列宽是否相同

gridLayout.makeColumnsEqualWidth = true; //设置所有列为相同宽度
gridLayout.marginWidth = 100; //设置网格左边缘与右边缘之间的像素数目
gridLayout.marginHeigth = 100; //设置网格顶部与底部之间的像素数目
gridLayout.numColumns = 2; //设置网格的列数为2
gridLayout.horizontalSpacing = 50; //左右两列之间的距离
gridLayout.verticalSpacing = 50; //上下两行之间的间距

//设置控件样式
//gridData = new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 3);
//SWT.TOP:水平方向的位置 ,垂直方向的位置,false:控件的尺寸不随容器变化,跨列,跨行

gridData = new GridData(); //定义样式对象
gridData.horizontalSpan = 2; //跨2列
gridData.verticalSpan = 2; //跨2行

gridData.horizontalAlignment = SWT.FILL; //水平方向如何对齐 取值:SWT.beginning, SWT.center, SWT.end, SWT.fill
gridData.verticalAlignment = SWT.FILL; //垂直方向如何对齐 取值:SWT.top, SWT.center, SWT.bottom, SWT.fill

gridData.grabExcessHorizontalSpace = true;//是否占用水平方向的剩余空间
gridData.grabExcessVerticalSpace = true; //是否占用垂直方向的剩余空间

gridData.minimumWidth = 500; //设置控件的宽度
gridData.minimumHeight = 50; //设置控件的高度
gridData.widthHint = 500; //设置控件的宽度 --- 控件随容器变化
gridData.heightHint = 50; //设置控件的高度 --- 控件随容器变化

SWT.BORDER 边界
SWT.MULTI 多个
SWT.V_SCROLL 是否可以多行
SWT.H_SCROLL 是否可以多列


------------------------
1. ARROW, CHECK, PUSH, TADIO, TOGGLE
2. LEFT, RIGHT, CENTER
3. UP, DOWN, LEFT, RIGHT
4. BORDER
5. LEFT_TO_RIGHT, RIGHT_TO_LEFT
每组选一种


--------------------------------------------------------------------------------------
填充式布局

FillLayout:所有子控件从上到下,从左到右,平均排列,不允许指定子空间大小

Filllayout layout = new Filllayout();

layout.spacing=10; //间距
layout.marginWidth=10; //左右边距
layout.margin_height=10; //上下边距

shell.setLayout(layout);
shell.layout();<开始布局>


SWT.Horizontal
SWT.Vertical


----------------------------------------------------------------------------------------
行布局

RowLayout:排同一行或列,但运行为每个子控件指定宽度和高度,若一行排不下会自动换行,行的宽度取所有容器高度的最大值。


***********************盒子模型**********************************************************
每个控件被分为9个部分

beginging center end
----------------------------------
top | | | |
----------------------------------
center | | 控件 | |
----------------------------------
bottom | | | |
----------------------------------

设置控件的颜色:
Label a = new Label(parent, SWT.NONE);
a.setText("a");
a.setBounds(0,0,100,100);
a.setBackground(new Color(Display.getDefault(), 255, 0, 0)); //设置背景色:红色
a.setForeground(new Color(Display.getDefault(), 255, 255, 255));//设置前景色:白色
a.setForeground(new Color(Display.getDefault(), 255, 0, 0)); //字体颜色
//Color color = display.getSystemColor(SWT.COLOR_BLACK);
黑色: 0,0,0
黄色:255, 255, 0
粉红:255, 155,250

----------------------------------------------------------------------------------------------
设置控件颜色透明
Button radioPaseOne = new Button(parent, SWT.RADIO);
radioPaseOne.setText(Messages.ESB_parseRadioType_0);
//设置背景色:没有颜色
parent.setBackgroundMode(SWT.NO_BACKGROUND);

----------------------------------------------------------------------------------------------
设置控件的图标:
Button a = new Button(parent, SWT.NONE);
a.setBounds(0, 1000, 100, 100);
a.setImage(new Image(Display.getDefault(), "F:\\business.gif"));
//aaaButton.setImage(Display.getDefault().getSystemImage(SWT.ICON_ERROR));//使用工具自带的图片
a.setText("Image Button");

设置控件的字体大小,样式:
Label a = new Label(parent, SWT.NONE);
a.setText("刘小彬");
a.setFont(new Font(Display.getDefault(), "Tahoma", 50, SWT.BOLD));
//"Arial" ,SWT.ITALIC:斜体 SWT.BOLD:加粗

用标签来实现图片展示
Label label = new Label(parent, SWT.NONE);
label.setImage(Display.getDefault().getSystemImage(SWT.ICON_QUESTION));
//SWT.ICON_QUESTION:错误信息
SWT.ICON_WARNING:警告信息
SWT.ICON_INFORMATION:提示信息
SWT.ICON_WORKING:正在进行的工作消息
SWT.ICON_QUESTION:问题


用标签来实现分割线
Label label1 = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
label1.setBounds(0, 1000, 1000, 20);

Label label2 = new Label(parent, SWT.SEPARATOR | SWT.VERTICAL);
label2.setBounds(0, 1100, 20, 1000);

***********************按钮的样式***********************************************

按钮的样式
SWT.PUSH:平通按钮,
SWT.CHECK:单击按钮下陷,在单击
SWT.MAX:窗体可最大化可小化
SWT.RESIZE:窗体可大可小

按钮上的文字
SWT.OK:确定
SWT.OK|SWT.CANCEL: 确定 取消
SWT.YES|SWT.NO 是 否
SWT.YES|SWT.NO|SWT.CANCEL 是 否 取消
SWT.RETRY|SWT.CANCEL 重试 取消
SWT.ABORT|SWT.RETRY|SWT.IGNORE 终止 重试 取消


***********************菜单的样式***********************************************

菜单Menu
SWT.BAR 用于主菜单
SWT.DROP_DOWN 用于子菜单
SWT.POP_UP 用于右键菜单
菜单项的样式:
SWT.CHECK 检查样式
SWT.CASCADE 级联样式
SWT.PUSH 普通型样式
SWT.RADIO 圆点样式
SWT.SEPARATOR 菜单项之间的分隔符


***********************消息提示框***********************************************
消息框图标样式
错误提示图标:SWT.ICON.ERROR
信息提示图标:SWT.IOCN.INFORMATION
询问提示图标:QUESTION
警告提示图标:WARNING

1.MessageDialog.openInforMation(shell,"标题","提示信息");
MessageDialog.openInformation(null, "aaa", "长度超标???");
2.boolean b = MessageDialog.openConfim(shell,"标题","提示信息");
if(b){
System.out.println("您单击了“确定”按钮");
}else{
System.out.println("您单击了“取消“按钮");
}
3.boolean b = MessageDialog.Question(shell,"标题","提示信息");
if(b){
System.out.println("您单击了“确定”按钮");
}else{
System.out.println("您单击了“取消“按钮");
}
4.MessageDialog.openError(shell,"标题","提示信息");
5.MessageDialog.openWarning(shell,"标题","提示信息");



<extension point="org.eclipse.ui.preferencePages">
<page
name="myplugin2 插件"
class="cn.com.liuxiaobin.preferences.RootPreferencePage"
id="cn.com.liuxiaobin.preferences.RootPreferencePage">
</page>
<page
name="DB 数据库"
category="cn.com.liuxiaobin.preferences.RootPreferencePage"
class="cn.com.liuxiaobin.preferences.DBPreferencePage"
id="cn.com.liuxiaobin.preferences.DBPreferencePage">
</page>
</extension>
************************基础语法***********************************************

/*******************************************************************************
* Copyright(c) 2005-2009 Huawei Tech. Co., Ltd.
* All rights reserved.
*
* Author: xbliuc
* Date : 2010-10-21
*******************************************************************************/
package com.huawei.esb.ui.properties;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

/**
* TODO 添加类注释
*/
public class test
{
public static void main(String[] args)
{
//Display:负责管理事件循环和控制UI线程和其他线程之间的通信
final Display display = Display.getDefault();
final Shell shell = new Shell(); //程序的入口
shell.setSize(500, 500); //设置窗体的大小
shell.setText("登陆界面"); //设置窗体的标题

//----------分组框代码----------------------------
Group group = new Group(shell, SWT.NONE);
group.setText("个人信息");
group.setBounds(10, 300, 100, 50);

//----------选项卡代码----------------------------
TabFolder tabfolder = new TabFolder(shell, SWT.NONE);
tabfolder.setBounds(6, 5, 168, 118);
//第一个选项页
final TabItem tabltem1 = new TabItem(tabfolder, SWT.NONE);
tabltem1.setText("个人信息");
{
//分组框信息
final Group group1 = new Group(tabfolder, SWT.NONE);
group1.setText("个人详细");
tabltem1.setControl(group1);
//单选按钮 --1
Button button5 = new Button(group1, SWT.RADIO);
button5.setBounds(30, 20, 50, 20);
button5.setText("中国");
//单选按钮 --2
Button button6 = new Button(group1, SWT.RADIO);
button6.setBounds(30, 50, 50, 20);
button6.setText("美国");
}
//第二个选项页
final TabItem tabltem2 = new TabItem(tabfolder, SWT.NONE);
tabltem2.setText("公司信息");
{
Composite comp = new Composite(tabfolder, SWT.NONE);
tabltem2.setControl(comp);
//文本框信息 -1
Text text8 = new Text(comp, SWT.BORDER);
text8.setText("123");
text8.setBounds(20, 10, 100, 20);
//文本框信息 -2
Text text9 = new Text(comp, SWT.BORDER);
text9.setText("321");
text9.setBounds(20, 50, 100, 20);
}

//----------分割栏代码----------------------------
SashForm sashform = new SashForm(shell, SWT.NONE); //定义一个分隔栏容器
Text text_1 = new Text(sashform, SWT.BORDER);
text_1.setText("左");
new Text(sashform, SWT.BORDER).setText("中");
new Text(sashform, SWT.BORDER).setText("右");
sashform.setWeights(new int[] {1, 2, 3});
sashform.setBounds(420, 20, 200, 200);

//----------带滚动条的面板----------------------------
//创建一个样式为深陷,带水平滚动条,垂直滚动条
ScrolledComposite scrolledComposite = new ScrolledComposite(shell,
SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setBounds(380, 260, 256, 148);
//创建一个面板
Composite composite = new Composite(scrolledComposite, SWT.NONE);
composite.setSize(300, 160);
scrolledComposite.setContent(composite);
//建立八个文本框
new Text(composite, SWT.BORDER).setBounds(10, 10, 60, 20);
new Text(composite, SWT.BORDER).setBounds(90, 10, 60, 20);
new Text(composite, SWT.BORDER).setBounds(10, 50, 60, 20);
new Text(composite, SWT.BORDER).setBounds(90, 50, 60, 20);
new Text(composite, SWT.BORDER).setBounds(10, 90, 60, 20);
new Text(composite, SWT.BORDER).setBounds(90, 90, 60, 20);
new Text(composite, SWT.BORDER).setBounds(10, 130, 60, 20);
new Text(composite, SWT.BORDER).setBounds(90, 130, 60, 20);

//----------标签代码----------------------------
Label label1 = new Label(shell, SWT.NONE);
label1.setText(" 用 户 注 册 ");
label1.setBounds(200, 50, 100, 20);

Label label = new Label(shell, SWT.NONE);
label.setText("用户姓名:");
label.setBounds(150, 80, 55, 20);

Label labe2 = new Label(shell, SWT.NONE);
labe2.setText("用户密码:");
labe2.setBounds(150, 110, 55, 20);

Label labe3 = new Label(shell, SWT.NONE);
labe3.setText("性别:");
labe3.setBounds(150, 135, 30, 20);

Label labe4 = new Label(shell, SWT.NONE);
labe4.setText("爱好:");
labe4.setBounds(150, 165, 30, 20);

Label labe5 = new Label(shell, SWT.NONE);
labe5.setText("年龄:");
labe5.setBounds(150, 195, 30, 20);

Label labe6 = new Label(shell, SWT.NONE);
labe6.setText("协议:");
labe6.setBounds(150, 225, 30, 20);

//----------文本框代码----------------------------
Text text = new Text(shell, SWT.BORDER);//新建一个Text对象
text.setText("Hello Word"); //设置初始文字
text.setBounds(220, 78, 100, 20); //设置空间x,y坐标,宽度,高度
text.addMouseListener(new MyMouseDoubleClick());

Text text1 = new Text(shell, SWT.BORDER);//新建一个Text对象
text1.setText("Hello Word"); //设置初始文字
text1.setBounds(220, 105, 100, 20); //设置空间x,y坐标,宽度,高度
text1.addMouseListener(new MouseDown());

//----------单选按钮代码----------------------------
Button button1 = new Button(shell, SWT.RADIO);
button1.setText("男");
button1.setBounds(224, 134, 50, 20);
Button button6 = new Button(shell, SWT.RADIO);
button6.setText("女");
button6.setBounds(284, 134, 50, 20);

//----------复选框代码----------------------------
Button button2 = new Button(shell, SWT.CHECK);
button2.setText("编码");
button2.setBounds(215, 165, 45, 20);

Button button3 = new Button(shell, SWT.CHECK);
button3.setText("游戏");
button3.setBounds(275, 165, 45, 20);

Button button4 = new Button(shell, SWT.CHECK);
button4.setText("影音");
button4.setBounds(335, 165, 45, 20);

//----------下拉列表代码----------------------------
Combo combo = new Combo(shell, SWT.READ_ONLY);
combo.setBounds(215, 190, 100, 25);
combo.removeAll(); //清空值
for (int i = 1; i <= 100; i++)
{
combo.add("" + i); //添加值
}
combo.select(0); //设置第一项为当前项

//----------列表框代码----------------------------
List list = new List(shell, SWT.READ_ONLY);
list.setBounds(215, 220, 150, 60);
list.removeAll();
for (int i = 1; i <= 10; i++)
{
list.add("" + i);
}
list.getSelection(); //默认选中第一行

//----------按钮代码----------------------------
Button button9 = new Button(shell, SWT.NONE);
button9.setText("确定");
button9.setBounds(180, 290, 50, 20);
button9.addMouseListener(new MyMouseDoubleClick());

Button button8 = new Button(shell, SWT.NONE);
button8.setText("取消");
button8.setBounds(260, 290, 50, 20);
button8.addMouseListener(new MyMouseDoubleClick());

shell.layout(); //窗体应用界面布置
shell.open(); //打开Shell主窗体
while (!shell.isDisposed())
{ //如果主窗体没有关闭,一直循环
if (!display.readAndDispatch())
{ //如果display不忙
display.sleep(); //休眠
}
}
}

//双击事件
private static final class MyMouseDoubleClick extends MouseAdapter
{
public void mouseDoubleClick(MouseEvent e)
{
MessageDialog.openInformation(null, "系统提示", "双击,乱点啥子啊???");
}
}

//单击事件
private static final class MouseDown extends MouseAdapter
{
public void mouseDown(MouseEvent e)
{
MessageDialog.openInformation(null, "系统提示", "单击,乱点啥子啊???");
}
}
}

=============消息框,打开文件夹,打开文件======================================================================

public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell(); //程序的入口
shell.setSize(500,500); //设置窗体的大小
shell.setText("刘小彬"); //设置窗体的标题

//提示消息框
Button bt = new Button(shell,SWT.NONE); //按钮
bt.setText("消息提示框");
bt.setBounds(100, 100, 100, 20);
//监听事件
bt.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
MessageBox b = new MessageBox(shell,SWT.ICON_WARNING |SWT.YES | SWT.NO);
b.setText("删除");
b.setMessage("您确定要删除文件吗?");
int chioc = b.open();
if(chioc == SWT.YES){
System.out.println("删除成功!!!");
}else{
System.out.println("未删除!!!");
}
}
});

//打开文件夹
Button bb = new Button(shell,SWT.NONE); //按钮
bb.setText("选择文件夹");
bb.setBounds(100, 150, 100, 20);
//监听事件
bb.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
DirectoryDialog dialog = new DirectoryDialog(shell);
dialog.setMessage("请选择要保存的文件夹");
dialog.setText("选择文件夹目录");
dialog.setFilterPath("C:\\"); //设置默认文件夹
String saveFile = dialog.open();
if(saveFile != null){
File directory = new File(saveFile);
System.out.println(directory.getPath());
}

}
});

//打开文件
Button bn = new Button(shell,SWT.NONE); //按钮
bn.setText("文件对话框");
bn.setBounds(100, 200, 100, 20);
//监听事件
bn.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
FileDialog dialog = new FileDialog(shell);
dialog.setFilterPath(System.getProperty("java.home"));
dialog.setFilterExtensions(new String[]{"*.txt","*."});
dialog.setFilterNames(new String[]{"Text Files(*.txt)","All Files(*.*)"});
String file = dialog.open();
if(file != null){
System.out.println(file);
}

}
});

shell.layout(); //窗体应用界面布置
shell.open(); //打开Shell主窗体
while(!shell.isDisposed()){ //如果主窗体没有关闭,一直循环
if(!display.readAndDispatch()){ //如果display不忙
display.sleep(); //休眠
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值