简体   繁体   English

在JFRAME中制作数字基础转换器

[英]Making a number base converter in JFRAME

So I have a JFrame file which contains 3 text boxes, a "Convert!" 因此,我有一个JFrame文件,其中包含3个文本框,一个“转换!” and a "Clear" button. 和“清除”按钮。 The 3 text boxes are to input numbers from Hexadecimal -- Decimal -- Binary. 3个文本框用于输入十六进制-十进制-二进制数。 You can input in any field pertaining to that specific number base and it'll convert that number base into the other 2 number bases. 您可以在与该特定数字基础有关的任何字段中输入,它将将该数字基础转换为其他2个数字基础。

My question is this : How can I detect which number base I'm supposed to convert from? 我的问题是这样的:如何检测应该转换的基数? For example if someone does a conversion, and wants to convert again, and he inputs another value for any number base so that 2 of the values are equivalent in the number bases, and one isn't, the program would mess up as it wouldn't know which one to convert. 例如,如果某人进行了转换并想再次进行转换,并且他输入了任何数字基数的另一个值,以使其中两个值在数字基数中相等,而一个数字基数不相等,则程序将像原来那样混乱不知道要转换哪一个。 How can I combat this problem. 我该如何解决这个问题。 And if someone is doing their first conversion, how would I be able to detect if there is text in the textbook. 如果有人进行了第一次转换,我将如何检测教科书中是否有文字。 Sorry I'm new to jframes and I barely know the syntax. 抱歉,我是jframe的新手,我几乎不了解语法。

You could add a DocumentFilter to each JTextField to perform the conversion: 您可以向每个JTextField添加一个DocumentFilter来执行转换:

DocumentFilter hexFilter = new DocumentFilter(){

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        super.replace(fb, offset, length, text, attrs);
        String hexInput = hexTextField.getText();
        //process hex input
    }
};
((AbstractDocument) hexTextField.getDocument()).setDocumentFilter(hexFilter);

The replace method will be invoked everytime the user inserts a text in the text field. 每当用户在文本字段中插入文本时,都会调用replace方法。

You just have to take the input from the text field and process it. 您只需要从文本字段中获取输入并进行处理即可。

I'm having this problem because there are 3 text fields and I don't know how I can "detect" in which textbox the user has made an input. 我有这个问题,因为有3个文本字段,而且我不知道如何“检测”用户在哪个文本框中进行了输入。

You can add a different DocumentFilter to each text field, so you will always know which text field the user is writing to. 您可以为每个文本字段添加一个不同的DocumentFilter ,这样您将始终知道用户正在写入哪个文本字段。

Something like: 就像是:

((AbstractDocument) hexTextField.getDocument()).setDocumentFilter(hexFilter);
((AbstractDocument) octTextField.getDocument()).setDocumentFilter(octFilter);
((AbstractDocument) binTextField.getDocument()).setDocumentFilter(binFilter);

This way you will always know that binFilter handles binary input, octFilter handles octal input and hexFilter handles hexadecimal input. 这样,您将始终知道binFilter处理二进制输入, octFilter处理八进制输入, hexFilter处理十六进制输入。


Another option would be to use a KeyListener , but AFAIK it doesn't react when you paste some text in the text field. 另一种选择是使用KeyListener ,但是AFAIK当您在文本字段中粘贴一些文本时不起作用。

You could place a Listener on each of the text fields and change the other text fields according to which one was changed by the user. 您可以在每个文本字段上放置一个侦听器,并根据用户更改的内容来更改其他文本字段。

I'm afraid I'm not entirely sure which listener you have to add to receive an event AFTER the text field gets changed (InputMethodListener didn't seem to fire anything and KeyListener fires before the text changes) so in this example I'll use ActionListener, which means the user will have to press "enter" after typing in the number. 恐怕我不确定是否在文本字段更改后必须添加哪个侦听器来接收事件(InputMethodListener似乎没有触发任何操作,而KeyListener在文本更改之前触发了),所以在此示例中,我将使用ActionListener,这意味着用户在输入数字后必须按“输入”。

final JTextField hex = new JTextField("hex");
final JTextField dec = new JTextField("dec");
ActionListener act = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == hex) {
            dec.setText(Integer.toString(Integer.parseInt(hex.getText(), 16)));
        } else if(e.getSource() == dec) {
            hex.setText(Integer.toHexString(Integer.parseInt(dec.getText())));
        }
    }
};
hex.addActionListener(act);
dec.addActionListener(act);

EDIT: This means you won't need the "Convert!" 编辑:这意味着您将不需要“转换!” button though, if you really want that then you'll need to keep track of which text field was edited the last time. 按钮,但是,如果您确实需要,则需要跟踪上一次编辑哪个文本字段。 Which you could do by doing: 您可以这样做:

JTextField lastEdited;
private void init() {
    //other init stuff
    final JTextField hex = new JTextField("hex");
    final JTextField dec = new JTextField("dec");
    KeyListener keyListener = new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            lastEdited = (JTextField) e.getSource();
        }
    };
    hex.addKeyListener(keyListener);
    dec.addKeyListener(keyListener);
    JButton convert = new JButton("Convert!");
    convert.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(lastEdited == hex) {
                dec.setText(Integer.toString(Integer.parseInt(hex.getText(), 16)));
            } else if(lastEdited == dec) {
                hex.setText(Integer.toHexString(Integer.parseInt(dec.getText())));
            }
        }
    });
    //other init stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM