hooyantsing's Blog

第48次课程

字数统计: 4.3k阅读时长: 20 min
2019/07/27

源辰74班

第48次课程

07.27.2019

内容

Java.swt包一[廖彦]

1.关于多选框联动

Class ShengShi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package pm.swt;
public class ShengShi {
    
    public static final String[] PROVINCE =  {"北京市","上海市","天津市","重庆市","河北省","山西省","辽宁省","吉林省","河南省","江苏省","浙江省","安徽省","福建省","江西省","山东省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省","陕西省","甘肃省","青海省","黑龙江省","内蒙古自治区","广西壮族自治区","西藏自治区","宁夏回族自治区","新疆维吾尔自治区","台湾省","香港特别行政区","澳门特别行政区"};
    
    public static final String[][] CITY = {
    //北京市辖区名称
            {"东城区","西城区","崇文区","宣武区","朝阳区","海淀区","丰台区","石景山区","房山区","通州区","顺义区","门头沟区","昌平区","大兴区","怀柔区","平谷区","密云县","延庆县"},
    //上海市辖区名称
            {"黄浦区","卢湾区","徐汇区","长宁区","静安区","普陀区","闸北区","虹口区","杨浦区","宝山区","闵行区","嘉定区","浦东新区","金山区","松江区","青浦区","南汇区","奉贤区","崇明县"},
    //天津市辖区名称
            {"和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","宁河县","静海县","蓟县"},
    //重庆市辖区名称
            {"渝中区","大渡口区","江北区","沙坪坝区","九龙坡区","南岸区","北碚区","万盛区","双桥区","渝北区","巴南区","万县区","涪陵区","永川市","合川市","江津市","南川市","长寿县","綦江县","潼南县","荣昌县","壁山县","大足县","铜梁县","梁平县","城口县","垫江县","武隆县","丰都县","忠 县","开 县","云阳县","青龙镇青龙嘴","奉节县","巫山县","巫溪县","南宾镇","中和镇","钟多镇","联合镇","汉葭镇"},
    //河北省主要城市名称
            {"石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口市","承德市","沧州市","廊坊市","衡水市"},
    //山西省主要城市名称
            {"太原市","大同市","阳泉市","长治市","晋城市","朔州市","晋中市","运城市","忻州市","临汾市","吕梁市"},
    //辽宁省主要城市名称
            {"沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦市","铁岭市","朝阳市","葫芦岛市"},
    //吉林省主要城市名称
            {"长春市","吉林市","四平市","辽源市","通化市","白山市","松原市","白城市","延边朝鲜族自治州"},
    //河南省主要城市名称
            {"郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市","济源市"},
    //江苏省主要城市名称
            {"南京市","无锡市","徐州市","常州市","苏州市","南通市","连云港市","淮安市","盐城市","扬州市","镇江市","泰州市","宿迁市"},
    //浙江省主要城市名称
            {"杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市"},
    //安徽省主要城市名称
            {"合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","阜阳市","宿州市","巢湖市","六安市","亳州市","池州市","宣城市"},
    //福建省主要城市名称
            {"福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市"},
    //江西省主要城市名称
            {"南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市"},
    //山东省主要城市名称
            {"济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","威海市","济宁市","泰安市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","菏泽市"},
    //湖北省主要城市名称
            {"武汉市","黄石市","襄樊市","十堰市","荆州市","宜昌市","荆门市","鄂州市","孝感市","黄冈市","咸宁市","随州市","恩施州","仙桃市","潜江市","天门市","神农架林区"},
    //湖南省主要城市名称
            {"长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西州"},
    //广东省主要城市名称
            {"广州市","深圳市","珠海市","汕头市","韶关市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市","东莞市","中山市","潮州市","揭阳市","云浮市"},
    //海南省主要城市名称
            {"海口市","龙华区","秀英区","琼山区","美兰区","三亚市"},
    //四川省主要城市名称
            {"成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充市","宜宾市","广安市","达州市","眉山市","雅安市","巴中市","资阳市","阿坝州","甘孜州","凉山州"},
    //贵州省主要城市名称
            {"贵阳市","六盘水市","遵义市","安顺市","铜仁地区","毕节地区","黔西南州","黔东南州","黔南州"},
    //云南省主要城市名称
            {"昆明市","大理市","曲靖市","玉溪市","昭通市","楚雄市","红河市","文山市","思茅市","西双版纳市","保山市","德宏市","丽江市","怒江市","迪庆市","临沧市"},
    //陕西省主要城市名称
            {"西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市"},
    //甘肃省主要城市名称
            {"兰州市","嘉峪关市","金昌市","白银市","天水市","武威市","张掖市","平凉市","酒泉市","庆阳市","定西市","陇南市","临夏州","甘南州"},
    //青海省主要城市名称
            {"西宁市","海东地区","海北州","黄南州","海南州","果洛州","玉树州","海西州"},
    //黑龙江省主要城市名称
            {"哈尔滨市","齐齐哈尔市","鸡西市","鹤岗市","双鸭山市","大庆市","伊春市","佳木斯市","七台河市","牡丹江市","黑河市","绥化市","大兴安岭地区"},
    //内蒙古自治区主要城市名称
            {"呼和浩特市","包头市","乌海市","赤峰市","通辽市","鄂尔多斯市","呼伦贝尔市","巴彦淖尔市","乌兰察布市","兴安盟","锡林郭勒盟","阿拉善盟"},
    //广西壮族自治区主要城市名称
            {"南宁市","柳州市","桂林市","梧州市","北海市","防城港市","钦州市","贵港市","玉林市","百色市","贺州市","河池市","来宾市","崇左市"},
    //西藏自治区主要城市名称
            {"拉萨市","昌都地区","山南地区","日喀则地区","那曲地区","阿里地区","林芝地区"},
    //宁夏回族自治区主要城市名称
            {"银川市","石嘴山市","吴忠市","固原市","中卫市"},
    //新疆维吾尔自治区主要城市名称
            {"乌鲁木齐市","克拉玛依市","吐鲁番地区","哈密地区","和田地区","阿克苏地区","喀什地区","克孜勒苏柯尔克孜自治州","巴音郭楞蒙古自治州","昌吉回族自治州","博尔塔拉蒙古自治州","伊犁哈萨克自治州","塔城地区","阿勒泰地区","石河子市","阿拉尔市","图木舒克市","五家渠市"},
    //台湾省主要城市名称
            {"台北市","高雄市","基隆市","台中市","台南市","新竹市","嘉义市","台北县","宜兰县","桃园县","新竹县","苗栗县","台中县","彰化县","南投县","云林县","嘉义县","台南县","高雄县","屏东县","澎湖县","台东县","花莲县"},
    //香港特别行政区主要辖区名称
            {"中西区","东区","九龙城区","观塘区","南区","深水埗区","黄大仙区","湾仔区","油尖旺区","离岛区","葵青区","北区","西贡区","沙田区","屯门区","大埔区","荃湾区","元朗区"},
    //澳门地区
            {"澳门地区"}
            };
}

