我正在尝试为JSpinner制作客户模型,但它不起作用。 代码如下:
public class ModelJSpinner implements SpinnerModel
{
private long value;
private long min;
private long max;
private long increment;
private ChangeListener l;
private ArrayList<ChangeListener> listeners;
@Override
public Object getValue()
{
return null;
}
public ModelJSpinner(long min, long max, long increment)
{
super();
this.min = min;
this.max = max;
this.increment = increment;
setValue(min);
listeners = new ArrayList<>();
}
@Override
public void setValue(Object value)
{
if (value == null)
{
}else {
this.value = (Long) value;
}
//fireStateChanged();
}
private void fireStateChanged()
{
if (listeners == null)
return;
for (int a = 0; a < listeners.size(); a++)
{
ChangeListener l = (ChangeListener) listeners.get(a);
try
{
l.stateChanged(new ChangeEvent(this));
}
catch (RuntimeException e)
{
e.printStackTrace();
}
}
}
@Override
public Object getNextValue()
{
Long nextValue = value + increment;
if (nextValue > max)
{
return null;
}
else
{
return nextValue;
}
}
@Override
public Object getPreviousValue()
{
Long previousValue = value - increment;
if (previousValue < min)
{
return null;
}
else
{
return previousValue;
}
}
@Override
public void addChangeListener(javax.swing.event.ChangeListener l)
{
this.l = l;
listeners.add(l);
}
@Override
public void removeChangeListener(javax.swing.event.ChangeListener l)
{
if (this.l == l)
{
l = null;
}
listeners.add(l);
}
}
然而,当我运行以下代码时,我得到的......除了JSpinner之外没什么可做的......
public class Test
{
public static void main(String[] args)
{
ModelJSpinner model = new ModelJSpinner(10L, 20L, 5L);
JSpinner spinner = new JSpinner(model);
spinner.setModel(model);
spinner.setValue(15L);
JFrame frame = new JFrame("adasasd");
frame.setSize(350, 150);
frame.add(spinner);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
一开始,我想要的只是看到JSpinner可以用上面提到的Model做某事,后来我想实现Changelisteners。
截至目前,我甚至无法正确绘制。有人可以帮助我吗?
我需要使用实现SpinnerModel的Class实例作为JSpinner的模型,但我无法使其工作。
BR DK
答案 0 :(得分:2)
这是失败的保证:
@Override
public Object getValue()
{
return null;
}
因为这是JSpinner用来确定要显示的值的方法。
而是返回您的value
字段所持有的值。另外,如果下一个值高于最大值,请不要为null
返回getNextValue()
。而是返回最大值。类似地,对于getPreviousValue()
,如果计算的先前值小于min,则返回最小值。
例如,
public class SpinnerModel3 implements SpinnerModel {
private long value;
private long min;
private long max;
private long increment;
// using a set to avoid allowing addition of duplicate listeners
private Set<ChangeListener> listenerSet = new HashSet<>();
public SpinnerModel3(long value, long min, long max, long increment) {
super();
this.value = value;
this.min = min;
this.max = max;
this.increment = increment;
}
@Override
public void addChangeListener(ChangeListener l) {
listenerSet.add(l);
}
@Override
public Object getNextValue() {
long nextValue = value + increment;
nextValue = Math.min(nextValue, max);
return nextValue;
}
@Override
public Object getPreviousValue() {
long prevValue = value - increment;
prevValue = Math.max(prevValue, min);
return prevValue;
}
@Override
public Object getValue() {
return value;
}
@Override
public void removeChangeListener(ChangeListener l) {
listenerSet.remove(l);
}
@Override
public void setValue(Object value) {
this.value = (long) value;
fireStateChanged();
}
protected void fireStateChanged() {
// create a ChangeEvent object
ChangeEvent e = new ChangeEvent(this);
for (ChangeListener l : listenerSet) {
l.stateChanged(e); // notify all listeners
}
}
}
请注意,通常最好使用扩展抽象模型类(如果有的话)(或者甚至更好,默认模型类,但没有可用于微调器模型)。更好的是:
@SuppressWarnings("serial")
public class SpinnerModel2 extends AbstractSpinnerModel {
private long value;
private long min;
private long max;
private long increment;
public SpinnerModel2(long value, long min, long max, long increment) {
super();
this.value = value;
this.min = min;
this.max = max;
this.increment = increment;
}
@Override
public Object getNextValue() {
long nextValue = value + increment;
nextValue = Math.min(nextValue, max);
return nextValue;
}
@Override
public Object getPreviousValue() {
long prevValue = value - increment;
prevValue = Math.max(prevValue, min);
return prevValue;
}
@Override
public Object getValue() {
return value;
}
@Override
public void setValue(Object value) {
this.value = (long) value;
fireStateChanged();
}
}