Can't figure out error with ActionListener

前端 未结 2 1855
忘了有多久
忘了有多久 2020-12-12 00:20

The following is my program. the goal is to convert from a roman numeral to an Arabic number after a user types in the numeral and presses the enter key.

This is a

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 01:13

    For a better answer, see @MadProgrammer 's answer.

    My solution:

    There is no ActionListener for JTextArea.

    So just use KeyListener instead

    HandlerForTextArea handler = new HandlerForTextArea();
    enterRomanNumber.addKeyListener(handler);
    

    Implements KeyListener

    private class HandlerForTextArea implements KeyListener
    {
    
        @Override
        public void keyPressed(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
            if (arg0.getKeyCode() == VK_ENTER){
                // TODO Your bussiness
            }
        }
    
        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
        }
    }
    

提交回复
热议问题