//内部类,实现创建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);
}
实际运行效果是:按钮成功创建,但是点击按钮没有响应。想请问下有什么其他办法?