?? 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 + -