hooyantsing's Blog

第49次课程

字数统计: 2k阅读时长: 11 min
2019/07/29

源辰74班

第49次课程

07.29.2019

内容

Java.swt包二[廖彦]

1.Table控件的练习

Class TableWin

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
package am.tablewin;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableWin {

    protected Shell shell;
    private Table table;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TableWin window = new TableWin();
            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(481, 616);
        shell.setText("SWT Application");
        
        table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setBounds(31, 23, 404, 532);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        
        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setWidth(100);
        tableColumn.setText("\u7F16\u53F7");
        
        TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
        tableColumn_1.setWidth(100);
        tableColumn_1.setText("\u59D3\u540D");
        
        TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
        tableColumn_2.setWidth(100);
        tableColumn_2.setText("\u804C\u52A1");
        
        TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
        tableColumn_3.setWidth(100);
        tableColumn_3.setText("\u85AA\u8D44");
        
        List<Map<String,Object>> ret = DBhelper.query("select * from emp");
        
        for(Map<String,Object> row:ret) {
            TableItem tableItem = new TableItem(table,SWT.NONE);
            tableItem.setText(new String[] {
                    row.get("EMPNO")+"", //避免空表格
                    row.get("ENAME").toString(),
                    row.get("JOB").toString(),
                    row.get("SAL").toString()
            });
        }
    }
}

2.Meru控件的练习

Class MeruWin

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
package am.tablewin;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MeruWin {

    protected Shell shell;
    private Table table;

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

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        //居中显示
        int x = (display.getBounds().width - shell.getBounds().width) / 2;
        int y = (display.getBounds().height - shell.getBounds().height) / 2;
        shell.setLocation(x, y);
        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(634, 593);
        shell.setText("SWT Application");
        
        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        
        MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
        mntmNewSubmenu.setText("\u7CFB\u7EDF\u7BA1\u7406");
        
        Menu menu_1 = new Menu(mntmNewSubmenu);
        mntmNewSubmenu.setMenu(menu_1);
        
        MenuItem menuItem = new MenuItem(menu_1, SWT.NONE);
        
        menuItem.setText("\u5458\u5DE5\u7BA1\u7406");
        
        MenuItem menuItem_1 = new MenuItem(menu_1, SWT.NONE);
        menuItem_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                DeptDialog dd = new DeptDialog(shell,SWT.NONE,"财务部");
                Object result = dd.open();
                MessageBox mb = new MessageBox(shell);
                mb.setMessage("返回部门名称:"+result);
                mb.open();
            }
        });
        menuItem_1.setText("\u90E8\u95E8\u7BA1\u7406");
        
        MenuItem mntmNewSubmenu_1 = new MenuItem(menu, SWT.CASCADE);
        mntmNewSubmenu_1.setText("New SubMenu");
        
        Menu menu_2 = new Menu(mntmNewSubmenu_1);
        mntmNewSubmenu_1.setMenu(menu_2);
        
        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
        tabFolder.setBounds(10, 10, 598, 514);
        
        TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
        tbtmNewItem.setText("\u9996\u9875");
        
        menuItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                
                //标题页去重
                for(TabItem t : tabFolder.getItems()) {
                    if("员工管理".equals(t.getText())) {
                        tabFolder.setSelection(t);
                        return;
                    }
                }
                
                TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
                tabItem.setText("员工管理");
                
                table = new Table(tabFolder, SWT.BORDER | SWT.FULL_SELECTION);
                tabItem.setControl(table);
                table.setHeaderVisible(true);
                table.setLinesVisible(true);
                
                
                TableColumn tableColumn = new TableColumn(table, SWT.NONE);
                tableColumn.setWidth(100);
                tableColumn.setText("\u7F16\u53F7");
                
                TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
                tableColumn_1.setWidth(100);
                tableColumn_1.setText("\u59D3\u540D");
                
                TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
                tableColumn_2.setWidth(100);
                tableColumn_2.setText("\u804C\u52A1");
                
                TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
                tableColumn_3.setWidth(100);
                tableColumn_3.setText("\u85AA\u8D44");
                
                List<Map<String,Object>> ret = DBhelper.query("select * from emp");
                
                for(Map<String,Object> row:ret) {
                    TableItem tableItem = new TableItem(table,SWT.NONE);
                    tableItem.setText(new String[] {
                            row.get("EMPNO")+"", //避免空表格
                            row.get("ENAME").toString(),
                            row.get("JOB").toString(),
                            row.get("SAL").toString()
                    });
                }
                //自动跳转到标题页
                tabFolder.setSelection(tabItem);
            }
        });
    }
}

**3.**Dialog工程文件

​用于做子程序框

Class DeptDialog

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
package am.tablewin;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

public class DeptDialog extends Dialog {

    //用于窗口关闭时,返回数据
    protected Object result;
    protected Shell shell;
    
