Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFormattedTextField问题_Java_Swing_Formatter_Jformattedtextfield - Fatal编程技术网

Java JFormattedTextField问题

Java JFormattedTextField问题,java,swing,formatter,jformattedtextfield,Java,Swing,Formatter,Jformattedtextfield,1) 我如何将游标设置为0 position而不使用插入符号或封装在invokeLater()中的焦点(confortly可以通过使用@camickr解决),有人知道另一种方法吗 2) 如何重置格式化程序有时(通过从键盘上通过制表符提高焦点),重置不起作用,而在focusLost(空字段)上,格式化程序返回/转世字符或字符串(setText(“”;)之前的最后一个已知项) 注意:了解代码或以下代码仅此方式,关于如何从OTN重置格式化程序,但他们糟糕的搜索规则…,仅代码(Jeanette的问题或

1) 我如何将游标设置为0 position而不使用插入符号或封装在invokeLater()中的焦点(confortly可以通过使用@camickr解决),有人知道另一种方法吗

2)
如何重置格式化程序
有时(通过从键盘上通过制表符提高焦点),重置不起作用,而在focusLost(空字段)上,格式化程序返回/转世字符或字符串(setText(“”;)之前的最后一个已知项)

注意:了解代码或以下代码
仅此方式
,关于如何从OTN重置格式化程序,但他们糟糕的搜索规则…,仅代码(Jeanette的问题或答案??)

所附图片基于我的sscce

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.MaskFormatter;

public class TestTest {

    private JFormattedTextField myFormattedField1 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
    private JFormattedTextField myFormattedField2 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
    private JFormattedTextField myFormattedField3 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
    private JFormattedTextField myFormattedField4 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
    private JButton jb = new JButton("Reset to Default");
    private JFrame frame = new JFrame("Text Test");

