📄 qq.java
字号:
package cn.com.chengang.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
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.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class QQ {
private StackLayout stackLayout = new StackLayout();
private Composite yourDataComp;
private Composite otherComp;
private List selectList;
private Composite rightComp;
private Image qqImage = new Image(null, "icons/qq.jpg");;
private Image moonImage = new Image(null, "icons/moon.jpg");
private Image starImage = new Image(null, "icons/star.jpg");
public static void main(String[] args) {
new QQ().open();
}
public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(550, 350);
shell.setText("个人设置");
shell.setLayout(new GridLayout());
// ------分割窗口------
SashForm sashForm = new SashForm(shell, SWT.BORDER);
sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
selectList = new List(sashForm, SWT.BORDER);// 分割窗左边的列表
selectList.setItems(new String[] { "个人资料", "联系方式" });
selectList.addSelectionListener(new MySelectionListener());// 加选择监听器
rightComp = new Composite(sashForm, SWT.NONE);// 右边的堆栈式容器
rightComp.setLayout(stackLayout);
// 共两页。将生成面板的代码提出成自定义方法,保证代码结构的清晰
yourDataComp = createYourDataComp(rightComp);// “个人资料”面板
otherComp = createOtherComp(rightComp);// “联系方式”面板
stackLayout.topControl = yourDataComp;// 在堆栈布局上先显示“个人资料”面板
sashForm.setWeights(new int[] { 1, 4 });// 分割窗口的左右空间比例
// ------底部的按钮组面板------
Composite buttonComp = new Composite(shell, SWT.BORDER);
// 用GridData使按钮组面板向其父容器Shell的右边界对齐
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.END;
buttonComp.setLayoutData(gridData);
// 设定按钮组面板内按钮为行列式布局,按钮间隔15像素
RowLayout rowLayout = new RowLayout();
rowLayout.spacing = 15;
buttonComp.setLayout(rowLayout);
// 在buttonComp下建立三个按钮,用全角空格撑开按钮
new Button(buttonComp, SWT.NONE).setText(" 确定 ");
new Button(buttonComp, SWT.NONE).setText(" 取消 ");
new Button(buttonComp, SWT.NONE).setText(" 应用 ");
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
// “个人资料”面板的生成方法
private Composite createYourDataComp(Composite rightComp) {
Composite comp = new Composite(rightComp, SWT.NONE);
comp.setLayout(new GridLayout(6, false));// 面板空间分成6列
new Label(comp, SWT.NONE).setText("用户号码:");
Text numberText = new Text(comp, SWT.READ_ONLY | SWT.BORDER);
// 水平抢占式充满,并占用三列的空间. createGridData是自定义方法
numberText.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 3));
Composite photoComp = new Composite(comp, SWT.BORDER);// 图片面板
// 水平和垂直的对齐式充满,横占两列,竖占4行
photoComp.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL, 2, 4));
photoComp.setLayout(new GridLayout(2, false));// 面板空间分2列
createImageComp(photoComp, qqImage);
// 选择图片的箭头型按钮,设置它向下对齐
Button photoButton = new Button(photoComp, SWT.ARROW | SWT.DOWN);
photoButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
// “升级成为会员”按钮,横占photoComp的两列,并横向对齐充满
Button updateButton = new Button(photoComp, SWT.NONE);
updateButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 2));
updateButton.setText("升级成为会员");
new Label(comp, SWT.NONE).setText("用户昵称:");
Text nickText = new Text(comp, SWT.BORDER);
nickText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
new Label(comp, SWT.NONE).setText("个性签名:");
Text descText = new Text(comp, SWT.BORDER);
descText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
new Label(comp, SWT.NONE).setText("等 级:");
Composite rankComp = new Composite(comp, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
gridData.heightHint = 20;// Composite默认的高度太高,故手工设定高度为20像素
rankComp.setLayoutData(gridData);
rankComp.setLayout(new RowLayout());
createImageComp(rankComp, moonImage);
createImageComp(rankComp, starImage);
createImageComp(rankComp, starImage);
createImageComp(rankComp, moonImage);
new Label(comp, SWT.NONE).setText("性 别:");
new Combo(comp, SWT.NONE);
new Label(comp, SWT.NONE).setText("姓名:");
Text nameText = new Text(comp, SWT.BORDER);
nameText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(comp, SWT.NONE).setText("年龄:");
Text oldText = new Text(comp, SWT.BORDER);
oldText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(comp, SWT.NONE).setText("毕业院校:");
Text schoolText = new Text(comp, SWT.BORDER);
schoolText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
new Label(comp, SWT.NONE).setText("生肖:");
Combo animalCombo = new Combo(comp, SWT.NONE);
animalCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
new Label(comp, SWT.NONE).setText("职业:");
Text jobText = new Text(comp, SWT.BORDER);
jobText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
new Label(comp, SWT.NONE).setText("星座:");
Combo constellationCombo = new Combo(comp, SWT.NONE);
constellationCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
// 把 "个人说明"标签由默认的居中,改为顶端对齐
Label introLabel = new Label(comp, SWT.NONE);
introLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
introLabel.setText("个人说明:");
Text introText = new Text(comp, SWT.BORDER | SWT.WRAP);// WRAP自动换行
introText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL, 5));
return comp;// 返回个人资料面板composite
}
// 生成一个简单的“联系方式”面板
private Composite createOtherComp(Composite rightComp) {
Composite comp = new Composite(rightComp, SWT.NONE);
comp.setLayout(new FillLayout());
new Label(comp, SWT.NONE).setText("联系方式面板");
return comp;
}
// 生成GridData对象的重复代码太多,写成一个方法可以减少程序的行数,用起来也方便些
private GridData createGridData(int style, int horizontalSpan) {
GridData gridData = new GridData(style);
gridData.horizontalSpan = horizontalSpan;
return gridData;
}
private GridData createGridData(int style, int horizontalSpan, int verticalSpan) {
GridData gridData = new GridData(style);
gridData.horizontalSpan = horizontalSpan;
gridData.verticalSpan = verticalSpan;
return gridData;
}
// 返回一个用来显示image的面板
private Composite createImageComp(Composite parnet, Image image) {
Composite c = new Composite(parnet, SWT.NONE);
c.setBackgroundImage(image);
// 根据图片的大小,用专用布局数据类来设定面板大小
ImageData imageData = image.getImageData();
int width = imageData.width;
int height = imageData.height;
Layout parnetLayout = parnet.getLayout();
if (parnetLayout instanceof GridLayout)
c.setLayoutData(new GridData(width, height));
else if (parnetLayout instanceof RowLayout)
c.setLayoutData(new RowData(width, height));
else if (parnetLayout instanceof FormLayout)
c.setLayoutData(new FormData(width, height));
return c;
}
// 选择监听器,采用事件的命名内部类的写法
private class MySelectionListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent e) {
// 得到列表被选项的序号,然后再判断显示哪个面板
if (selectList.getSelectionIndex() == 0)
stackLayout.topControl = yourDataComp;
else
stackLayout.topControl = otherComp;
rightComp.layout();// 刷新堆栈式布局的顶容器
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -