Feng_Ellick 2016-06-29 15:49 采纳率: 0%
浏览 2471

在java swing 中实现动态创建按钮并绑定点击事件,为何不能实现?

//内部类,实现创建button,兵绑定点击事件
class RentBtn extends JButton{

    public JButton getRentBtn(int x,int y,int weigth,int heigth,int shopId,Merchant merchant){
        int btn_x = x;
        int  btn_y = y;
        int  btn_weigth = weigth;
        int btn_heigth= heigth;
        int id = shopId;
        Merchant user = merchant;
        JButton jButton = new JButton("租用");
        jButton.setBounds(btn_x, btn_y, btn_weigth, btn_heigth);

        jButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Session session = null;
                Transaction transaction = null;
                try {
                    session = HibernateUtil.getSession();
                    transaction = session.beginTransaction();
                    transaction.begin();

                    String hql = "select s from Shop s where s.id=:id";
                    Shop shop = (Shop) session.createQuery(hql).setParameter("id", shopId);
                    shop.setMerchant(user);
                    transaction.commit();
                    session.save(shop);
                    ShopRent shopRent = new ShopRent(user);

                    JOptionPane jOptionPane = new JOptionPane("租用成功");

                } catch (Exception e) {
                    HibernateUtil.closeSession();
                    transaction.rollback();
                }
            }
        });
        return jButton;
    }
 for(int i=0;i<shops.size();i++)
        {
            name = shops.get(i).getId();
            map.put(i, name);
            propertyfee = shops.get(i).getPropertyfee();
            utilities = shops.get(i).getUtilities();
            rent = shops.get(i).getRent();

            shopname = new JTextField();
            shopname.setText(String.valueOf(name));
            shopname.setBounds(10, 84+i*30, 47, 21);
            add(shopname);
            shopname.setColumns(10);

            propertyfeeFiled = new JTextField();
            propertyfeeFiled.setText(String.valueOf(propertyfee));
            propertyfeeFiled.setColumns(10);
            propertyfeeFiled.setBounds(91, 84+i*30, 47, 21);
            add(propertyfeeFiled);

            utilitiesFiled = new JTextField();
            utilitiesFiled.setText(String.valueOf(utilities));
            utilitiesFiled.setColumns(10);
            utilitiesFiled.setBounds(177, 84+i*30, 47, 21);
            add(utilitiesFiled);

            rentFiled = new JTextField();
            rentFiled.setText(String.valueOf(rent));
            rentFiled.setColumns(10);
            rentFiled.setBounds(267, 84+i*30, 47, 21);
            add(rentFiled);

            RentBtn rentBtn = new RentBtn();
            JButton jButton = rentBtn.getRentBtn(356, 84+i*30, 72, 23, name,mer );
            add(jButton);
        }

实际运行效果是:按钮成功创建,但是点击按钮没有响应。想请问下有什么其他办法?

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-06-29 21:31
    关注

    把try catch去掉,看下是不是你访问数据库的代码有问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器