import javax.swing.*; public class Example1 { public static void main(String[] args) { JFrame frame = new JFrame("JTextArea Example"); JTextArea textArea = new JTextArea(); textArea.setBounds(10, 10, 200, 200); textArea.requestFocus(); // set focus to the JTextArea component frame.add(textArea); frame.setSize(300, 300); frame.setLayout(null); frame.setVisible(true); } }
import javax.swing.*; public class Example2 { public static void main(String[] args) { JFrame frame = new JFrame("JTextArea Example"); JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setBounds(10, 10, 200, 200); textArea.requestFocus(); // set focus to the JTextArea component frame.add(scrollPane); frame.setSize(300, 300); frame.setLayout(null); frame.setVisible(true); } }This example is similar to the first example, but the JTextArea is contained within a JScrollPane component. The requestFocus() method is called on the JTextArea component to set the input focus. The JScrollPane is then added to the JFrame and displayed on the screen. In both examples, the javax.swing package is used to create the components and set the input focus.