如何在条件下启用 JButton?
How to enable a JButton on a condition?
我正在尝试创建一个在满足特定条件时启用的 JButton。程序初始值为setEnabled(false)
,更新后应该是setEnabled(true)
.
我尝试了几件事。这里有一些代码:
public class SwimAction extends AbstractAction {
private final PoolModel poolModel;
private final Swimmer swimmer;
public SwimAction(PoolModel poolModel, Swimmer swimmer) {
super("GO!");
this.poolModel = poolModel;
this.swimmer = swimmer;
// default
setEnabled(false);
}
我尝试了以下方法:
// Replaced the setEnabled(false) by setEnabled(checkGo())
public boolean checkGo(){
return(pool.isNotOccupied());
}
// Overwrite setEnabled
@Overwrite
public void setEnabled(boolean bool){
boolean oldBool = this.enabled;
if (oldBool != bool && pool.isNotOccupied()) {
this.enabled = bool;
this.firePropertyChange("enabled", oldBool, bool);
}
}
但是,其中 none 有效。任何人都知道如何在满足特定条件 (pool.isNotOccupied()
) 时启用按钮?
似乎您需要侦听池对象占用状态的变化属性,最好的方法是为其提供某种侦听器。这可以像 ChangeListener 一样简单,或者更好的是 PropertyChangeListener。最佳解决方案的细节可能在很大程度上取决于您的程序的结构,以及我们看不到的其余代码,但是,如果 PoolModel 是您正在收听的内容,如果您给它...
public class PoolModel {
public static final String OCCUPIED = "occupied";
private PropertyChangeSupport propChangeSupport = new PropertyChangeSupport(this);
private boolean occupied;
public void addPropertyChangeListener(PropertyChangeListener l) {
propChangeSupport.addPropertyChangeListener(l);
}
// also have a remove listener
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
boolean oldValue = this.occupied;
boolean newValue = occupied;
this.occupied = occupied;
propChangeSupport.firePropertyChange(OCCUPIED, oldValue, newValue);
}
// ......
然后在使用它的代码中:
poolModel.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals(OCCUPIED)) {
setEnabled((boolean) pcEvent.getNewValue());
}
});
我正在尝试创建一个在满足特定条件时启用的 JButton。程序初始值为setEnabled(false)
,更新后应该是setEnabled(true)
.
我尝试了几件事。这里有一些代码:
public class SwimAction extends AbstractAction {
private final PoolModel poolModel;
private final Swimmer swimmer;
public SwimAction(PoolModel poolModel, Swimmer swimmer) {
super("GO!");
this.poolModel = poolModel;
this.swimmer = swimmer;
// default
setEnabled(false);
}
我尝试了以下方法:
// Replaced the setEnabled(false) by setEnabled(checkGo())
public boolean checkGo(){
return(pool.isNotOccupied());
}
// Overwrite setEnabled
@Overwrite
public void setEnabled(boolean bool){
boolean oldBool = this.enabled;
if (oldBool != bool && pool.isNotOccupied()) {
this.enabled = bool;
this.firePropertyChange("enabled", oldBool, bool);
}
}
但是,其中 none 有效。任何人都知道如何在满足特定条件 (pool.isNotOccupied()
) 时启用按钮?
似乎您需要侦听池对象占用状态的变化属性,最好的方法是为其提供某种侦听器。这可以像 ChangeListener 一样简单,或者更好的是 PropertyChangeListener。最佳解决方案的细节可能在很大程度上取决于您的程序的结构,以及我们看不到的其余代码,但是,如果 PoolModel 是您正在收听的内容,如果您给它...
public class PoolModel {
public static final String OCCUPIED = "occupied";
private PropertyChangeSupport propChangeSupport = new PropertyChangeSupport(this);
private boolean occupied;
public void addPropertyChangeListener(PropertyChangeListener l) {
propChangeSupport.addPropertyChangeListener(l);
}
// also have a remove listener
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
boolean oldValue = this.occupied;
boolean newValue = occupied;
this.occupied = occupied;
propChangeSupport.firePropertyChange(OCCUPIED, oldValue, newValue);
}
// ......
然后在使用它的代码中:
poolModel.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals(OCCUPIED)) {
setEnabled((boolean) pcEvent.getNewValue());
}
});