public void init() { // 添加按钮 JPanel buttonPanel = new JPanel(); buttonPanel.add(okButton); mainPanel.setLayout(new GridLayout(0, 3)); mainWin.add(mainPanel, BorderLayout.CENTER); JFormattedTextField intField0 = new JFormattedTextField( new InternationalFormatter(NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return new NumberFilter(); } }); intField0.setValue(100); addRow("只接受数字的文本框", intField0); JFormattedTextField intField1 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField1.setValue(new Integer(100)); // 添加输入校验器 intField1.setInputVerifier(new FormattedTextFieldVerifier()); addRow("带输入校验器的文本框", intField1); // 创建自定义格式器对象 IPAddressFormatter ipFormatter = new IPAddressFormatter(); ipFormatter.setOverwriteMode(false); // 以自定义格式器对象创建格式化文本框 JFormattedTextField ipField = new JFormattedTextField(ipFormatter); ipField.setValue(new byte[] {(byte) 192, (byte) 168, 4, 1}); addRow("IP地址格式", ipField); mainWin.add(buttonPanel, BorderLayout.SOUTH); mainWin.pack(); mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWin.setVisible(true); }
/** Update the statistic */ public void setStats(int mi, double me, int ma, double sd) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); nf.setGroupingUsed(false); min.setText(Integer.toString(mi)); max.setText(Integer.toString(ma)); String s = nf.format(me); mean.setText(s); s = nf.format(sd); stdDev.setText(s); }