Java JTextArea - Adding a newline

后端 未结 4 1324
孤街浪徒
孤街浪徒 2021-01-28 08:00

So I\'m stumped. I, for some reason, seem to remember that Java does weird things when you try to add new lines to stuff... so I think that may be the problem here.

Thi

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 08:31

    EDIT

    I have created a quick piece of code to add a KeyListener onto a JTextArea and this works fine. You don't need to particularly watch for an Enter key being pressed. How is this different from the rest of your code? (If you could paste more of it, that would be helpful.) I sort of feel like your KeyListener is not doing something properly. Are letters typing when you type? Try just taking out the if statement where you check for ENTER and see if that helps.

    public class TestGui {
    
        private JFrame frame;
        private JTextArea textArea;
    
        public TestGui() {
            frame = new JFrame();
            frame.setSize(200, 200);
    
            textArea = new JTextArea();
            textArea.setSize(200, 200);
            frame.add(textArea);
    
                // Notice that I don't do anything inside this listener.
                // It doesn't need to be here.
            textArea.addKeyListener(new KeyListener() {
    
                @Override
                public void keyPressed(KeyEvent e) {
                }
    
                @Override
                public void keyReleased(KeyEvent e) {
                }
    
                @Override
                public void keyTyped(KeyEvent e) {
                }
    
            });
    
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            TestGui gui = new TestGui();
        }
    }
    

提交回复
热议问题