Сохранение текста в JTextArea - Java
Формулировка задачи:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class trainingGUI {
static JTextArea textArea = new JTextArea(10, 20);
static JFrame frame2 = new JFrame();
static JLabel label = new JLabel();
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLocationRelativeTo(null);
frame1.setSize(400, 400);
frame1.setLayout(new BorderLayout());
frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame2.setLocationRelativeTo(null);
frame2.setSize(400, 400);
frame2.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton buttonSaveText = new JButton("Сохранить текст");
JButton buttonShowText = new JButton("Показать текст");
buttonSaveText.addActionListener(new buttonSaveTextActionListener());
buttonShowText.addActionListener(new buttonShowTextActionListener());
panel.add(buttonSaveText);
panel.add(buttonShowText);
frame1.add(textArea, BorderLayout.CENTER);
frame1.add(panel, BorderLayout.SOUTH);
frame2.add(label);
frame1.setVisible(true);
}
public static class buttonSaveTextActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
label.setText(textArea.getText());
}
}
public static class buttonShowTextActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent actionEvent) {
frame2.setVisible(true);
}
}
}Решение задачи: «Сохранение текста в JTextArea»
textual
Листинг программы
String s = "<html>" + "Пере" + "<br>" + "нос" + "<br>"+ "стро<br>ки</html>"; label.setText(s);