十六进制Jspinner的一些提示?我的方法是正确的 [英] Some hints for hex Jspinner ? I s the approach correct
本文介绍了十六进制Jspinner的一些提示?我的方法是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让JSpinner将十六进制值从0x0000000旋转到0xffffffff。尝试扩展abstractspinner模型但失败了。我的方法是否正确,是否有任何其他解决方案。一些帮助将非常有用
i want t make a JSpinner to spin hex values from 0x0000000 to 0xffffffff . Tried extending the abstractspinner model but failed . Is my approach correct and is there any other solution.some help would be really usefull
推荐答案
这是一种可以帮助你的方法
Here is one way which might help you
import java.awt.BorderLayout;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;
public class HexSpinnerTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner sp = new JSpinner(new SpinnerNumberModel(0,0,10000,1));
JSpinner.DefaultEditor editor =
(JSpinner.DefaultEditor)sp.getEditor();
JFormattedTextField tf = editor.getTextField();
tf.setFormatterFactory(new MyFormatterFactory());
f.getContentPane().add(sp, BorderLayout.NORTH);
f.setSize(200,200);
f.setVisible(true);
}
private static class MyFormatterFactory extends DefaultFormatterFactory {
public AbstractFormatter getDefaultFormatter() {
return new HexFormatter();
}
}
private static class HexFormatter extends DefaultFormatter {
public Object stringToValue(String text) throws ParseException {
try {
return Long.valueOf(text, 16);
} catch (NumberFormatException nfe) {
throw new ParseException(text,0);
}
}
public String valueToString(Object value) throws ParseException {
return Integer.toHexString(
((Integer)value).intValue()).toUpperCase();
}
}
}
我已经为0做了到10000,它们将被转换为HEX。
I have done it for 0 to 10000, which are getting converted to HEX.
这篇关于十六进制Jspinner的一些提示?我的方法是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文