1

以下の機能を備えたJSpinnerを作成する必要があります。

  • 範囲は 0 ~ 1000 で、10 刻みで指定します。
  • 表示形式は 4 桁である必要があります。(例: 10 -> 0010)
  • ユーザーが手動で値を入力できるようにする必要があります。
  • 数字以外の文字は使用できません。
  • 3 桁を超える値を入力することはできません。

これは私の現在の実装です:

spinnerUpDown.setModel(new SpinnerNumberModel(0,0,1000,10));
spinnerUpDown.setEditor(new JSpinner.NumberEditor(spinnerUpDown,"0000"));
JFormattedTextField txt = ((JSpinner.NumberEditor) spinnerUpDown.getEditor()).getTextField();
((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);

その実装の問題は、ユーザーが手動で値を入力できないことです。

行を削除すると:

((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);

手動で値を入力できますが、文字も入力できます。

この問題を克服するための提案。ありがとうございました!!

4

2 に答える 2

2

JSpinner通常JTextComponent、エディタには a を使用します。エディターを入手して、それに適用することができますDocumentFilter

これにより、ドキュメントに入ってくるテキストをフィルタリングできます。

これらのよく引用される例を確認してください

JFormattedTextField独自にインストールしていDocumentFilterます。PlainDocument独自のフィルターを返す独自のフィルターを提供することで、これを克服できます

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

public class TestSpinner01 {

    public static void main(String[] args) {
        new TestSpinner01();
    }

    public TestSpinner01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JSpinner spinnerUpDown = new JSpinner();
                spinnerUpDown.setModel(new SpinnerNumberModel(0, 0, 1000, 10));
                spinnerUpDown.setEditor(new JSpinner.NumberEditor(spinnerUpDown, "0000"));
                System.out.println(spinnerUpDown.getEditor());


                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(spinnerUpDown);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                PlainDocument doc = new PlainDocument() {
                    private TestFilter filter;
                    @Override
                    public DocumentFilter getDocumentFilter() {
                        if (filter == null) {
                            filter = new TestFilter();
                        }
                        return filter;
                    }

                };

                JTextComponent txt = ((JSpinner.DefaultEditor) spinnerUpDown.getEditor()).getTextField();
                txt.setDocument(doc);
            }
        });
    }

    public class TestFilter extends DocumentFilter {

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

        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            System.out.println("insert");
            super.insertString(fb, offset, string, attr);
        }

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            System.out.println("Replace...");
            super.replace(fb, offset, length, text, attrs);
        }
    }
}

奇妙なことに、サンプルコードは私にとってはうまく機能しましたが、実際に抱えていた問題はフォーマッターに関係していました

于 2013-05-19T07:57:28.903 に答える
0

に登録KeyListenerJFormattedTextFieldます。このkeyPressed()メソッドでは、入力されたキーが数字以外の文字の場合、その文字KeyEventを空の文字列に設定できます。そのため、何も表示されませんJFormattedTextField

于 2013-05-19T07:15:32.300 に答える