绑定两个 JSpinners 递增和递减
binding two JSpinners increment and decrement
我已经用 netbeans Form 创建了两个 JSpinner,我想 link 这两个 JSpinner,这样如果其中一个的值减少,另一个的值就会增加,反之亦然。我试过这段代码,但它不起作用:
int currentValue = durexep_spin.getValue();
private void durexep_spinPropertyChange(java.beans.PropertyChangeEvent evt) {
int p = soldexep_spin.getValue();
int q = durexep_spin.getValue();
if(q<currentValue){
soldexep_spin.setValue(p+1);
}
else if (q>currentValue){
soldexep_spin.setValue(p-1);
}
您可以创建 javax.swing.event.ChangeListener
的子类,并在其构造函数中使用两个引用:JSPinner 基类和 JSpinner 图像。然后编写 stateChanged
方法以根据基数的当前值更新图像的值(假设您知道两个值的总和)。
最后,您只需实例化两个侦听器实例并将一个附加到每个 JSpinner。
{
// ... Initialization of the JPanel ...
int constantSum=10;
soldexep_spin.addChangeListener(new MyListener(soldexep_spin, durexep_spin, constantSum));
durexep_spin.addChangeListener(new MyListener(durexep_spin, soldexep_spin, constantSum));
}
private class MyListener implements javax.swing.event.ChangeListener
{
private final JSpinner base;
private final JSpinner image;
private final int constantSum;
public MyListener(JSpinner base, JSpinner image, int constantSum)
{
super();
this.base=base;
this.image=image;
this.constantSum=constantSum;
// Initializes the image value in a coherent state:
updateImage();
}
public void stateChanged(ChangeEvent e)
{
updateImage();
}
private void updateImage()
{
int baseValue=((Number)this.base.getValue()).intValue();
int imageValue=((Number)this.image.getValue()).intValue();
int newImageValue=this.constantSum - baseValue;
if (imageValue != newImageValue)
{
// Avoid an infinite loop of changes if the image value was already correct.
this.image.setValue(newImageValue);
}
}
我已经用 netbeans Form 创建了两个 JSpinner,我想 link 这两个 JSpinner,这样如果其中一个的值减少,另一个的值就会增加,反之亦然。我试过这段代码,但它不起作用:
int currentValue = durexep_spin.getValue();
private void durexep_spinPropertyChange(java.beans.PropertyChangeEvent evt) {
int p = soldexep_spin.getValue();
int q = durexep_spin.getValue();
if(q<currentValue){
soldexep_spin.setValue(p+1);
}
else if (q>currentValue){
soldexep_spin.setValue(p-1);
}
您可以创建 javax.swing.event.ChangeListener
的子类,并在其构造函数中使用两个引用:JSPinner 基类和 JSpinner 图像。然后编写 stateChanged
方法以根据基数的当前值更新图像的值(假设您知道两个值的总和)。
最后,您只需实例化两个侦听器实例并将一个附加到每个 JSpinner。
{
// ... Initialization of the JPanel ...
int constantSum=10;
soldexep_spin.addChangeListener(new MyListener(soldexep_spin, durexep_spin, constantSum));
durexep_spin.addChangeListener(new MyListener(durexep_spin, soldexep_spin, constantSum));
}
private class MyListener implements javax.swing.event.ChangeListener
{
private final JSpinner base;
private final JSpinner image;
private final int constantSum;
public MyListener(JSpinner base, JSpinner image, int constantSum)
{
super();
this.base=base;
this.image=image;
this.constantSum=constantSum;
// Initializes the image value in a coherent state:
updateImage();
}
public void stateChanged(ChangeEvent e)
{
updateImage();
}
private void updateImage()
{
int baseValue=((Number)this.base.getValue()).intValue();
int imageValue=((Number)this.image.getValue()).intValue();
int newImageValue=this.constantSum - baseValue;
if (imageValue != newImageValue)
{
// Avoid an infinite loop of changes if the image value was already correct.
this.image.setValue(newImageValue);
}
}