如何用已实现的Setters正确更改SpinnerNumberModel的最大值和最小值(How to change max and min of a SpinnerNumberModel prope

编程入门 行业动态 更新时间:2024-10-26 08:33:07
如何用已实现的Setters正确更改SpinnerNumberModel的最大值和最小值(How to change max and min of a SpinnerNumberModel properly with the implemented Setters)

我想用一个SpinnerNumberModel实现一个JSpinner到一个GUI 。 在GUI构建完成 , value, min, max, step将从ini文件中读取,因此我需要使用SpinnerNumberModel类中的Setters来更改它们的SpinnerNumberModel 。 这些值是以String从文件中读取的,然后解析为double 。

JSpinner和SpinnerNumberModel在类InputSpinner中实现:

public class InputSpinner implements ChangeListener { JSpinner component; SpinnerNumberModel model; //empty constructor public InputSpinner() { model = new SpinnerNumberModel(); component = new JSpinner(model); JComponent field = ((JSpinner.DefaultEditor) component.getEditor()); Dimension prefSize = field.getPreferredSize(); prefSize = new Dimension(50, prefSize.height); field.setPreferredSize(prefSize); component.addChangeListener(this); } //constructor with name public InputSpinner(String name) { model = new SpinnerNumberModel(); component = new JSpinner(model); component.setName(name); JComponent field = ((JSpinner.DefaultEditor) component.getEditor()); Dimension prefSize = field.getPreferredSize(); prefSize = new Dimension(50, prefSize.height); field.setPreferredSize(prefSize); component.addChangeListener(this); } //basic properties for all possible components public void setName(String componentName) { component.setName(componentName); } public void setToolTipText(String tooltip) { component.setToolTipText(tooltip); } public void setValue (Number value) { model.setValue(value); } public void setMaximum (Number max) { model.setMaximum((Comparable<Double>)max); } public void setMinimum (Number min) { model.setMinimum((Comparable<Double>)min); } public void setStepSize (Number step) { model.setStepSize(step); } //getter for item public JComponent getComponent() { return this.component; } }

由于StepSize和Value的Setters具有Number作为传输参数,因此我可以在将SpinnerNumberModel和JSpinner都实例化后设置它们,并且它可以以这种方式工作。

min和max的Setters要求类型为Comparable 。 简单地将double值解析为Comparable (raw type)或Comparable<Double>不会导致异常,但也会导致JSpinner无法正常工作。 (初始值不能通过编辑或使用编辑器附带的2个按钮来改变)。

当我将JSpinner添加到我的GUI中,然后更改这些值时,我会这样做:

public class main { public static void main(String[] args) { JFrame frame = new JFrame("frame"); InputSpinner spinner = new InputSpinner("spinner"); frame.add(spinner.getComponent()); spinner.setValue(some double value I got from ini-file); //works properly spinner.setStepSize(some double value I got from ini-file); //works properly spinner.setMinimum(some double value I got from ini-file); //causes problems spinner.setMaximum(some double value I got from ini-file); //causes problems frame.pack(); frame.setVisible(true); return; }

}

我如何正确设定值,我应该如何处理Comparable的需求?

I want to implement a JSpinner with a SpinnerNumberModel to a GUI. The values for value, min, max, step are read from a ini-file after the GUI gets built, therefore I need to change them with the Setters from the SpinnerNumberModel class. The values are read from the file as a String, then get parsed to double.

The JSpinner and SpinnerNumberModel are implemented in a class InputSpinner:

public class InputSpinner implements ChangeListener { JSpinner component; SpinnerNumberModel model; //empty constructor public InputSpinner() { model = new SpinnerNumberModel(); component = new JSpinner(model); JComponent field = ((JSpinner.DefaultEditor) component.getEditor()); Dimension prefSize = field.getPreferredSize(); prefSize = new Dimension(50, prefSize.height); field.setPreferredSize(prefSize); component.addChangeListener(this); } //constructor with name public InputSpinner(String name) { model = new SpinnerNumberModel(); component = new JSpinner(model); component.setName(name); JComponent field = ((JSpinner.DefaultEditor) component.getEditor()); Dimension prefSize = field.getPreferredSize(); prefSize = new Dimension(50, prefSize.height); field.setPreferredSize(prefSize); component.addChangeListener(this); } //basic properties for all possible components public void setName(String componentName) { component.setName(componentName); } public void setToolTipText(String tooltip) { component.setToolTipText(tooltip); } public void setValue (Number value) { model.setValue(value); } public void setMaximum (Number max) { model.setMaximum((Comparable<Double>)max); } public void setMinimum (Number min) { model.setMinimum((Comparable<Double>)min); } public void setStepSize (Number step) { model.setStepSize(step); } //getter for item public JComponent getComponent() { return this.component; } }

As the Setters for StepSize and Value have Number as transfer parameters, I can set them after instancing both the SpinnerNumberModel and the JSpinner and it obviosly works that way.

The Setters for min and max otherwise demand the type Comparable. Simply parsing the double values to Comparable (raw type) or Comparable<Double> does not cause an exception, but also causes the JSpinner to not work properly anymore. (The initial value cannot be changed neither by editing nor by using the 2 Buttons that come with the editor).

When I add a JSpinner to my GUI and then change the values I do it like this:

public class main { public static void main(String[] args) { JFrame frame = new JFrame("frame"); InputSpinner spinner = new InputSpinner("spinner"); frame.add(spinner.getComponent()); spinner.setValue(some double value I got from ini-file); //works properly spinner.setStepSize(some double value I got from ini-file); //works properly spinner.setMinimum(some double value I got from ini-file); //causes problems spinner.setMaximum(some double value I got from ini-file); //causes problems frame.pack(); frame.setVisible(true); return; }

}

How do I set the values properly, respectively how should I deal with the demand for Comparable ?

最满意答案

我不知道为什么你的设置方法不能使用double,但我找到了使用double作为current,min,max和stepsize的方法。

如果你创建你的模型,你可以像这样在构造函数中传递值:

double current = 5.7; double min = (double) 5.5; double max = (double) 7.3; double step = 0.1; model = new SpinnerNumberModel(current, min, max, step);

不管你使用哪个值,但是如果你想进一步使用double,它们看起来是双倍的,因为那么这个代码也可以工作:

JFrame frame = new JFrame("frame"); InputSpinner spinner = new InputSpinner("spinner"); frame.add(spinner.getComponent()); spinner.setValue(5.1); spinner.setMinimum(1.0); spinner.setMaximum(25.3); spinner.setStepSize(2.5); frame.pack(); frame.setVisible(true);

就像我看到的那样,你必须将步长设置为最后一个值,但现在它的工作很好

i don't know why exactly your set methods don't work with double, but i found a way to use double as current, min , max and stepsize.

if you create you model, you pass the values in constructor like this:

double current = 5.7; double min = (double) 5.5; double max = (double) 7.3; double step = 0.1; model = new SpinnerNumberModel(current, min, max, step);

it doesn't matter which values you use, but they seem to be double if you want to use double further, because then this code works as well:

JFrame frame = new JFrame("frame"); InputSpinner spinner = new InputSpinner("spinner"); frame.add(spinner.getComponent()); spinner.setValue(5.1); spinner.setMinimum(1.0); spinner.setMaximum(25.3); spinner.setStepSize(2.5); frame.pack(); frame.setVisible(true);

as i see it, you have to set step size as last value somewhy, but now its working just fine

更多推荐

本文发布于:2023-07-24 19:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   如何用   最小值   正确   SpinnerNumberModel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!