Java桌面应用(3)---同屏刷新、实时更新数据库中新增记录显示

一、效果图

1、同屏刷新效果

(1)、刷新前

(2)、刷新后

2、刷新数据库中新增记录显示效果

 (1)、刷新前

(2)、刷新后

 新增数据是插入到数据库中的

 二、方法构思

  1. 将所有的功能分别写在JPanel容器之中
  2. removeAll()、remove()、revalidate()、reqaint()配合使用,此四种方法具有顺序要求
  3. 依次使用上面提到的方法删除不用的JPanel容器
  4. 在删除完容器之后需要对窗体进行重新布局
  5. 主窗体使用null布局方式,容器JPanel简易使用setBounds()进行布局
  6. 注意不要将创建的JPanel容器进行封装,否则会导致反应迟缓问题
  7. 检查JPanel容器是否重叠,可以多次点击功能按钮,之后使用Tab在文本输入行中进行跳转测试

三、部分源码

此代码对应如上效果图,数据库新增记录刷新在bt1的监听响应事件中

b2.addActionListener(e -> {
//            移除之前添加容器zp的所有布局,为这一次布局清空障碍
//            若无此句,则面板上的同一位置会出现若干此容器,个数为点击添加按钮的次数
            zp.removeAll();
            ij.remove(ce);
            ij.remove(dp);
            ij.remove(up);
            ij.remove(cp);
//            ij.remove(zp);
            ij.revalidate();
            ij.repaint();
            zp.setLayout(null);
            pan.setBounds(135,0,660,300);
            ij.add(pan);
            zp.setBounds(135, 300, 660, 300);
            JLabel l1 = new JLabel("姓名:");
            JLabel l2 = new JLabel("电话:");
            JLabel l3 = new JLabel("性别:");
            JLabel l4 = new JLabel("邮箱:");
            JLabel l5 = new JLabel("类型:");
            JLabel l6 = new JLabel("住址:");
            JTextField t1 = new JTextField();
            JTextField t2 = new JTextField();
            JTextField t4 = new JTextField();
            JTextField t6 = new JTextField();
            String[] sexx = {"男", "女"};
//            下拉选择列表
            JComboBox<String> comboBox2 = new JComboBox<>(sexx);
            comboBox2.addActionListener(ee -> {
                JComboBox<String> comboBox1 = (JComboBox<String>) ee.getSource();
                sex = (String) comboBox1.getSelectedItem();
            });
            String[] typee = {"朋友", "家人", "同事", "好友"};
            JComboBox<String> comboBox = new JComboBox<>(typee);
            comboBox.addActionListener(ee -> {
                JComboBox<String> comboBox1 = (JComboBox<String>) ee.getSource();
                type = (String) comboBox1.getSelectedItem();
            });

            l1.setFont(new Font("楷体", Font.BOLD, 24));
            l2.setFont(new Font("楷体", Font.BOLD, 24));
            l3.setFont(new Font("楷体", Font.BOLD, 24));
            l4.setFont(new Font("楷体", Font.BOLD, 24));
            l5.setFont(new Font("楷体", Font.BOLD, 24));
            l6.setFont(new Font("楷体", Font.BOLD, 24));
            t1.setFont(new Font("楷体", Font.BOLD, 24));
            t2.setFont(new Font("楷体", Font.BOLD, 24));
            comboBox2.setFont(new Font("楷体", Font.BOLD, 24));
            t4.setFont(new Font("楷体", Font.BOLD, 24));
            comboBox.setFont(new Font("楷体", Font.BOLD, 24));
            t6.setFont(new Font("楷体", Font.BOLD, 24));

            l1.setBounds(10, 10, 100, 40);
            t1.setBounds(100, 10, 160, 40);
            l2.setBounds(330, 10, 100, 40);
            t2.setBounds(420, 10, 160, 40);

            l3.setBounds(10, 60, 100, 40);
            comboBox2.setBounds(100, 60, 160, 40);
            l4.setBounds(330, 60, 100, 40);
            t4.setBounds(420, 60, 160, 40);

            l5.setBounds(10, 110, 100, 40);
            comboBox.setBounds(100, 110, 160, 40);
            l6.setBounds(330, 110, 100, 40);
            t6.setBounds(420, 110, 160, 40);

            zp.add(l1);
            zp.add(t1);
            zp.add(l2);
            zp.add(t2);

            zp.add(l3);
            zp.add(comboBox2);
            zp.add(l4);
            zp.add(t4);

            zp.add(l5);
            zp.add(comboBox);
            zp.add(l6);
            zp.add(t6);

            JButton bt1 = new JButton("保存");
            JButton bt2 = new JButton("清空");

            bt1.setFont(new Font("楷体", Font.BOLD, 24));
            bt2.setFont(new Font("楷体", Font.BOLD, 24));
            bt1.setBounds(170, 170, 100, 40);
            bt2.setBounds(370, 170, 100, 40);

            zp.add(bt1);
            zp.add(bt2);

            bt2.addActionListener(e1 -> {
                t1.setText("");
                t2.setText("");
                sex = "男";
                t4.setText("");
                type = "朋友";
                t6.setText("");
            });
            bt1.addActionListener(e1 -> {
                if (t1.getText().equals("") && t2.getText().equals("") && t4.getText().equals("") && t6.getText().equals("")) {
                    new MyMessage("内容不能为空");
                } else {
                    String sql = "insert into contacts values('" + t1.getText() + "','" + t2.getText() + "','" + sex + "','" + t4.getText() + "','" + type + "','" + t6.getText() + "','" + name + "')";
                    int flag = UpdateDao.upData(sql);
                    if (flag > 0) {
                        new MyMessage("保存成功");
//                        添加联系人成功之后,通过移除之前的显示容器,重新创建一个显示容器,
//                        依次来实现添加之后的的实时刷新,显示与上方面板
                        t1.setText("");
                        t2.setText("");
                        sex = "男";
                        t4.setText("");
                        type = "朋友";
                        t6.setText("");
                        pan.removeAll();
                        ij.revalidate();
                        ij.repaint();
                        JScrollPane pane = Look.look(name);
                        pan.add(pane);
                        pan.setLayout(null);
                        pane.setBounds(0, 0, 660, 300);
                        pan.setBounds(135, 0, 660, 300);
                        ij.add(pan);
                        zp.setBounds(135, 300, 660, 300);
                        ij.add(zp);

                    } else {
                        new MyMessage("保存失败");
                    }
                }
            });

            ij.add(zp).setVisible(true);
        });

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值