如何在Java中的MaskFormatter的JFormattedTextField中的特定索引处插入字符? [英] how to insert characters at particular index in JFormattedTextField of MaskFormatter in Java?

查看:92
本文介绍了如何在Java中的MaskFormatter的JFormattedTextField中的特定索引处插入字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,叫做"AAAABB".我有一个JFormattedField of MaskFormatter.所以我的框架中有? ? ? ? ? ?.我有两个按钮A和B.当用户按下A按钮时,对于所有出现的A,MaskFormatter的JFormattedField应该用字母A替换?.在本示例中,索引为0、1、2、3.

I have a string which is say "AAAABB". I have a JFormattedField of MaskFormatter. so I have ? ? ? ? ? ? in my Frame. I have two buttons A and B. when the user presses A button, the JFormattedField of the MaskFormatter should replace ? with Letter A for all occurrences of A. i.e, index 0,1,2,3 in this example.

我的代码获取要用A替换的索引列表.但是我很难实现setLetter(String Letter, int Position)方法,该方法需要在JformattedField中替换字母和索引.例如,如果我通过setLetter("A",2),则在上面的示例中应该得到? ? A ? ? ?.请尝试使用此代码查看框架.

I have my code that gets the list of indices to be replaced with A. But I am having difficulty in implementing setLetter(String Letter, int Position) method, that takes letter and indices to be replaced in the JformattedField. example if I pass setLetter("A",2), I should get ? ? A ? ? ? in the above example. Please try this code to see the frame.

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);
    }

}

推荐答案

我不认为掩码格式化程序是您想要的,因为您实际上并未在此处格式化字符串.如果您只想取一个字符串并在某些位置设置字符,则应该自己编写该字符串.

I don't think a mask formatter is what you want for this because you aren't really formatting a string here. If you just want to take a string and set characters at certain positions you should formulate the string yourself.

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);

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

Also a style note: all caps is for identifiers of static variables.

这篇关于如何在Java中的MaskFormatter的JFormattedTextField中的特定索引处插入字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