Java JTextArea - Adding a newline

后端 未结 4 1322
孤街浪徒
孤街浪徒 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:25

    EDIT: After I see your full code, your keylistener is working. The problem is: outputArea.setText(txt); who overwrites your linebreaks. Try something like this:

    public void displayText(KeyEvent k){
        txt = inputArea.getText();
        char key = k.getKeyChar();
        if(key != KeyEvent.VK_ENTER)
            outputArea.append(Character.toString(k.getKeyChar()));
        else {
            // do something special, then add a linebreak
            outputArea.append("\n");
        }
    }
    

    ORIGINAL ANSWER: Try using getKeyChar() instead of getKeyCode(). See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/KeyEvent.html for when to use what. Also, since you are just testing on enter, you probarly won't need to use a KeyListener. You can use a ActionListener on the input-field instead. See sample code for both ActionListener and KeyListener solutions below.

    public class JTextAreaTest extends JFrame{
    private JTextArea txaConsole;
    private JTextField txtInput;
    private JButton btnSubmit;
    
    public JTextAreaTest(){
        SubmitListener submitListener = new SubmitListener();
        MyKeyListener myKeyListener = new MyKeyListener();
    
        txaConsole = new JTextArea(10,40);
        txtInput = new JTextField();
        txtInput.addActionListener(submitListener);
        //txtInput.addKeyListener(myKeyListener);
        btnSubmit = new JButton("Add");
        btnSubmit.addActionListener(submitListener);
    
        add(new JScrollPane(txaConsole), BorderLayout.NORTH);
        add(txtInput, BorderLayout.CENTER);
        add(btnSubmit, BorderLayout.EAST);
    
    
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        txtInput.requestFocusInWindow();
    }
    
    private void addInputToConsole(){
        String input = txtInput.getText().trim();
        if(input.equals("")) return;
    
        txaConsole.append(input + "\n");
        txtInput.setText("");
    }
    
    // You don't need a keylistener to listen on enter-presses. Use a
    // actionlistener instead, as shown below.
    private class MyKeyListener extends KeyAdapter {
        @Override
        public void keyTyped(KeyEvent e) {
            if(e.getKeyChar() == KeyEvent.VK_ENTER)
                addInputToConsole();
        }
    }
    
    private class SubmitListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            addInputToConsole();
        }
    }
    
    public static void main(String[] args) {
        new JTextAreaTest();
    }
    }
    
    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
  • 2021-01-28 08:47

    I know there is a difference with new line characters between OS's out there. You could try

    System.getProperty("line.separator")
    

    to get the new line character for the specific os it is running on. Don't know if that will fix your problem or not.

    0 讨论(0)
  • 2021-01-28 08:47

    I'm not sure I understand what you are trying to do, all I know is that you are using the wrong approach. You should NOT be using a KeyListener.

    From what I can see you want the contents of the output text area to be the same as the contents of the input area. If this is your requirement then you just use:

    JTextArea input = new JTextArea(...);
    JTextArea output = new JTextArea(...);
    output.setDocument( input.getDocument() );
    

    Or if you are trying to listen for all the changes to the input text area then you should be using a DocumentListener. The Swing tutorial has a section titled "How to Write a Document Listener" for more information.

    0 讨论(0)
提交回复
热议问题