Liny_@NotePad

沉迷ACG中

一个简陋的SWT个人资料界面 = =..

YOYO posted @ 2009年7月19日 20:28 in 【Java SE】 with tags SWT , 3932 阅读

在SWT的教程上看到的一个练习~于是照着敲……截图如下: 

  1. package demo.layout;
  2.  
  3. import org.eclipse.swt.SWT;
  4.  
  5. public class QQ {
  6.        
  7.         /**
  8.          * 为了自定义监听器类MyMouseListener中的代码能够访问到以下对象,故将这些对象定义成类的实例变量
  9.          */
  10.         private StackLayout stackLayout = new StackLayout();
  11.         private Composite yourDataComp;
  12.         private Composite otherComp;
  13.         private List selectList;
  14.         private Composite rightComp;
  15.        
  16.         /**
  17.          * 由于自定义方法较多程序较长,这次的主程序框架选择了Eclipse“新建”向导的第二组:"public open() method"
  18.          * @param args
  19.          */
  20.         public static void main(String[] args){
  21.                 try {
  22.                         QQ window = new QQ();
  23.                         window.open();
  24.                 }catch(Exception e) {
  25.                         e.printStackTrace();
  26.                 }
  27.         }
  28.        
  29.         public void open() {
  30.                 Display display = Display.getDefault();
  31.                 Shell shell = new Shell();
  32.                 shell.setSize(550, 350);
  33.                 shell.setText("个人设置");
  34.                 shell.setLayout(new GridLayout());
  35.                 {
  36.                         //      分割窗口
  37.                         SashForm sashForm = new SashForm(shell, SWT.BORDER);
  38.                         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
  39.                         {
  40.                                 //      分割窗左边的列表框
  41.                                 selectList = new List(sashForm, SWT.BORDER);
  42.                                 //      作为演示只加了两项
  43.                                 selectList.setItems(new String[] { "个人资料", "联系方式"} );
  44.                                 //      加一个鼠标监听器
  45.                                 selectList.addMouseListener(new MyMouseListener());
  46.                         }
  47.                         {
  48.                                 //      右边的堆栈式面板
  49.                                 rightComp = new Composite(sashForm, SWT.NONE);
  50.                                 rightComp.setLayout(stackLayout);
  51.                                 //      共两页。将生成此面板的代码提出成一个方法,保证代码结构的清晰
  52.                                 yourDataComp = createYourDataComp(rightComp);   // 个人资料的面板
  53.                                 otherComp = createOtherComp(rightComp);   //       联系方式的面板
  54.                                
  55.                                 //      在堆栈面板上先显示“个人资料”界面
  56.                                 stackLayout.topControl = yourDataComp;
  57.                         }
  58.                         //      分割窗口的左右空间比例
  59.                         sashForm.setWeights( new int[] { 1,4 });
  60.                 }
  61.                 {
  62.                         //      界面的按钮的面板
  63.                         Composite buttonComp = new Composite(shell, SWT.NONE);
  64.                         //      使用GridData设置buttonComp在它父容器shell中的布局方式
  65.                         GridData gridData = new GridData();
  66.                         gridData.horizontalAlignment = GridData.END;    //  让buttonComp向右靠
  67.                         buttonComp.setLayoutData(gridData);
  68.                         //      设置buttonComp的布局为RowLayout,用来设定buttonComp内组件的布局方式
  69.                         RowLayout rowLayout = new RowLayout();
  70.                         rowLayout.spacing = 15//       按钮之间间隔15个像素
  71.                         buttonComp.setLayout(rowLayout);
  72.                         //      在buttonComp下建立三个按钮
  73.                         new Button(buttonComp, SWT.NONE).setText("      确定     ");
  74.                         new Button(buttonComp, SWT.NONE).setText("      取消     ");
  75.                         new Button(buttonComp, SWT.NONE).setText("      应用     ");
  76.                 }
  77.                
  78.                 shell.layout();
  79.                 shell.open();
  80.                 while(!shell.isDisposed()) {
  81.                         if(!display.readAndDispatch()) {
  82.                                 display.sleep();
  83.                         }
  84.                 }
  85.         }
  86.        
  87.         /**
  88.          * 个人资料面板的生成
  89.          * @param rightComp
  90.          * @return
  91.          */
  92.         private Composite createYourDataComp(Composite rightComp) {
  93.                 Composite composite = new Composite(rightComp, SWT.NONE);
  94.                 composite.setLayout(new GridLayout(6, false));//        个人资料面板分成6列
  95.                
  96.                 //--- 用户号码的标签及其文本框
  97.                 new Label(composite, SWT.NONE).setText("用户号码:");
  98.                 //      只读型的文本框
  99.                 Text text = new Text(composite, SWT.READ_ONLY | SWT.BORDER);
  100.                 //      水平抢占式充满,并占用三列的空间,createGridData是自定义方法
  101.                 text.setLayoutData(createGridData(GridData.FILL_HORIZONTAL, 3));
  102.                
  103.                 //--- 图片部分,我们再用一个面板嵌套来管理
  104.                 Composite photoComp = new Composite(composite, SWT.BORDER);
  105.                 //      水平垂直对齐式占满,横占两列,竖占4行
  106.                 photoComp.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL, 2, 4));
  107.                 photoComp.setLayout(new GridLayout(2, false));
  108.                 {
  109.                         //      图片
  110.                         Composite tempComp = new Composite(photoComp, SWT.BORDER);
  111.                         tempComp.setLayoutData(new GridData(50, 50));      // 设定大小:50宽50高
  112.                        
  113.                         //      选择图片的箭头型按钮,并设置它向下靠
  114.                         Button selPhotoButton = new Button(photoComp, SWT.ARROW|SWT.DOWN);
  115.                         selPhotoButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
  116.                        
  117.                         //      升级成为会员的按钮,横站photoComp的两列,并横向对齐充满
  118.                         Button updateButton = new Button(photoComp, SWT.NONE);
  119.                         updateButton.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 2));
  120.                         updateButton.setText("升级成为会员");
  121.                 }
  122.                
  123.                 new Label(composite, SWT.NONE).setText("用户昵称:");
  124.                 Text nicknameText = new Text(composite, SWT.BORDER);
  125.                 nicknameText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
  126.                
  127.                 new Label(composite, SWT.NONE).setText("个性签名:");
  128.                 Text attachNameText = new Text(composite, SWT.BORDER);
  129.                 attachNameText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL, 3));
  130.                
  131.                 new Label(composite, SWT.NONE).setText("等    级:");
  132.                 {
  133.                         //      图片
  134.                         Composite tempComp = new Composite(composite, SWT.BORDER);
  135.                         GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
  136.                         gridData.horizontalSpan = 3;
  137.                                 //      Composite 默认的高度太高,故手工设定高度为20像素
  138.                                 gridData.heightHint = 20;
  139.                                 tempComp.setLayoutData(gridData);
  140.                 }
  141.                
  142.                 new Label(composite, SWT.NONE).setText("性    别:");
  143.                 Combo sexCombo = new Combo(composite, SWT.NONE);
  144.                 sexCombo.setItems(new String[] { "男", "女" } );
  145.                
  146.                 new Label(composite, SWT.NONE).setText("姓名:");
  147.                 Text nameText = new Text(composite, SWT.BORDER);
  148.                 nameText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  149.                
  150.                 new Label(composite, SWT.NONE).setText("年龄:");
  151.                 Text oldText = new Text(composite, SWT.BORDER);
  152.                 oldText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  153.                
  154.                 new Label(composite, SWT.NONE).setText("毕业院校:");
  155.                 Text schoolText = new Text(composite, SWT.BORDER);
  156.                 schoolText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  157.  
  158.                 new Label(composite, SWT.NONE).setText("生肖:");
  159.                 Combo animalCombo = new Combo(composite, SWT.NONE);
  160.                 animalCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  161.                 animalCombo.setItems(new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" } );
  162.  
  163.                 new Label(composite, SWT.NONE).setText("职业:");
  164.                 Text jobText = new Text(composite, SWT.BORDER);
  165.                 jobText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  166.  
  167.                 new Label(composite, SWT.NONE).setText("星座:");
  168.                 Combo constellationCombo = new Combo(composite, SWT.NONE);
  169.                 constellationCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
  170.                 constellationCombo.setItems(new String[] { "白羊", "金牛", "双子", "巨蟹", "狮子", "处女", "天枰", "天蝎", "射手", "摩羯", "水瓶", "双鱼" } );
  171.                
  172.                 Label introLabel = new Label(composite, SWT.NONE);
  173.                 //      默认是居中,改为顶端对齐
  174.                 introLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
  175.                 introLabel.setText("个人说明:");
  176.                 Text introText = new Text(composite, SWT.BORDER|SWT.WRAP);      //    WRAP自动换行
  177.                 introText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL, 5));
  178.                
  179.                 //      返回个人资料面板
  180.                 return composite;
  181.         }
  182.        
  183.         /**
  184.          * 生成一个简单的联系方式面板
  185.          * @param rightComp
  186.          * @return
  187.          */
  188.         private Composite createOtherComp(Composite rightComp) {
  189.                 Composite composite = new Composite(rightComp, SWT.NONE);
  190.                 composite.setLayout(new FillLayout());
  191.                 new Label(composite, SWT.NONE).setText("联系方式面板");
  192.                 return composite;
  193.         }
  194.        
  195.         /**
  196.          * 生成GridData对象的重复代码太多,写成一个方法可以减少程序的行数,用起来也方便
  197.          * 而且还要注意一个GridData只能被一个组件使用,不能两个组件使用一个GridData,则有一个组件将不会显示出来
  198.          * @param style
  199.          * @param horizontalSpan
  200.          * @return
  201.          */
  202.         private GridData createGridData(int style, int horizontalSpan) {
  203.                 GridData gridData = new GridData(style);
  204.                 gridData.horizontalSpan = horizontalSpan;
  205.                 return gridData;
  206.         }
  207.        
  208.         private GridData createGridData(int style, int horizontalSpan, int verticalSpan) {
  209.                 GridData gridData = new GridData(style);
  210.                 gridData.horizontalSpan = horizontalSpan;
  211.                 gridData.verticalSpan = verticalSpan;
  212.                 return gridData;
  213.         }
  214.        
  215.         /**
  216.          * 鼠标监听器,采用事件的命名内部类写法
  217.          * @author Administrator
  218.          *
  219.          */
  220.         private class MyMouseListener extends MouseAdapter {
  221.                 public void mouseDown(MouseEvent e) {
  222.                         //      得到列表的当前选择项的序号(即单击的那项)
  223.                         int selectionIndex = selectList.getSelectionIndex();
  224.                         //      如果单击第一项“个人资料”,则让相对应的面板移至最上面
  225.                         if(selectionIndex == 0) {
  226.                                 stackLayout.topControl = yourDataComp;
  227.                         }else{
  228.                                 stackLayout.topControl = otherComp;
  229.                         }
  230.                         rightComp.layout();     //   刷新堆栈式布局的顶容器
  231.                 }
  232.         }
  233.  
  234. }
