SpinnerModel 功能示例 Java Swing
SpinnerModel functioning example Java Swing
我正在尝试为 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 可以用上面提到的模型做点什么,后来我想实现 Changelisteners。
到目前为止,我什至无法正确绘制它。有人可以帮助我吗?
我需要使用 Class 的实例实现 SpinnerModel 作为 JSpinner 的模型,但我无法让它工作。
BR
丹麦
这是失败的保证:
@Override
public Object getValue()
{
return null;
}
因为这是 JSpinner 用来确定要显示的值的方法。
取而代之的是 return 您的 value
字段所持有的值。此外,如果下一个值高于最大值,则不要 return null
getNextValue()
。相反 return 最大值。同样对于 getPreviousValue()
,return 如果计算的先前值小于最小值,则为最小值。
例如,
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
}
}
}
请注意,如果可用,通常最好使用扩展抽象模型 class(或者甚至更好,默认模型 class,但 none 可用于旋转模型)。更好的是:
@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();
}
}
我正在尝试为 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 可以用上面提到的模型做点什么,后来我想实现 Changelisteners。
到目前为止,我什至无法正确绘制它。有人可以帮助我吗?
我需要使用 Class 的实例实现 SpinnerModel 作为 JSpinner 的模型,但我无法让它工作。
BR 丹麦
这是失败的保证:
@Override
public Object getValue()
{
return null;
}
因为这是 JSpinner 用来确定要显示的值的方法。
取而代之的是 return 您的 value
字段所持有的值。此外,如果下一个值高于最大值,则不要 return null
getNextValue()
。相反 return 最大值。同样对于 getPreviousValue()
,return 如果计算的先前值小于最小值,则为最小值。
例如,
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
}
}
}
请注意,如果可用,通常最好使用扩展抽象模型 class(或者甚至更好,默认模型 class,但 none 可用于旋转模型)。更好的是:
@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();
}
}