    private Text text;
    private String dname;

    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public DeptDialog(Shell parent, int style,String dname) {
        super(parent, style);
        setText("SWT Dialog");
        this.dname = dname;
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            //设置返回结果
            dname = text.getText();
            result = dname;
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
        shell.setSize(576, 519);
        shell.setText(getText());
        
        Label label = new Label(shell, SWT.NONE);
        label.setBounds(109, 210, 61, 17);
        label.setText("\u90E8\u95E8\u540D\u79F0");
        
        text = new Text(shell, SWT.BORDER);
        text.setText("\u8D22\u52A1\u90E8");
        text.setBounds(196, 210, 73, 23);
        
        Label label_1 = new Label(shell, SWT.NONE);
        label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15, SWT.NORMAL));
        label_1.setBounds(181, 144, 109, 33);
        label_1.setText("\u90E8\u95E8\u7BA1\u7406");
        
        //设置部门名称
        text.setText(dname);
    }
}

4.工具类引入

Class DBhelper

关于Dao学习时的练习工具类

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
package am.tablewin;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class DBhelper {
    
    private static String url;
    private static String username;
    private static String password;
    
    //驱动加载
    static {
        try {
            Properties p = new Properties();
            //读取配置文件,转成输入流
            InputStream in = DBhelper.class.getClassLoader().getResourceAsStream("db.properties");
            p.load(in);
            String driver = p.getProperty("driver");
            url = p.getProperty("url");
            username = p.getProperty("username");
            password = p.getProperty("password");
            Class.forName(driver);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    

    public static void main(String[] args) {
        //String sql = "insert into dept values(?,?,?)";
        //update(sql,11,"财务","衡阳");
    }
    
    /**
     * ...可变参数数组
     */
    //增删改
    public static int update(String sql,Object...paramArray) {
        Connection conn = null;
        try {
            conn = openConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            for(int i=0;i<paramArray.length;i++) {
                ps.setObject(i+1, paramArray[i]);
            }
            return ps.executeUpdate();
        } catch(Exception e){
            //异常转型
            throw new RuntimeException(e);
        } finally {
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    //连接
    public static Connection openConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
    //查找
    public static List<Map<String,Object>> query(String sql,Object...paramArray) {
        Connection conn = null;
        try {
            conn = openConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            for(int i=0;i<paramArray.length;i++) {
                ps.setObject(i+1, paramArray[i]);
            }
            ResultSet rs = ps.executeQuery();
            //获取元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            List<Map<String,Object>> ret = new ArrayList<>();
            while(rs.next()) {
                Map<String,Object> row = new LinkedHashMap<>();
                for(int i=1;i<rsmd.getColumnCount();i++) {
                    String columnName = rsmd.getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    row.put(columnName, columnValue);
                }
                ret.add(row);
            }
            return ret;
        } catch(Exception e){
            //异常转型
            throw new RuntimeException(e);
        } finally {
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    /**
     * 查询一条记录(例如根据主键查询记录)
     */
    public static Map<String,Object> queryOne (String sql,Object...paramArray){
        Connection conn = null;
        try {
            conn = openConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            for(int i=0;i<paramArray.length;i++) {
                ps.setObject(i+1, paramArray[i]);
            }
            ResultSet rs = ps.executeQuery();
            //获取元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            if(rs.next()) {
                Map<String,Object> row = new LinkedHashMap<>();
                for(int i=1;i<rsmd.getColumnCount();i++) {
                    //获取列名
                    String columnName = rsmd.getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    row.put(columnName, columnValue);
                }
                return row;
            }
            return null;
        } catch(Exception e){
            //异常转型
            throw new RuntimeException(e);
        } finally {
            try {
                if(conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    //分页查询
    public static List<Map<String,Object>> queryPage (
            String sql,int page,int rows,Object...paramArray){
        int startrow = (page - 1) * rows;
        int endrow = page * rows;
        sql = "select t.*\n" +
              " from ( select t.*,rownum rn from ( "+sql+") t where rownum <= ?) t\n" +
              " where t.rn > ?";    
        //生成新的查询数组
        Object[] newParamArray = new Object[paramArray.length + 2];
        System.arraycopy(paramArray, 0, newParamArray, 0, paramArray.length);
        newParamArray[newParamArray.length - 2] = endrow;
        newParamArray[newParamArray.length - 1] = startrow;

        return query(sql,newParamArray);
    }
    
    /**
     * 从查询结果中获取一个值,返回第一个行第一列的值
     */
    public static Object getValue(String sql,Object...paramArray) {
        Map<String,Object> row = queryOne(sql,paramArray);
        if(row==null) {
            return null;
        }else {
            for(Map.Entry<String, Object> e:row.entrySet()) {
                return e.getValue();
            }
        }
        return null;
    }
}
CATALOG