Class SwtHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package pm.swt;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.widgets.Button;

public class SwtHelper {
    public static List<String> getSelection(Button...btns){
        List<String> list = new ArrayList<>();
        for(Button btn : btns) {
            list.add(btn.getText());
        }
        return list;
    }
}

Class TestWin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package pm.swt;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TabFolder;

public class TestWin {

    protected Shell shell;
    private Text name;
    private Text pwd;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestWin window = new TestWin();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setText("\u5B66\u751F\u6CE8\u518C\u7CFB\u7EDF");
        shell.setSize(453, 416);
        
        Label label = new Label(shell, SWT.NONE);
        label.setBounds(95, 30, 61, 17);
        label.setText("\u59D3\u540D");
        
        name = new Text(shell, SWT.BORDER);
        name.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                if(name.getText().length()<6||name.getText().length()>10){
                    MessageBox mb = new MessageBox(shell,SWT.ICON_WARNING | SWT.OK);
                    mb.setText("系统提示");
                    mb.setMessage("用户名必须是6~10位!");
                    mb.open();
                }
            }
        });
        name.setBounds(164, 27, 165, 23);
        
        Label label_1 = new Label(shell, SWT.NONE);
        label_1.setBounds(95, 81, 61, 17);
        label_1.setText("\u5BC6\u7801");
        
        pwd = new Text(shell, SWT.BORDER);
        pwd.setBounds(164, 80, 166, 23);
        
        Label label_2 = new Label(shell, SWT.NONE);
        label_2.setBounds(95, 133, 61, 17);
        label_2.setText("\u7231\u597D");
        
        Button likeBtn4 = new Button(shell, SWT.CHECK);
        likeBtn4.setBounds(285, 133, 47, 17);
        likeBtn4.setText("\u7BEE\u7403");
        
        Button likeBtn1 = new Button(shell, SWT.CHECK);
        likeBtn1.setBounds(164, 133, 33, 17);
        likeBtn1.setText("\u5531");
        
        Button likeBtn2 = new Button(shell, SWT.CHECK);
        likeBtn2.setBounds(202, 133, 33, 17);
        likeBtn2.setText("\u8DF3");
        
        Button likeBtn3 = new Button(shell, SWT.CHECK);
        likeBtn3.setBounds(241, 133, 44, 17);
        likeBtn3.setText("Rap");
        
        Combo combo = new Combo(shell, SWT.NONE);

        combo.setItems(ShengShi.PROVINCE);
        combo.setBounds(163, 178, 72, 25);
        
        Label label_3 = new Label(shell, SWT.NONE);
        label_3.setBounds(95, 181, 33, 17);
        label_3.setText("\u7C4D\u8D2F");
        
        Label label_4 = new Label(shell, SWT.NONE);
        label_4.setBounds(95, 238, 61, 17);
        label_4.setText("\u6027\u522B");
        
        Button button_2 = new Button(shell, SWT.RADIO);
        button_2.setBounds(164, 238, 61, 17);
        button_2.setText("\u7537\u751F");
        
        Button button_3 = new Button(shell, SWT.RADIO);
        button_3.setBounds(232, 238, 97, 17);
        button_3.setText("\u5973\u751F");
        
        DateTime dateTime = new DateTime(shell, SWT.BORDER);
        dateTime.setBounds(164, 280, 165, 24);
        
        Button button_5 = new Button(shell, SWT.NONE);
        button_5.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //当鼠标点击按钮(或键盘回车),则会执行该方法的代码
                //获取文本框的值
                System.out.println("用户名"+name.getText());
                System.out.println("密码:"+pwd.getText());
                //获取下拉框的值
                System.out.println("学历:"+combo.getText());
                //获取单选框、多选框的值
                List<String> likes = SwtHelper.getSelection(likeBtn1,likeBtn2,likeBtn3,likeBtn4);
                System.out.println("爱好:"+likes);
                List<String> sexs = SwtHelper.getSelection(button_2,button_2);
                System.out.println("性别:"+sexs);
                //获取日期
                System.out.printf("生日:%s年%s月%s日",
                        dateTime.getYear(),
                        dateTime.getMonth(),
                        dateTime.getDay());
            }
        });
        button_5.setBounds(117, 332, 80, 27);
        button_5.setText("\u786E\u8BA4");
        
        Button button_6 = new Button(shell, SWT.NONE);
        button_6.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //关闭窗口
                shell.close();
            }
        });
        button_6.setBounds(232, 332, 80, 27);
        button_6.setText("\u53D6\u6D88");
        
        
        Label label_5 = new Label(shell, SWT.NONE);
        label_5.setBounds(95, 287, 61, 17);
        label_5.setText("\u751F\u65E5");
        
        Combo combo_1 = new Combo(shell, SWT.NONE);
        combo_1.setBounds(257, 178, 72, 25);
        
        combo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //获取选中的选项的下标值(索引)
                int index = combo.getSelectionIndex();
                //更新城市下拉框的选项列表
                combo_1.setItems(ShengShi.CITY[index]);
            }
        });
    }
}

2.上下左右键位 移动

Class Move

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package pm.swt;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;

public class Move {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            Move window = new Move();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    
    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(686, 685);
        shell.setText("SWT Application");
        
        Label label = new Label(shell, SWT.NONE);
        label.setImage(SWTResourceManager.getImage(Move.class, "/pm/swt/timg.png"));
        label.setBounds(207, 196, 246, 250);
        shell.addKeyListener(new KeyAdapter() {
            
            @Override
            public void keyPressed(KeyEvent e) {
                int top = 16777217;
                int down = 16777218;
                int left = 16777219;
                int right = 16777220;
                System.out.println(e.keyCode);
                if(e.keyCode==top) {
                    Point p = label.getLocation();
                    p.y -= 5;
                    label.setLocation(p);
                }
                if(e.keyCode==down) {
                    Point p = label.getLocation();
                    p.y += 5;
                    label.setLocation(p);
                }
                if(e.keyCode==left) {
                    Point p = label.getLocation();
                    p.x -= 5;
                    label.setLocation(p);
                }
                if(e.keyCode==right) {
                    Point p = label.getLocation();
                    p.y += 5;
                    label.setLocation(p);
                }
            }
            
        });
    }
}
CATALOG