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); }
public FormatTestFrame() { JPanel buttonPanel = new JPanel(); okButton = new JButton("Ok"); buttonPanel.add(okButton); add(buttonPanel, BorderLayout.SOUTH); mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(0, 3)); add(mainPanel, BorderLayout.CENTER); JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField.setValue(new Integer(100)); addRow("Number:", intField); JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField2.setValue(new Integer(100)); intField2.setFocusLostBehavior(JFormattedTextField.COMMIT); addRow("Number (Commit behavior):", intField2); JFormattedTextField intField3 = new JFormattedTextField( new InternationalFormatter(NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return filter; } }); intField3.setValue(new Integer(100)); addRow("Filtered Number", intField3); JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance()); intField4.setValue(new Integer(100)); intField4.setInputVerifier( new InputVerifier() { public boolean verify(JComponent component) { JFormattedTextField field = (JFormattedTextField) component; return field.isEditValid(); } }); addRow("Verified Number:", intField4); JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance()); currencyField.setValue(new Double(10)); addRow("Currency:", currencyField); JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance()); dateField.setValue(new Date()); addRow("Date (default):", dateField); DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT); format.setLenient(false); JFormattedTextField dateField2 = new JFormattedTextField(format); dateField2.setValue(new Date()); addRow("Date (short, not lenient):", dateField2); try { DefaultFormatter formatter = new DefaultFormatter(); formatter.setOverwriteMode(false); JFormattedTextField urlField = new JFormattedTextField(formatter); urlField.setValue(new URL("http://java.sun.com")); addRow("URL:", urlField); } catch (MalformedURLException ex) { ex.printStackTrace(); } try { MaskFormatter formatter = new MaskFormatter("###-##-####"); formatter.setPlaceholderCharacter('0'); JFormattedTextField ssnField = new JFormattedTextField(formatter); ssnField.setValue("078-05-1120"); addRow("SSN Mask:", ssnField); } catch (ParseException ex) { ex.printStackTrace(); } JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter()); ipField.setValue(new byte[] {(byte) 130, 65, 86, 66}); addRow("IP Address:", ipField); pack(); }
public JobForm() { final JobForm form = this; this.setContentPane(mainPanel); this.setModal(true); saveButton.setEnabled(false); ButtonGroup group = new ButtonGroup(); group.add(onDemandRadioButton); group.add(scheduledRadioButton); jobNameTextField.addKeyListener( new KeyAdapter() { @Override public void keyTyped(KeyEvent keyEvent) { super.keyTyped(keyEvent); saveButton.setEnabled(!jobNameTextField.getText().isEmpty()); } }); ActionListener radioActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { setEnabledOptions(); } }; scheduledRadioButton.addActionListener(radioActionListener); onDemandRadioButton.addActionListener(radioActionListener); intervalUnitComboBox.setModel(new DefaultComboBoxModel(Job.getUnits())); scheduledRadioButton.setSelected(true); intervalFormattedTextField.setText("15"); setEnabledOptions(); intervalFormattedTextField.setInputVerifier( new InputVerifier() { @Override public boolean verify(JComponent jComponent) { if (jComponent instanceof JFormattedTextField) { JFormattedTextField field = (JFormattedTextField) jComponent; try { parseInt(field.getText()); } catch (NumberFormatException e) { return false; } return true; } return false; } }); saveButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { try { form.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); String jobName = jobNameTextField.getText().trim(); int interval = onDemandRadioButton.isSelected() ? 0 : parseInt(intervalFormattedTextField.getText()); String unit = onDemandRadioButton.isSelected() ? "none" : Job.getUnits()[intervalUnitComboBox.getSelectedIndex()]; SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH); String now = ISO8601DATEFORMAT.format(new Date()); if (!jobName.matches("^[A-Za-z][A-Za-z0-9_]+")) { form.setCursor(Cursor.getDefaultCursor()); JOptionPane.showMessageDialog( form, "Invalid service name. Job name must start with a letter, \n" + "contain only letters, numbers, and undercores.", "Error creating the job", JOptionPane.ERROR_MESSAGE); return; } if (existingJobNames == null) { existingJobNames = new ArrayList<String>(); for (Job job : AzureRestAPIManager.getManager().listJobs(subscriptionId, serviceName)) { existingJobNames.add(job.getName().toLowerCase()); } } if (existingJobNames.contains(jobName.toLowerCase())) { form.setCursor(Cursor.getDefaultCursor()); JOptionPane.showMessageDialog( form, "Invalid job name. A job with that name already exists in this service.", "Error creating the job", JOptionPane.ERROR_MESSAGE); return; } if (id == null) AzureRestAPIManager.getManager() .createJob(subscriptionId, serviceName, jobName, interval, unit, now); else { AzureRestAPIManager.getManager() .updateJob( subscriptionId, serviceName, jobName, interval, unit, now, enabledCheckBox.isSelected()); } if (afterSave != null) afterSave.run(); form.setCursor(Cursor.getDefaultCursor()); form.setVisible(false); form.dispose(); } catch (Throwable ex) { form.setCursor(Cursor.getDefaultCursor()); UIHelper.showException("Error trying to save job", ex); } } }); cancelButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { form.setVisible(false); form.dispose(); } }); }