    public TestTest() {
        myFormattedField1.setText("ABCDEFGHIJKL");
        myFormattedField2.setText("ABCDEFGHIJKL");
        myFormattedField3.setText("ABCDEFGHIJKL");
        myFormattedField4.setText("ABCDEFGHIJKL");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.setLayout(new GridLayout(5, 0));
        frame.add(jb);
        frame.add(myFormattedField1);
        frame.add(myFormattedField2);
        frame.add(myFormattedField3);
        frame.add(myFormattedField4);
        jb.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                myFormattedField1.setText("");
                myFormattedField2.setText("");
                myFormattedField3.setText("");
                myFormattedField4.setText("");
            }
        });
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        TestTest textTest = new TestTest();
    }

    protected MaskFormatter createFormatter(String s) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(s);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
        }
        return formatter;
    }
}
  • 在Mac OS上,UI委托的默认行为,
    com.apple.laf.AquaTextFieldF
    ,类似于:

    • Tab或Shift Tab:将插入符号放置在字段的开头

    • 单击:将插入符号短暂放置在字段的开头,然后将其移动到单击点

    在国际海事组织,后者做得更顺利

  • 焦点丢失时的默认行为是
    COMMIT\u或\u REVERT
    。在下面的示例中,Tab通过第一个无效字段查看效果。格式化程序既不能提交也不能还原为无效值,因此它会替换
    MASK.length()
    空格。如果有点模糊,则此值有效

  • 附录:我已经更新了下面的代码以允许更改

    代码:


    这个答案相当经验性;我欢迎任何更正或更好的答案。作为参考,我已经更新了代码以允许选择“焦点丢失”设置。谢谢,我真的不明白Excellent ans如何正确地运行您的代码,谢谢您的另一节课抱歉,这不是更具建设性的内容。API说
    COMMIT\u或\u REVERT
    是默认值;我本想用
    getEnum()
    作为检查,但这是多余的。1)为什么?2) 空字符串是无效项,因此该字段将恢复为掩码本身(设置占位符字符以查看,空格不可见:-)
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import javax.swing.text.MaskFormatter;
    
    public class TestTest {
    
        private JFormattedTextField myFormattedField1 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
        private JFormattedTextField myFormattedField2 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
        private JFormattedTextField myFormattedField3 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
        private JFormattedTextField myFormattedField4 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA"));
        private JButton jb = new JButton("Reset to Default");
        private JFrame frame = new JFrame("Text Test");
    
        public TestTest() {
            myFormattedField1.setText("ABCDEFGHIJKL");
            myFormattedField2.setText("ABCDEFGHIJKL");
            myFormattedField3.setText("ABCDEFGHIJKL");
            myFormattedField4.setText("ABCDEFGHIJKL");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(150, 150);
            frame.setLayout(new GridLayout(5, 0));
            frame.add(jb);
            frame.add(myFormattedField1);
            frame.add(myFormattedField2);
            frame.add(myFormattedField3);
            frame.add(myFormattedField4);
            jb.addActionListener(new java.awt.event.ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    myFormattedField1.setText("");
                    myFormattedField2.setText("");
                    myFormattedField3.setText("");
                    myFormattedField4.setText("");
                }
            });
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            TestTest textTest = new TestTest();
        }
    
        protected MaskFormatter createFormatter(String s) {
            MaskFormatter formatter = null;
            try {
                formatter = new MaskFormatter(s);
            } catch (java.text.ParseException exc) {
                System.err.println("formatter is bad: " + exc.getMessage());
            }
            return formatter;
        }
    }
    
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.text.MaskFormatter;
    
    /** @see http://stackoverflow.com/questions/7378821 */
    public class TrashTest {
    
        private static final String MASK = "########";
        private static final String DEFAULT = "01234567";
        private static final String BOGUS = "0123456";
        private JFormattedTextField jtf1 = createField();
        private JFormattedTextField jtf2 = createField();
        private JFormattedTextField jtf3 = createField();
        private JFormattedTextField jtf4 = createField();
        private JButton reset = new JButton("Reset to Default");
        private JComboBox combo = new JComboBox();
        private JFrame frame = new JFrame("Text Test");
    
        public TrashTest() {
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(150, 150);
            frame.setLayout(new GridLayout(0, 1));
            frame.add(reset);
            frame.add(jtf1);
            frame.add(jtf2);
            frame.add(jtf3);
            frame.add(jtf4);
            frame.add(combo);
            this.initFields();
            reset.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    initFields();
                }
            });
            for (Edit e : Edit.values()) {
                combo.addItem(e);
            }
            combo.setSelectedIndex(jtf1.getFocusLostBehavior());
            combo.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Edit current = (Edit) combo.getSelectedItem();
                    jtf1.setFocusLostBehavior(current.value);
                }
            });
            frame.pack();
            frame.setVisible(true);
        }
    
        private void initFields() {
            jtf1.setText(BOGUS);
            jtf2.setText(DEFAULT);
            jtf3.setText(DEFAULT);
            jtf4.setText(DEFAULT);
        }
    
        protected JFormattedTextField createField() {
            MaskFormatter formatter = null;
            try {
                formatter = new MaskFormatter(MASK);
            } catch (java.text.ParseException e) {
                e.printStackTrace(System.out);
            }
            JFormattedTextField jtf = new JFormattedTextField(formatter);
            return jtf;
        }
    
        enum Edit {
    
            COMMIT(JFormattedTextField.COMMIT),
            COMMIT_OR_REVERT(JFormattedTextField.COMMIT_OR_REVERT),
            REVERT(JFormattedTextField.REVERT),
            PERSIST(JFormattedTextField.PERSIST);
            private int value;
    
            private Edit(int n) {
                this.value = n;
            }
    
            public static Edit getEnum(int n) {
                for (Edit e : Edit.values()) {
                    if (e.value == n) {
                        return e;
                    }
                }
                return Edit.COMMIT_OR_REVERT;
            }
        }
    
        public static void main(String[] args) {
            TrashTest textTest = new TrashTest();
        }
    }