import javax.swing.*; public class JTextAreaExample extends JFrame { public JTextAreaExample() { JTextArea textArea = new JTextArea(); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); this.add(scrollPane); textArea.append("Hello, World!\n"); textArea.append("This is a JTextArea example.\n"); } public static void main(String[] args) { JTextAreaExample frame = new JTextAreaExample(); frame.setTitle("JTextArea Example"); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import javax.swing.*; public class JTextAreaExample2 { public static void main(String[] args) { JTextArea textArea = new JTextArea(5, 20); textArea.setEditable(true); JScrollPane scrollPane = new JScrollPane(textArea); JFrame frame = new JFrame("JTextArea Example 2"); frame.add(scrollPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); for(int i = 0; i < 10; i++) { textArea.append("Line " + i + "\n"); } } }In this example, we create a JTextArea with an initial size of 5 rows and 20 columns, enable editing, and add it to a JScrollPane. Then, we create a JFrame and add the JScrollPane to it. Finally, we use a loop to append 10 lines of text to the JTextArea. The package library for JTextArea is javax.swing.