本文介绍了JSpinner:显示一系列带符号的HexBinary值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 JSpinner
?
中显示一系列签名的HexBinary数字的最佳方式是什么?从 0x8000
到 0x7ffe
e.g. from 0x8000
to 0x7ffe
我试过以下解决方案,但没有太多的运气:
I have tried the following solutions, without much luck:
- 使用默认格式化程序将
JSpinnerNumberModel
转换从int到Hexbinary。[不能显示范围的负数]
- 使用
JSpinnerListModel
并将构造的HexBinary值列表(不需要代码的人为的解决方案并不完美)。
- 使用
- Use a
JSpinnerNumberModel
with a default formatter to convert from int to Hexbinary.[Cannot display negative part of the range] - Use a
JSpinnerListModel
and pass it a constructed list of HexBinary values that fall in the range (contrived solution with unnecessary code. doesn't work perfectly).
是否有更好的通用解决方案?
Is there a better, generic solution?
推荐答案
一种方法是扩展 AbstractSpinnerModel
来创建一个 LongNumberModel
,如下所示。另请参阅此相关的。
One approach would be to extend AbstractSpinnerModel
to create a LongNumberModel
, as shown below. See also this related example.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.text.ParseException;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;
/**
* @see https://stackoverflow.com/a/13121724/230513
* @see https://stackoverflow.com/a/9758714/230513
*/
public class HexSpinnerTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
private static void display() throws HeadlessException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner sp = new JSpinner(new LongNumberModel(0x8000L, 0x8000L, 0xFFFFL, 1L));
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) sp.getEditor();
JFormattedTextField tf = editor.getTextField();
tf.setFormatterFactory(new MyFormatterFactory());
f.getContentPane().add(sp, BorderLayout.NORTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class LongNumberModel extends AbstractSpinnerModel {
private Long value, stepSize;
private Comparable<Long> minimum, maximum;
public LongNumberModel(Long value, Long minimum, Long maximum, Long stepSize) {
this.value = value;
this.minimum = minimum;
this.maximum = maximum;
this.stepSize = stepSize;
}
@Override
public Object getValue() {
return value;
}
@Override
public void setValue(Object value) {
this.value = (Long) value;
fireStateChanged();
}
@Override
public Object getNextValue() {
long v = value.longValue() + stepSize.longValue();
return bounded(v);
}
@Override
public Object getPreviousValue() {
long v = value.longValue() - stepSize.longValue();
return bounded(v);
}
private Object bounded(long v) {
if ((maximum != null) && (maximum.compareTo(v) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(v) > 0)) {
return null;
}
return Long.valueOf(v);
}
}
private static class MyFormatterFactory extends DefaultFormatterFactory {
@Override
public AbstractFormatter getDefaultFormatter() {
return new HexFormatter();
}
}
private static class HexFormatter extends DefaultFormatter {
@Override
public Object stringToValue(String text) throws ParseException {
try {
return Long.valueOf(text, 16);
} catch (NumberFormatException nfe) {
throw new ParseException(text, 0);
}
}
@Override
public String valueToString(Object value) throws ParseException {
return Long.toHexString(
((Long) value).intValue()).toUpperCase();
}
}
}
这篇关于JSpinner:显示一系列带符号的HexBinary值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!