重拾JAVA之WinForm实战之(三)

  1. 云栖社区>
  2. 博客>
  3. 正文

重拾JAVA之WinForm实战之(三)

技术小阿哥 2017-11-07 21:59:00 浏览681
展开阅读全文

今天主要是将如下界面做成JAVA版的,我做这个Winform的目的并不是说觉得winform有前途,而是在练手,为了后面web和Andriod开发打点基础。

175324736.png

那做好的java界面如下

221551595.png

OK,画好了,看代码,首先是绑定树的一个代码

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
public void BuildCodeTree() throws SQLException {
        treeCode.removeAll();
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(new NodeData(
                "root""系统参数"));
        String sql = "SELECT Distinct ename,cname FROM dbo.Codes WITH(NOLOCK)";
        ResultSet res = JDBCSqlHelper.query(sql);
        try {
            while (res.next()) {
                DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(
                        new NodeData(res.getString("ename"),
                                res.getString("cname")));
                top.add(childTreeNode);
            }
            this.treeCode = new JTree(top);
            treeCode.setBounds(1039146278);
            treeCode.addTreeSelectionListener(new TreeSelectionListener() {
                public void valueChanged(TreeSelectionEvent e) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeCode
                            .getLastSelectedPathComponent();
                    String nodeName = ((NodeData) node.getUserObject()).ename;
                    if (nodeName == "root")
                        return;
                    String sql = "SELECT 0 as bit,data,ename,cname,display_content  FROM dbo.Codes WHERE ename='"
                            + nodeName + "'";
                    ResultSet res = JDBCSqlHelper.query(sql);
                    List<String> columnList = new ArrayList<String>();
                    columnList.add("选择");
                    columnList.add("数据值");
                    columnList.add("英文代码");
                    columnList.add("中文代码");
                    columnList.add("显示值");
                    DataFillHelper.FillTable(res, table, columnList);
                    table.getColumnModel().getColumn(0)
                            .setCellRenderer(new TableCellRenderer() {
                                public Component getTableCellRendererComponent(
                                        JTable table, Object value,
                                        boolean isSelected, boolean hasFocus,
                                        int row, int column) {
                                    JCheckBox ck = new JCheckBox();
                                    ck.setSelected(isSelected);
                                    ck.requestFocus(hasFocus);
                                    ck.setHorizontalAlignment((int0.5f);
                                    return ck;
                                }
                            });
                    table.getColumnModel().getColumn(0).setWidth(10);
                }
            });
            treeCode.setBorder(new LineBorder(new Color(000)));
            contentPane.add(treeCode);
            // treeCode.setCellRenderer(new MyRenderer());
            treeCode.setShowsRootHandles(true);
            treeCode.setRootVisible(true);
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        finally {
            res.close();
        }
    }

这段代码首先会从数据库读出要加载的树的数据,由于这里的树只有两级,所以代码很简单。先定义一个root节点,然后将数库中的读出的节点加载在他下面。这样树就形成了,然后在树节点被选择时注册一个事件,事件中,如果选择的是root节点,则不加载右边的Jtable数据。否则根据英文代码加载左边的Jtable数据。

节点的数据使用NodeData这个类类记录的

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
private class NodeData {
        private String ename;
        public String getEname() {
            return ename;
        }
        public void setEname(String ename) {
            this.ename = ename;
        }
        private String cname;
        public String getCname() {
            return cname;
        }
        public void setCname(String cname) {
            this.cname = cname;
        }
        public NodeData() {
        }
        public NodeData(String ename, String cname) {
            this.setEname(ename);
            this.setCname(cname);
        }
        public String toString() {
            return cname;
        }
    }

左边的JTable数据的加载通过DataFillHelper类实现。

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
public class DataFillHelper {
    public static void FillTable(ResultSet res, JTable jTable,List<String> columnList) {
        Vector<String> columnHeads = new Vector<String>();
        Vector<Vector> rows = new Vector<Vector>();
        try {
          ResultSetMetaData rsmd = res.getMetaData();
          for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            columnHeads.addElement(rsmd.getColumnName(i));
          }
          while (res.next()) {
            Vector<String> v = new Vector<String>();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
              v.addElement(res.getString(i));
            }
            rows.add(v);
          }
          DefaultTableModel model = new DefaultTableModel(rows, columnHeads);
          jTable.setModel(model);
          makeFace(jTable);
          for (int i = 0; i < jTable.getColumnModel().getColumnCount(); i++) {
            jTable.getColumnModel().getColumn(i).setHeaderValue(
                    columnList.get(i));
          }
                                                                                                                                                                                                                                                     
        catch (SQLException e) {
          // TODO 自动生成 catch 块
          e.printStackTrace();
        }
      }
                                                                                                                                                                                                                                               
    public static void makeFace(JTable table) {
        try {
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table,
                        Object value, boolean isSelected, boolean hasFocus,
                        int row, int column) {
                    if (row % 2 == 0)
                        setBackground(new Color(206231255));
                    else if (row % 2 == 1)
                        setBackground(Color.white);
                    return super.getTableCellRendererComponent(table, value,
                            isSelected, hasFocus, row, column);
                }
            };
          for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
          }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
}

这里就不多做解释,解释我也解释不清楚。OK,在这里需要说明的是如果将jtable放在JScrollPane中,可能会导致jtable的列头不能显示,所以需要加入下面的代码

1
2
3
4
5
6
7
table = new JTable();
        table.setBorder(UIManager.getBorder("FormattedTextField.border"));
        table.setBackground(Color.WHITE);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(16639430278);
        contentPane.add(scrollPane);
        table.setFillsViewportHeight(true);

就是那句SetFillsViewportHeight。OK,接下来我们看一下界面上的删除按钮功能。在前面我们已经在JTable中加入了checkBox。接下里我们看删除按钮的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void DeleteCodes() {
        int[] rowIndexs = table.getSelectedRows();
                                                                                        
        if(rowIndexs.length==0){
            MessageHelper.ShowMessage("请选择要删除的数据!");
            return;
        }
        StringBuffer strBuffer = new StringBuffer();
        for (int i : rowIndexs) {
            Object data = table.getValueAt(i, 1);
            Object ename = table.getValueAt(i, 2);
            strBuffer.append("'");
            strBuffer.append(data.toString() + ename.toString());
            strBuffer.append("',");
        }
        strBuffer.setLength(strBuffer.length() - 1);
        String sql = "DELETE FROM dbo.Codes WHERE (data+ename) IN ("
                + strBuffer.toString() + ")";
        MessageHelper.ShowMessage(sql);
        JDBCSqlHelper.update(sql);
    }

还有一个要说的是,我们并不想让后后面的列被编辑,我们只想第一列被编辑,所以需要覆盖IsCellEditable方法

1
2
3
4
5
6
7
8
table = new JTable() {
            public boolean isCellEditable(int row, int column) {
                if (column == 0) {
                    return true;
                else
                    return false;
            }
        };

OK,今天就讲到这里,洗洗睡!



本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/1330835,如需转载请自行联系原作者


网友评论

登录后评论
0/500
评论
技术小阿哥
+ 关注