When concatenating String add an ENTER

3

When I concatenate (+) strings it adds only a enter shortly after the concatenation.

private static Dimension TOOLKIT = Toolkit.getDefaultToolkit().getScreenSize();

private static GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment();  
private static Rectangle SCREEN = GE.getMaximumWindowBounds(); 

private static Color BLACK = new Color(0, 0, 0);
private static Color CINZA = new Color(32, 32, 32);
private static Color GREEN = new Color(12, 202, 5);

static JTextArea showMens = new JTextArea();
static JTextArea writeMens = new JTextArea();

public static void main(String[] args) 
{
    createChatLayout();
}

public static void createChatLayout()
{
    JFrame frameChat = new JFrame();

    int WIDTH = SCREEN.width;
    int HEIGHT = SCREEN.height;
    int BORDA = 10;

    frameChat.setExtendedState(frameChat.MAXIMIZED_BOTH);

    frameChat.setDefaultCloseOperation(frameChat.EXIT_ON_CLOSE);
    frameChat.setLayout(null);
    frameChat.setVisible(true);

    Container c = frameChat.getContentPane();
    c.setBackground(BLACK);

    showMens.setBounds(BORDA, BORDA, WIDTH - BORDA * 2, HEIGHT - BORDA * 2 - HEIGHT / 4 - 2 * BORDA);
    writeMens.setBounds(BORDA, (HEIGHT - BORDA * 2 - HEIGHT / 4 - BORDA) + BORDA , WIDTH - BORDA * 2, HEIGHT / 4 - BORDA);

    showMens.setBackground(CINZA);
    writeMens.setBackground(CINZA);
    showMens.setForeground(GREEN);
    writeMens.setForeground(GREEN);
    showMens.setFont(createFont(16));   
    writeMens.setFont(createFont(16));
    showMens.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));  
    writeMens.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));  
    showMens.setLineWrap(true);
    writeMens.setLineWrap(true);
    showMens.setWrapStyleWord(true);
    writeMens.setWrapStyleWord(true);
    showMens.setEditable(false);

    c.add(showMens);
    c.add(writeMens);

    frameChat.repaint();

    eventos();
}

public static void eventos()
{
    writeMens.addKeyListener(new KeyListener()
    {
        public void keyTyped(KeyEvent e) {}

        public void keyReleased(KeyEvent e) {}

        public void keyPressed(KeyEvent e) 
        {
            if(e.getKeyCode() == e.VK_ENTER)
            {
                showMens.append("Lucas: " + writeMens.getText());
                showMens.append("\n");
                writeMens.setText("");
            }
        }
    });
}

public static Font createFont(int tamanho)
{
    Font font = null;
    try 
    {
        font = Font.createFont(Font.TRUETYPE_FONT, new File("ocraextended.ttf")).deriveFont(Font.PLAIN, tamanho);
    } 
    catch (FontFormatException e){} catch (IOException e){}

    return font;
}

    
asked by anonymous 06.08.2015 / 20:19

1 answer

3

I can not reproduce the error using the following code:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MeuGUI {

    JPanel janelaSaida;
    JPanel janelaEntrada;
    JTextField entradaTexto;
    JTextArea saidaTexto;

    public MeuGUI() {

        JFrame frame = new JFrame();    
        janelaSaida = new JPanel();
        janelaEntrada = new JPanel();
        entradaTexto = new JTextField(40);
        saidaTexto = new JTextArea(10, 40);
        janelaEntrada.add(entradaTexto);
        janelaSaida.add(saidaTexto);    
        frame.getContentPane().add(BorderLayout.NORTH, janelaSaida);
        frame.getContentPane().add(BorderLayout.SOUTH, janelaEntrada);    
        entradaTexto.addKeyListener(new MyListener());    
        frame.pack();
        frame.setVisible(true);    
    }

    public static void main(String[] args) {    
        new MeuGUI();    
    }

    public class MyListener implements KeyListener {    
        @Override
        public void keyPressed(KeyEvent arg0) {    
            if (arg0.getKeyCode() == arg0.VK_ENTER) {    
                saidaTexto.append("Daniel: " + entradaTexto.getText() + "\n");
                entradaTexto.setText("");    
            }    
        }

        @Override
        public void keyReleased(KeyEvent arg0) {    
        }

        @Override
        public void keyTyped(KeyEvent arg0) {    
        }
    }
}

It seems to work without problems:

Incidentally,IsuggestyouuseActionListenerinsteadofKeyListener,asrecommended here . p>

EDIT:

With the inclusion of your entire code, I noticed that you're using JTextArea to insert text. I suggest you switch to JTextField , which I believe will fix the problem right away.

If you still want to use JTextArea , move writeMens.setText(""); to keyReleased :

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == e.VK_ENTER) {
            writeMens.setText("");
        }
    }

I think it solves:

    
06.08.2015 / 22:36