如何在spinner中显示十六进制的数?是要自己去写editor么?
解决方案 »
- 北大青鸟 S1java全部课件 供学习者下载
- 关于java中 子类显式调用父类的构造函数问题,请大家帮忙看看...
- 我是新手~~请大虾帮我解决以下问题是处在哪?
- 关于Java中byte的问题,如何得到范围外字符的编码(我感觉有难度)
- 高分求助:8859_1 与 GBK 之间的转换
- 哪里有有关JUNIT的中文资料啊?顺便给大家散散分,过年了吗?高兴
- 一个菜鸟的问题(很简单)?
- 对于ORACLE8i,如何用java实现存储过程?
- 谁能解决applet 的权限问题!!!
- Java中n!的算法?怎么算?
- 求一段解析字符串中数据的最优代码~~~~~~~~~!!!
- 求教java调用c语法问题,在现线等,非常急
import java.text.ParseException;import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.DefaultFormatterFactory;public class T {
public static void main(String[] args) {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(50, 0, 100, 1));
final JFormattedTextField.AbstractFormatter formatter = new JFormattedTextField.AbstractFormatter() {
public Object stringToValue(String text) throws ParseException {
if (text != null && text.startsWith("0x")) {
text = text.substring(2);
}
return new Integer(Integer.parseInt(text, 16));
} public String valueToString(Object value) throws ParseException {
int v = ((Number)value).intValue();
return "0x" + Integer.toHexString(v).toUpperCase();
}
};
class HexNumberEditor extends JSpinner.DefaultEditor {
public HexNumberEditor(JSpinner spinner) {
super(spinner);
if (!(spinner.getModel() instanceof SpinnerNumberModel)) {
throw new IllegalArgumentException("model not a SpinnerNumberModel");
}
getTextField().setEditable(true);
getTextField().setFormatterFactory(new DefaultFormatterFactory(formatter));
}
}
spinner.setEditor(new HexNumberEditor(spinner));
JFrame f = new JFrame();
f.getContentPane().add(spinner, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}