Skip to main content
tags editadas
Link
rrr
  • 635
  • 7
  • 21
removeu 2124 caracteres do conteúdo
Fonte Link
rrr
  • 635
  • 7
  • 21

Aplicação

https://i.sstatic.net/WA2br.png

Código

@Override
public void keyPressed(KeyEvent e) {
    String messageSent = "User: " + writingTextField.getText();
    if(e.getKeyCode() == KeyEvent.VK_ENTER){

        writingTextField.setText("");

        readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
    }
}

public class MessagePanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    JButton sendButton;
    JTextField writingTextField;
    JTextArea readingTextArea;

    public MessagePanel() {

        setLayout(new BorderLayout());
        createWritingPanel();
        createReadingPanel();

    }

    private void createReadingPanel() {
        readingTextArea = new JTextArea();
        readingTextArea.setEditable(false);

        add(readingTextArea, BorderLayout.CENTER);
    }

    private void createWritingPanel() {
        JPanel writingPanel = new JPanel();
        writingPanel.setLayout(new BorderLayout());

        writingTextField = new JTextField();

        sendButton = new JButton("Send");
        sendButton.addActionListener(new ButtonActionListener());

        writingPanel.add(writingTextField, BorderLayout.CENTER);
        writingPanel.add(sendButton, BorderLayout.EAST);

        add(writingPanel, BorderLayout.SOUTH);

    }


    class ButtonActionListener implements ActionListener, KeyListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String messageSent = "User: " + writingTextField.getText();
            writingTextField.setText("");

            readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void keyPressed(KeyEvent e) {
            String messageSent = "User: " + writingTextField.getText();
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                
                writingTextField.setText("");

                readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }
}

Aplicação

https://i.sstatic.net/WA2br.png

Código

@Override
public void keyPressed(KeyEvent e) {
    String messageSent = "User: " + writingTextField.getText();
    if(e.getKeyCode() == KeyEvent.VK_ENTER){

        writingTextField.setText("");

        readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
    }
}

public class MessagePanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    JButton sendButton;
    JTextField writingTextField;
    JTextArea readingTextArea;

    public MessagePanel() {

        setLayout(new BorderLayout());
        createWritingPanel();
        createReadingPanel();

    }

    private void createReadingPanel() {
        readingTextArea = new JTextArea();
        readingTextArea.setEditable(false);

        add(readingTextArea, BorderLayout.CENTER);
    }

    private void createWritingPanel() {
        JPanel writingPanel = new JPanel();
        writingPanel.setLayout(new BorderLayout());

        writingTextField = new JTextField();

        sendButton = new JButton("Send");
        sendButton.addActionListener(new ButtonActionListener());

        writingPanel.add(writingTextField, BorderLayout.CENTER);
        writingPanel.add(sendButton, BorderLayout.EAST);

        add(writingPanel, BorderLayout.SOUTH);

    }


    class ButtonActionListener implements ActionListener, KeyListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String messageSent = "User: " + writingTextField.getText();
            writingTextField.setText("");

            readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void keyPressed(KeyEvent e) {
            String messageSent = "User: " + writingTextField.getText();
            if(e.getKeyCode() == KeyEvent.VK_ENTER){
                
                writingTextField.setText("");

                readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }
}

Código

@Override
public void keyPressed(KeyEvent e) {
    String messageSent = "User: " + writingTextField.getText();
    if(e.getKeyCode() == KeyEvent.VK_ENTER){

        writingTextField.setText("");

        readingTextArea.setText(readingTextArea.getText() + messageSent + "\n");
    }
}
tags editadas
Link
Laerte
  • 22,8mil
  • 19
  • 86
  • 130
adicionou 362 caracteres ao conteúdo; título editado
Fonte Link
Laerte
  • 22,8mil
  • 19
  • 86
  • 130
Carregando
adicionou 362 caracteres ao conteúdo; título editado
Fonte Link
Laerte
  • 22,8mil
  • 19
  • 86
  • 130
Carregando
adicionou 1745 caracteres ao conteúdo
Fonte Link
rrr
  • 635
  • 7
  • 21
Carregando
adicionou 238 caracteres ao conteúdo
Fonte Link
rrr
  • 635
  • 7
  • 21
Carregando
Fonte Link
rrr
  • 635
  • 7
  • 21
Carregando