When concateno String add a ENTER

Asked

Viewed 124 times

3

When I concateno(+) strings adds one alone enter right 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;
}

  • The function that writes the line is which? Because in java we have Write and Writeln, the second adds a enter. Isn’t that something? If you can, post the functions used, such as showMens, for example.

  • But of course, it’s not alone. It adds enter to Panel for after/during firing the keyPressed event

  • And what should I do to take that away ??

1 answer

3


I cannot 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) {    
        }
    }
}

Seems to work smoothly:

Teste de GUI

By the way, I suggest you use the ActionListener instead of KeyListener, as recommended here.

EDIT:

With the inclusion of your entire code, I realized you’re using a JTextArea for inserting text. I suggest you switch to a JTextField, that I believe will solve the problem immediately.

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

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

I think it solves:

Chat Funcionando

  • @Lucas Edited the answer - see if it works!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.