public Fee(String identity) { usersDao = new UsersDao(); this.users = usersDao.findUsersByIdentity(identity); // this.userMain=userMain; fee = this; feeRecordDao = new FeeRecordDao(); houseDao = new HouseDao(); this.setBounds(300, 200, 300, 250); // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("缴费:" + users.getUsername()); this.setLayout(null); houseLabel = new JLabel("您要缴费的房屋:"); houseLabel.setBounds(20, 20, 150, 20); feeLabel = new JLabel("您当前应缴费的面积:"); feeLabel.setBounds(20, 60, 200, 20); // 获取当前用户的房子,可能返回多个值 ArrayList<House> list = houseDao.findHouseByIdentity(users.getIdentity()); int size = list.size(); // 这儿定义数组的时候一定要确定数组的长度,否则会发生数组越界错误 String[] values; values = new String[size]; for (int i = 0; i < size; i++) { values[i] = list.get(i).getHouseid() + ""; } boxModel = new DefaultComboBoxModel(values); box = new JComboBox(boxModel); box.setBounds(150, 20, 80, 20); // System.out.println(box.getSelectedItem()); // 获取选中的房子 House house = houseDao.findHouseByHouseid(Integer.parseInt(box.getSelectedItem().toString())); // 获取房子的面积 double area = house.getArea(); feeTextField = new JTextField(20); feeTextField.setText(area + ""); feeTextField.setEditable(false); feeTextField.setBounds(150, 60, 50, 20); priceLabel = new JLabel("平米*1.5元/平米"); priceLabel.setBounds(200, 60, 100, 20); submitBtn = new JButton("确认缴费"); submitBtn.setBounds(100, 100, 100, 20); // 监听器 submitBtn.addActionListener(new MyBtnListener()); box.addItemListener(new MyItemListener()); this.add(box); this.add(submitBtn); this.add(feeTextField); this.add(feeLabel); this.add(priceLabel); this.add(houseLabel); this.setVisible(true); }
@Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { int num = box.getSelectedIndex(); House house = houseDao.findHouseByHouseid(Integer.parseInt(box.getSelectedItem().toString())); // 获取房子的面积 double area = house.getArea(); // 把textfile里放入面积 feeTextField.setText(area + ""); } }