cleaning services ab 说:
2019年10月05日 17:30

As soon as you choose Vitality Clean maid services for the professional Family home keeping and additionally House maid service for use on your home, you’re able to rest simple and easy knowing you can be working along with the best commercially aware cleaning supplier in Dubai. You can have the vibe on the air and additionally purity at your house, when you aquire acquainted with this Home Maid services. you don’t really have to worry about picking a cleaning supplier that venues your prior requisites — not before long, we’ll have your personal property sparkling sparkling!

full time maids in d 说:
2021年1月26日 16:46

If you get a new maid into the future in along with help this will free way up more of your energy to help you pursue your current hobbies or maybe do exercises that you might not accomplish previously. Exercises cause you to be healthier and lower real get older by a very extensive period. Exercises put back your trip to a medical professional, saving anyone expenses along with ensuring that you don't lose your time and efforts.

villa painting dubai 说:
2021年6月23日 19:17

Eventhough it might seem cumbersome and annoying, you would be wise to get at the very least three quotations from about three different building contractors. Getting at the very least three quotes not simply helps anyone estimate what the position could cost, but also allows you learn precisely how each builder operates. The offer process permits you to ask numerous questions since you can about assembling your garden shed, the builder, and the way they plan to complete the task.

babysitting services 说:
2021年9月24日 19:20

Allow me to explain need your cleaning service fairly often, most businesses meet the needs of one time frame cleanings equally efficiently. If you will be on the point of have a party or meeting of any kind then your maid cleaning up service might be a great asset to acquire encouraging person he knows. But you decide to do end up being careful with what service you pick as they can be not all of equal while in the efficiency area and others.

cleaning company dub 说:
2023年8月23日 17:09

These days it's quite common for painting contractors to include a clause in their contracts stating that homeowners permit them to place advertising signage in their front yard. This can be a great advertising opportunity while you are working on a painting job. Some areas have ordinances that forbid this kind of advertising though so local regulations may deem such a clause immaterial.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter