Вывод String в JTextArea с новой строчки
Нужно выводить "TX: " + sended_data после нажатия на кнопку с новой строчки, при этом сохранять предыдущие.
↓Пример↓
Ввод:
1[Send], 1[Send], 1[Send], 1[Send]
↓Вывод↓
ТХ:1
TX:1
TX:1
TX:1
Сам код:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
JFrame main_frame = new JFrame();
JPanel main_panel = new JPanel();
main_frame.setSize(600,450);
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main_frame.setTitle("MessageBox");
main_frame.add(main_panel);
main_panel.setLayout(null);
JButton send_button = new JButton("Send");
send_button.setBounds(480,360,80,25);
main_panel.add(send_button);
JTextField send_text_field = new JTextField(30);
send_text_field.setBounds(20,360,450,25);
main_panel.add(send_text_field);
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
main_panel.add(textArea);
JScrollPane textAreaPane = new JScrollPane(textArea);
textAreaPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textAreaPane.setBounds(20,20,540,325);
main_panel.add(textAreaPane);
send_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String sended_data=send_text_field.getText();
StringJoiner joiner = new StringJoiner("\n");
joiner.add("TX: " + sended_data);
textArea.setText(joiner.toString());
}
});
main_frame.setVisible(true);
main_frame.setLocationRelativeTo(null);
}
}