最近用swt/jface来做一个桌面小工具 。在写的时候遇到了点问题 就是点击菜单栏进行页面切换的问题。在网上搜了很久才找到一个可行的方法。就是使用swt的StackLayout布局方式,将父容器设为堆栈布局 在进行切换的时候 将要显示的子容器进行置顶显示。希望能给大家带来帮助!
没有jar包的自取,有重复的jar包 选版本最高的
链接:https://pan.baidu.com/s/1ls5Zz9w3eUsYMe6Su7KFDg
提取码:1y72
代码如下:
public class mainShell extends Shell {
public static void main(String[] args) {
Display display = Display.getDefault();
mainShell mainshell = new mainShell(display);//shell面板
mainshell.setSize(1510,800);//设置宽高
mainshell.setLayout(null);//清空布局样式
mainshell.open();
while (!mainshell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public mainShell(Display display) {//构造函数
final Composite composite = new Composite(this, SWT.NONE);//父容器
StackLayout stack = new StackLayout();// 创建堆栈布局
composite.setBounds(0, 0, 1500, 600);
composite.setLayout(stack);//将父容器设为stacklayout
Composite page = new Composite(composite,SWT.NONE);//此处是用来测试的子容器
page.setLayout(new RowLayout());
page.setBounds(10, 11, 190, 90);
Label label = new Label(page, SWT.NONE);
label.setText("Label on page 1");
label.pack();
Composite page1 = new Composite(composite,SWT.NONE);//此处是用来测试的子容器
page1.setLayout(new RowLayout());
page.setBounds(10, 11, 190, 90);
Label label1 = new Label(page1, SWT.NONE);
label1.setText("Label on page 2");
label1.pack();
// 菜单监听
Listener showListener = new Listener() {
public void handleEvent(Event event) {
Menu menu = (Menu) event.widget;
MenuItem item = menu.getParentItem();
if (item != null) {
// addListener(item.getText(), shell);
if ("执行(R)".equals(item.getText())) {
System.out.println(1);
} else if ("设置(S)".equals(item.getText())) {//点击设置菜单 打开设置面板
setComposite setcompo = new setComposite(composite, SWT.BORDER);
stack.topControl=setcompo;//在这里将设置面板置顶显示
composite.layout();//这一步很重要 不然旧容器内容不会隐藏 会和新容器的内容一块显示出来
} else if ("查看(L)".equals(item.getText())) {
stack.topControl=page1;
composite.layout();
} else if ("帮助(H)".equals(item.getText())) {
System.out.println(4);
}
}
}
};
Menu menu = new Menu(this, SWT.BAR);// 创建主菜单栏bar
this.setMenuBar(menu);
Menu fileMenu = new Menu(menu);// 创建菜单对象
MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);// 创建菜单子对象
fileItem.setText("文件(F)");// 设置菜单名称
fileItem.setMenu(fileMenu);// 将菜单子对象放入菜单容器
MenuItem runItem = new MenuItem(menu, SWT.CASCADE);
runItem.setText("执行(R)");
runItem.setMenu(fileMenu);
Menu runMenu = new Menu(this, SWT.DROP_DOWN);
runMenu.addListener(SWT.Show, showListener);//菜单监听
runItem.setMenu(runMenu);
MenuItem lookItem = new MenuItem(menu, SWT.CASCADE);
lookItem.setText("查看(L)");
lookItem.setMenu(fileMenu);
Menu lookMenu = new Menu(this, SWT.DROP_DOWN);
lookMenu.addListener(SWT.Show, showListener);
lookItem.setMenu(lookMenu);
MenuItem setItem = new MenuItem(menu, SWT.CASCADE);
setItem.setText("设置(S)");
setItem.setMenu(fileMenu);
setItem.setAccelerator(SWT.CTRL + 'S');
Menu setMenu = new Menu(this, SWT.DROP_DOWN);
setMenu.addListener(SWT.Show, showListener);
setItem.setMenu(setMenu);
MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
helpItem.setText("帮助(H)");
helpItem.setMenu(fileMenu);
Menu helpMenu = new Menu(this, SWT.DROP_DOWN);
helpMenu.addListener(SWT.Show, showListener);
helpItem.setMenu(helpMenu);
// 设置文件菜单的子菜单
Menu fileMenus = new Menu(this, SWT.DROP_DOWN);
fileItem.setMenu(fileMenus);
MenuItem newFileItem = new MenuItem(fileMenus, SWT.CASCADE);
newFileItem.setText("新建(&N)");
newFileItem.setAccelerator(SWT.CTRL + 'N');// 定义快捷键Ctrl+N
MenuItem saveFileItem = new MenuItem(fileMenus, SWT.CASCADE);
saveFileItem.setText("保存(S)");
}
@Override
protected void checkSubclass() {//因为继承了shell类 所以要加这个方法可以为空方法 不加的话可能也许会出现错误
// Disable the check that prevents subclassing of SWT components
}
}
设置面板类,代码如下:
public class setComposite extends Composite {
public setComposite(Composite parent, int style) {
super(parent, style);
// TODO Auto-generated constructor stub
this.setLayout(new FillLayout());
final TabFolder tabFolder = new TabFolder(this, SWT.BORDER);
tabFolder.setLayout(new FillLayout());
TabItem item1 = new TabItem(tabFolder, SWT.NONE);
item1.setText("装置参数");
Composite csCom = new Composite(tabFolder, SWT.NONE);
csCom.setBackgroundMode(SWT.COLOR_GRAY);
csCom.setLayout(new FillLayout());
Composite leftCom = new Composite(csCom,SWT.NONE);//左容器
leftCom.setLayout(null);//将左容器布局设为null
leftCom.setBounds(0, 0, 750, 500);
Label dzlabel = new Label(leftCom, SWT.NONE);//定值区号的标签
dzlabel.setText("定值区号");//赋值
dzlabel.setBounds(300, 105, 60, 30);//定值区号偏移量和宽高
Text dztext = new Text(leftCom,SWT.BORDER);//定值区号的文本框
dztext.setText("0");//赋值
dztext.setBounds(370, 100, 130, 30);//定值区号文本框的偏移量和宽高
Label bhlabel = new Label(leftCom, SWT.NONE);//保护装置地址的标签
bhlabel.setText("保护装置地址");
bhlabel.setBounds(270, 145, 100, 30);//保护装置地址的偏移量和宽高
Text bhtext = new Text(leftCom,SWT.BORDER);//保护装置地址的文本框
bhtext.setText("0");
bhtext.setBounds(370, 140, 130, 30);//保护装置地址文本框的偏移量和宽高
Label ckAlabel = new Label(leftCom, SWT.NONE);//串口A的标签
ckAlabel.setText("串口A波特率");
ckAlabel.setBounds(270, 185, 100, 30);//串口A的偏移量和宽高
Combo ckAtext=new Combo(leftCom,SWT.ABORT);//串口A的文本框
ckAtext.add("19200");
ckAtext.add("9600");
ckAtext.add("4800");
ckAtext.select(1);
ckAtext.setBounds(370, 180, 130, 30);//串口A文本框的偏移量和宽高
Label ckBlabel = new Label(leftCom, SWT.NONE);//串口B的标签
ckBlabel.setText("串口B波特率");
ckBlabel.setBounds(270, 225, 100, 30);//串口B的偏移量和宽高
Combo ckBtext = new Combo(leftCom,SWT.ABORT);//串口B的文本框
ckBtext.add("19200");
ckBtext.add("9600");
ckBtext.add("4800");
ckBtext.select(1);
ckBtext.setBounds(370, 220, 130, 30);//串口B文本框的偏移量和宽高
Label printlabel = new Label(leftCom, SWT.NONE);//打印波特率的标签
printlabel.setText("打印波特率");
printlabel.setBounds(280, 265, 90, 30);//打印波特率的偏移量和宽高
Combo printText = new Combo(leftCom,SWT.ABORT);//打印波特率的文本框
printText.add("19200");
printText.add("9600");
printText.add("4800");
printText.select(2);
printText.setBounds(370, 260, 130, 30);//打印波特率文本框的偏移量和宽高
Label tsLabel = new Label(leftCom, SWT.NONE);//调试波特率的标签
tsLabel.setText("调试波特率");
tsLabel.setBounds(280, 305, 90, 30);//调试波特率的偏移量和宽高
Combo tsText = new Combo(leftCom,SWT.ABORT);//调试波特率的文本框
tsText.add("19200");
tsText.add("9600");
tsText.add("4800");
tsText.select(2);
tsText.setBounds(370, 300, 130, 30);//调试波特率文本框的偏移量和宽高
Label xtLabel = new Label(leftCom, SWT.NONE);//系统频率的标签
xtLabel.setText("系统频率");
xtLabel.setBounds(290, 345, 80, 30);//系统频率的偏移量和宽高
Combo xtText = new Combo(leftCom,SWT.ABORT);//系统频率的文本框
xtText.add("50");
xtText.add("25");
xtText.add("10");
xtText.select(1);
xtText.setBounds(370, 340, 130, 30);//系统频率的偏移量和宽高
Label ULabel1 = new Label(leftCom, SWT.NONE);//电压一次额定值的标签
ULabel1.setText("电压一次额定值");
ULabel1.setBounds(250, 385, 105, 30);//电压一次额定值的偏移量和宽高
Text UText1 = new Text(leftCom,SWT.BORDER);//电压一次额定值的文本框
UText1.setText("0");
UText1.setBounds(370, 380, 130, 30);//电压一次额定值的偏移量和宽高
Label ULabel2 = new Label(leftCom, SWT.NONE);//电压二次额定值的标签
ULabel2.setText("电压二次额定值");
ULabel2.setBounds(250, 425, 105, 30);//电压二次额定值的偏移量和宽高
Text UText2 = new Text(leftCom,SWT.BORDER);//电压二次额定值的文本框
UText2.setText("0");
UText2.setBounds(370, 420, 130, 30);//电压二次额定值的偏移量和宽高
Label ALabel = new Label(leftCom, SWT.NONE);//电流一次额定值的标签
ALabel.setText("电流一次额定值");
ALabel.setBounds(250, 465, 105, 30);//电流一次额定值的偏移量和宽高
Text AText = new Text(leftCom,SWT.BORDER);//电流一次额定值的文本框
AText.setText("0");
AText.setBounds(370, 460, 130, 30);//电流一次额定值的偏移量和宽高
//右容器
Composite rightCom = new Composite(csCom,SWT.NONE);
rightCom.setLayout(null);
rightCom.setBounds(0, 0, 750, 500);
Label ALabel1 = new Label(rightCom, SWT.NONE);//电二一次额定值的标签
ALabel1.setText("电流二次额定值");
ALabel1.setBounds(250, 105, 105, 30);//电流二次额定值的偏移量和宽高
Text AText1 = new Text(rightCom,SWT.BORDER);//电流二次额定值的文本框
AText1.setText("0");
AText1.setBounds(370, 100, 130, 30);//电流二次额定值的偏移量和宽高
Label czLabel = new Label(rightCom, SWT.NONE);//厂站名称的标签
czLabel.setText("保护装置地址");
czLabel.setBounds(270, 145, 100, 30);//厂站名称的偏移量和宽高
Text czText = new Text(rightCom,SWT.BORDER);//厂站名称的文本框
czText.setText("0");
czText.setBounds(370, 140, 130, 30);//厂站名称文本框的偏移量和宽高
Label dyLabel = new Label(rightCom, SWT.NONE);//打印方式的标签
dyLabel.setText("串口A波特率");
dyLabel.setBounds(270, 185, 100, 30);//打印方式的偏移量和宽高
Text dyText = new Text(rightCom,SWT.BORDER);//打印方式的文本框
dyText.setText("0");
dyText.setBounds(370, 180, 130, 30);//打印方式文本框的偏移量和宽高
Label zdLabel = new Label(rightCom, SWT.NONE);//自动打印的标签
zdLabel.setText("串口B波特率");
zdLabel.setBounds(270, 225, 100, 30);//自动打印的偏移量和宽高
Text zdText = new Text(rightCom,SWT.BORDER);//自动打印的文本框
zdText.setText("0");
zdText.setBounds(370, 220, 130, 30);//自动打印文本框的偏移量和宽高
Label gyLabel = new Label(rightCom, SWT.NONE);//规约类型的标签
gyLabel.setText("打印波特率");
gyLabel.setBounds(280, 265, 90, 30);//规约类型的偏移量和宽高
Text gyText = new Text(rightCom,SWT.BORDER);//规约类型的文本框
gyText.setText("0");
gyText.setBounds(370, 260, 130, 30);//规约类型文本框的偏移量和宽高
Label fmLabel = new Label(rightCom, SWT.NONE);//分脉对冲的标签
fmLabel.setText("调试波特率");
fmLabel.setBounds(280, 305, 90, 30);//分脉对冲的偏移量和宽高
Text fmText = new Text(rightCom,SWT.BORDER);//分脉对冲的文本框
fmText.setText("0");
fmText.setBounds(370, 300, 130, 30);//分脉对冲文本框的偏移量和宽高
Label yfLabel = new Label(rightCom, SWT.NONE);//远方修改定值率的标签
yfLabel.setText("系统频率");
yfLabel.setBounds(290, 345, 80, 30);//远方修改定值的偏移量和宽高
Text yfText = new Text(rightCom,SWT.BORDER);//远方修改定值的文本框
yfText.setText("0");
yfText.setBounds(370, 340, 130, 30);//远方修改定值的偏移量和宽高
Label infLabel = new Label(rightCom, SWT.NONE);//103规约额定值的标签
infLabel.setText("电压一次额定值");
infLabel.setBounds(250, 385, 105, 30);//103规约的偏移量和宽高
Text infText = new Text(rightCom,SWT.BORDER);//103规约的文本框
infText.setText("0");
infText.setBounds(370, 380, 130, 30);//103规约的偏移量和宽高
/*
* Composite footer = new Composite(this,SWT.NONE); footer.setLayout(new
* FillLayout()); Button btn = new Button(footer, SWT.PUSH);
* btn.setText("打印定值清单"); btn.setBounds(0, 0, 100, 100);
*/
item1.setControl(csCom);//将csCom容器放入tab标签TabFolder中
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
item2.setText("保护定值");
Text button2 = new Text(tabFolder, SWT.PUSH);
button2.setText("Page 2");
item2.setControl(button2);
TabItem item3 = new TabItem(tabFolder, SWT.NONE);
item3.setText("软压板");
Button button3 = new Button(tabFolder, SWT.PUSH);
button3.setText("Page 3");
item3.setControl(button3);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}