📄 stacklayout1.java
字号:
/**
* @作者:陈刚
* @Email:glchengang@yeah.net
* @Blog:http://blog.csdn.net/glchengang
*/
package swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class StackLayout1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
//------------------新插入的界面核心代码------------------------
shell.setLayout(new FillLayout()); //shell应用充满式布局
//因为comp1容器使用stackLayout布局,所以comp1中的两个Text框是重叠在一起的
final Composite comp1 = new Composite(shell, SWT.NONE);
final StackLayout stackLayout = new StackLayout();
comp1.setLayout(stackLayout);
//在comp1中创建两个Text框
final Text text1 = new Text(comp1, SWT.BORDER);
text1.setText("Text1");
final Text text2 = new Text(comp1, SWT.BORDER);
text2.setText("Text2");
//在一个新容器中建立两个按钮用来控制上面两个text框的显示
Composite comp2 = new Composite(shell, SWT.NONE);
comp2.setLayout(new RowLayout());
//按钮button1。它控制Text1的显示
Button button1 = new Button(comp2, SWT.NONE);
button1.setText("显示Text1");
button1.addSelectionListener(new SelectionAdapter() { //单击事件
public void widgetSelected(SelectionEvent e) {
/*******************************************************
* --------下面两句是StackLayout中控制的关键----------------
******************************************************/
stackLayout.topControl = text1; //将t1框显示
comp1.layout(); //将界面刷新一下,否则显示上会没任何反映的
}
});
//按钮button2。它控制Text2的显示
Button button2 = new Button(comp2, SWT.NONE);
button2.setText("显示Text2");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
stackLayout.topControl = text2;
comp1.layout();
}
});
//------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -