private JTable getTRent() {
if (tRent == null) { ...
tRent.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
tDescription.setText(house.getDescripcionMansion(tRent.getSelectedRow()));
image.setIcon(new ImageIcon(BookWindow.class.getResource(
"/images/" + house.getCodeMansion(tRent.getSelectedRow()) + ".png")));
String childrenAllowed = house.getChildrenAllowed(tRent.getSelectedRow());
if (childrenAllowed == "N"){
txKids.setEditable(false);
cbBookHouse.setEnabled(true);
}
}
});
}
return tRent;
}
我在前面的代码中遇到的问题是,除了图像之外的任何条件都已完成,似乎都被忽略了。
我刚刚检查了该值是否已正确读取,并且在控制台中使用 println 时,我会根据行得到 N 或 Y,但是如果我检查该值,则不会发生任何情况我希望更改其状态的组件。
还尝试将这些条件放入组件内,并且发生了同样的情况。
例如方法getChildrenAllowed()
的代码是这样的
public String getChildrenAllowed(int index) {
return houseRelation.get(index).getChildren();
}
最佳答案
不要将字符串与 ==
进行比较,而使用 equals()
或 equalsIgnoreCase()
if ("N".equalsIgnoreCase(childrenAllowed))
关于java - mouseClicked 事件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20840762/