如何在Java中MaskFormatter的JFormattedTextField的特定索引处插入字符?

如何在Java中MaskFormatter的JFormattedTextField的特定索引处插入字符?,java,swing,formatter,jformattedtextfield,Java,Swing,Formatter,Jformattedtextfield,我有一个字符串,上面写着“AAAABB”。我有一个MaskFormatter的jformatted字段。所以我有在我的框架中。我有两个按钮A和B。当用户按下一个按钮时,MaskFormatter的jFormatted字段应该用字母A替换?,用于所有出现的A。即,在本例中,索引0,1,2,3 我有一个代码,它将索引列表替换为A。但是我在实现setLetter(String Letter,int Position)方法时遇到了困难,该方法在jFormatted字段中替换字母和索引。例如,如果我传递s

我有一个字符串,上面写着“AAAABB”。我有一个MaskFormatter的
jformatted字段。所以我有
在我的框架中。我有两个按钮A和B。当用户按下一个按钮时,
MaskFormatter
的jFormatted字段应该用字母A替换
,用于所有出现的A。即,在本例中,索引0,1,2,3

我有一个代码,它将索引列表替换为A。但是我在实现
setLetter(String Letter,int Position)
方法时遇到了困难,该方法在jFormatted字段中替换字母和索引。例如,如果我传递setLetter(“A”,2),我应该得到
?A。请尝试此代码以查看框架

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

public class TestMain extends JPanel{

    JFormattedTextField input;

    private MaskFormatter formatter;

    public final String WORD = "ABBAABBA";

    public TestMain() {
        try {
            JLabel label = new JLabel("Guesss");
            String s="";
            for (int i =0;i<WORD.length();i++){
                s+="? ";
            }
            formatter = new MaskFormatter(s);
            formatter.setPlaceholderCharacter('?');
            input = new JFormattedTextField(formatter);
            input.setColumns(20);
            add(label);
            add(input);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
        JButton buttonA = new JButton("A");
        JButton buttonB = new JButton("B");

        buttonA.addActionListener(clickedbutton());
        buttonB.addActionListener(clickedbutton());

        add(buttonA);
        add(buttonB);
    }

    private ActionListener clickedbutton() {
             return new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                    JButton pressedButton = (JButton) e.getSource();
                    String letter = e.getActionCommand();
                    try {
                        //System.out.println("actionCommand is: ---" + letter);

                        //Get the list of indices
                        int index = WORD.indexOf(letter);
                        ArrayList<Integer> indices = new ArrayList<Integer>();
                        if (index>=0){
                            for(int j=0;j<WORD.length();j++){
                                if (WORD.charAt(j)==letter.charAt(0)){
                                    indices.add(j);
                                }
                            }
                            //System.out.println(indices);
                        }
                        for (int k =0 ; k < indices.size(); k++){
                            setLetter(letter, k);
                        }

                    } catch (ParseException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                 }
                 };
         }

    public void setLetter(String letter, int position) throws ParseException {
        String word="Hello";
        input.setValue(letter);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        TestMain tm = new TestMain();
        frame.add(tm);
        frame.pack();
        frame.setTitle("formatter");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.text.ParseException;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JFormattedTextField;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.text.MaskFormatter;
公共类TestMain扩展了JPanel{
JFormattedTextField输入;
专用MaskFormatter格式化程序;
公共最终字符串WORD=“ABBAABBA”;
公共TestMain(){
试一试{
JLabel标签=新JLabel(“猜测”);
字符串s=“”;
对于(int i=0;i=0){

对于(int j=0;j我不认为掩码格式化程序是您想要的,因为您在这里并没有真正格式化字符串。如果您只想获取字符串并在某些位置设置字符,您应该自己对字符串进行格式化

String text = "";

for (int i = 0; i < WORD.length(); i++) {
    if (String.valueOf(word.charAt(i)).equals(letter)) {
        text += letter + " ";
    } else {
        text += "? ";
    }
}

input.setText(text);
String text=”“;
for(int i=0;i

还有一个样式说明:所有大写字母都是用于静态变量的标识符。

a
JTextComponent
是按顺序排列的一系列字符。从概念上讲,不可能将字符按顺序插入文档中的任意位置。这就是为什么我建议在oh god!t中使用
DocumentFilter
这将涉及重写一整串我猜如果你没有附加到
JFormattedTextField
,你可以通过生成
字符串来“作弊”(就像你做了掩码一样)。每次用户单击字母按钮时,您只需重新生成字段文本,将所需位置的
替换为正确的字符…在这种情况下,您需要使字段不可编辑。您建议的
documentFilter
非常好。但我需要它来侦听操作事件和ca我将给出“作弊”的建议现在在这种情况下,我想我甚至不需要MaskFormatter。我只需要一个字符串
,然后每次都根据需要重新生成更改。谢谢。这就是我现在正在实现的。我认为caps也用于Final常量,对吗?我不知道Final和not Final之间有任何区别。Oracle有官方代码co不构成一个的约束:。只有在静态final时才使用所有的caps。