2012-03-19 35 views
3

我試圖使JSpinner只接受數字,但我也希望它讀取/響應退格。使JSpinner只讀取數字,但也檢測退格

public class test { 
    JFrame frame; 
    JPanel panel; 
    JSpinner spinner; 

    public test() 
    { 
     frame = new JFrame("test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setPreferredSize(new Dimension(200,200)); 

     panel = new JPanel(); 

     SpinnerNumberModel numSpin = new SpinnerNumberModel(10, 0,1000,1); 
     spinner = new JSpinner(numSpin); 
     JFormattedTextField txt = ((JSpinner.NumberEditor) spinner.getEditor()).getTextField(); 
     ((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false); 
     panel.add(spinner); 

     frame.setContentPane(panel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 

    } 
    public static void main(String[] args) 

    { 
     test test = new test(); 
    } 
} 

上面的代碼僅用於製作數字,但這不允許我退格。我發現這個網站的一些例子,但他們寫的C.

+0

請學習Java命名約定並嚴格遵守 – kleopatra 2012-03-20 11:21:43

+0

@kleopatra請KY – 2013-04-11 18:24:07

回答

11
  • 你是對的JFormattedTextField未正確實施JSpinner,你有工具DocumentFilter從keyboad輸入或從剪貼板粘貼的un_wanted Chars過濾, (感謝@StanislavL)

  • 你有selectAll()focusGained()裹成invokeLater()自己的問題解決了,

例如

import java.awt.BorderLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JSpinner; 
import javax.swing.SpinnerNumberModel; 
import javax.swing.SwingUtilities; 
import javax.swing.text.*; 

public class TestDigitsOnlySpinner { 

    public static void main(String... args) { 
     SwingUtilities.invokeLater((Runnable) new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame("enter digit"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter(); 
       frame.getContentPane().add(jspinner, BorderLayout.CENTER); 
       frame.getContentPane().add(new JButton("just another widget"), BorderLayout.SOUTH); 
       frame.pack(); 
       frame.setVisible(true); 
      } 

      private JSpinner makeDigitsOnlySpinner_BasicAttempt() { 
       JSpinner spinner = new JSpinner(new SpinnerNumberModel()); 
       return spinner; 
      } 

      private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() { 
       JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1)); 
       JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor(); 
       final Document jsDoc = jsEditor.getTextField().getDocument(); 
       if (jsDoc instanceof PlainDocument) { 
        AbstractDocument doc = new PlainDocument() { 

         private static final long serialVersionUID = 1L; 

         @Override 
         public void setDocumentFilter(DocumentFilter filter) { 
          if (filter instanceof MyDocumentFilter) { 
           super.setDocumentFilter(filter); 
          } 
         } 
        }; 
        doc.setDocumentFilter(new MyDocumentFilter()); 
        jsEditor.getTextField().setDocument(doc); 
       } 
       return spinner; 
      } 
     }); 
    } 

    private static class MyDocumentFilter extends DocumentFilter { 

     @Override 
     public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { 
      if (stringContainsOnlyDigits(string)) { 
       super.insertString(fb, offset, string, attr); 
      } 
     } 

     @Override 
     public void remove(FilterBypass fb, int offset, int length) throws BadLocationException { 
      super.remove(fb, offset, length); 
     } 

     @Override 
     public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { 
      if (stringContainsOnlyDigits(text)) { 
       super.replace(fb, offset, length, text, attrs); 
      } 
     } 

     private boolean stringContainsOnlyDigits(String text) { 
      for (int i = 0; i < text.length(); i++) { 
       if (!Character.isDigit(text.charAt(i))) { 
        return false; 
       } 
      } 
      return true; 
     } 
    } 

    private TestDigitsOnlySpinner() { 
    } 
} 
+0

沒錯工作謝謝:)一個簡單的問題,你會怎麼做光標出現後的數字,因此更容易退格? – 2012-03-20 02:46:53

+0

是否可以設置JSpinner中字符數量的限制。例如最多3個數字,所以您可以手動輸入的最大數字是999. – 2012-03-20 23:22:25

+0

是的,這很簡單,因爲這個原因,SpinnerNumberModel中的定義是'JSpinner spinner = new JSpinner(new SpinnerNumberModel(0,0,999,1 ));' – mKorbel 2012-03-20 23:26:00

相關問題