gpt4 book ai didi

java - JSpinner : how to display both numbers and text?

转载 作者:搜寻专家 更新时间:2023-11-01 03:25:13 25 4
gpt4 key购买 nike

我想创建一个 JSpinner,它可以在 指定的最小值指定的最大值 之间获取所有可能的 Double 值。

此外,JSpinner 应该能够显示文本而不是特定值。假设我们的 JSpinner 可以取 -1 到 10 之间的值。我想显示一个文本,例如“自动”,而不是 -1。

如何替换Normal Jspinner : we can see "-1"经过 JSpinner where we can see "Auto" instead of "-1" even if Auto equals actually -1

这是我写的模型,但似乎还不够,因为它说在 JSpinner 中有一个错误,因为文本不是 Double

public class SpinnerSpecialModel
extends AbstractSpinnerModel implements SpinnerMinMaxModel {

public static final double DEFAULT_MINIMUM = 0.0;
public static final double DEFAULT_MAXIMUM = Double.POSITIVE_INFINITY;
public static final double DEFAULT_STEP = 1.0;
public static final double DEFAULT_VALUE = 1.0;
public static final double DEFAULT_SPECIAL_NUMBER = -1.0;
public static final String DEFAULT_SPECIAL_TEXT = "Auto";

private double maximum;
private double minimum;
private double stepSize;
private double currentNumber;
private double specialNumber;
private String specialText;

private Object m_Value;

public SpinnerSpecialModel(double max, double min, double step, double num,
double specialNum, String specialTxt) {
maximum = max;
minimum = min;
stepSize = step;
currentNumber = num;
specialNumber = specialNum;
specialText = specialTxt;
setAccurateValue(num);
}

public SpinnerSpecialModel(double specialNum, String specialTxt) {
this(DEFAULT_MAXIMUM, DEFAULT_MINIMUM,
DEFAULT_STEP, DEFAULT_VALUE, specialNum, specialTxt);
}

public SpinnerSpecialModel() {
this(DEFAULT_SPECIAL_NUMBER, DEFAULT_SPECIAL_TEXT);
}

@Override
public Object getValue() {
if (currentNumber == specialNumber) {
m_Value = specialText;
}
else {
m_Value = currentNumber;
}
return m_Value;
}

@Override
public void setValue(Object value) {
setAccurateValue(value);
}

private void setAccurateValue(Object value) {
if (value instanceof Double) {
double doubleValue = (Double) value;
if (doubleValue != currentNumber) {
if (doubleValue == specialNumber) {
currentNumber = specialNumber;
m_Value = specialText;
}
else if (doubleValue > maximum) {
currentNumber = maximum;
m_Value = maximum;
}
else if (doubleValue < minimum) {
currentNumber = maximum;
m_Value = minimum;
}
else {
currentNumber = doubleValue;
m_Value = doubleValue;
}
fireStateChanged();
}
}

if (value instanceof String) {
String stringValue = (String) value;
if (stringValue.equals(specialText)) {
this.currentNumber = specialNumber;
this.m_Value = specialText;
fireStateChanged();
}
}
}

@Override
public Object getNextValue() {
return getNewValue(+1);
}

@Override
public Object getPreviousValue() {
return getNewValue(-1);
}

/**
*
* @param direction
* @return
*/
private Object getNewValue(int direction) {
double newValue = currentNumber + direction * stepSize;
setAccurateValue(newValue);
return m_Value;
}

@Override
public double getMaximum() {
return maximum;
}

@Override
public double getMinimum() {
return minimum;
}

@Override
public double getStepSize() {
return stepSize;
}

@Override
public void setMaximum(double max) {
maximum = max;
}

@Override
public void setMinimum(double min) {
minimum = min;
}

@Override
public void setStepSize(double step) {
stepSize = step;
}
}

最佳答案

做到这一点的最好和正确的方法不是写一个模型那么简单,但也不是很复杂。您实际上需要编写一个 Editor 和一个 Formatter 才能拥有真正的 MVC 微调器:

  • 一个扩展 JSpinner 的类:SpecialValuesSpinner
  • 一个实现 SpinnerModel 的类:SpecialValuesSpinnerModel
  • 扩展 DefaultEditor 并实现 DocumentListener 的类:SpecialValuesSpinnerEditor
  • 一个扩展 NumberFormatter 的类:SpecialValuesSpinnerFormatter

我不会向您展示所有类的代码,但基本上您必须在每个类中执行以下操作:

特殊值微调器:

public class SpecialValuesSpinner() extends SpinnerNumberModel {
// in your constructor do this
setModel(new SpecialValuesSpinnerModel(YOUR_SPECIAL_VALUES);
setEditor(new SpecialValuesSpinnerEditor());
}

特殊值微调器模型:

public class SpinnerSpecialValuesModel() extends JSpinner {
// in this class you handle the fact that now, you have an
// interval of values and a list of special values that are allowed.
// here is what I did :
@Override
public Object getNextValue() {
return incrValue(+1);
}

@Override
public Object getPreviousValue() {
return incrValue(-1);
}

private Object incrValue(int dir) {
// NB : BigDecimal here because this is what I used,
// but use what you want in your model
BigDecimal result = null;
BigDecimal numberBD = new BigDecimal(getNumber().toString());
BigDecimal stepSizeBD = new BigDecimal(getStepSize().toString());
BigDecimal dirBD = new BigDecimal(dir);
BigDecimal nextValue = numberBD.add(stepSizeBD.multiply(dirBD));

TreeSet<BigDecimal> currentAllowedValues = new TreeSet<BigDecimal>();
currentAllowedValues.addAll(m_SpecialValues);
if (getMaximum() != null) {
currentAllowedValues.add((BigDecimal) getMaximum());
}
if (getMinimum() != null) {
currentAllowedValues.add((BigDecimal) getMinimum());
}
if (isIncludedInBounds(nextValue)) {
currentAllowedValues.add(nextValue);
}

if (dir > 0) {
try {
result = currentAllowedValues.higher(numberBD);
}
catch (NoSuchElementException e) {}
}
else if (dir < 0) {
try {
result = currentAllowedValues.lower(numberBD);
}
catch (NoSuchElementException e) {}
}
return result;
}
}

在 SpecialValuesSpinnerEditor 中,我们使用 Document Listener 来自动完成(很容易做到,只需搜索 SO)。

public class SpecialValuesSpinnerEditor extends DefaultEditor implements DocumentListener {

// You have to do in your contructor
SpecialValuesSpinnerFormatter formatter =
new SpecialValuesSpinnerFormatter (spinner.getSpecialValues(), format);
getTextField().setFormatterFactory(new DefaultFormatterFactory(formatter));
}

现在,最重要的是格式化程序,它在用户输入(字符串)和数字之间进行转换,并处理模型的显示:

public class SpecialValuesSpinnerFormatter extends NumberFormatter {
// Just override the methos StringToValue and ValueToString.
// You can check here if the value is special
// i.e you must display its special text instead. e.g. : "Auto" instead of -1
}

关于java - JSpinner : how to display both numbers and text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com