javaでテキストエリアに値を設定する方法

Javaでは、JTextAreaにsetText()メソッドを呼び出すことで値を割り当てることができます。たとえば、次のコードがあります。

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JTextArea textArea = new JTextArea();
        
        String content = "Hello, World!";
        textArea.setText(content); // 设置文本框的内容
        
        frame.add(textArea);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

上記の例では、setText()メソッドは文字列”Hello, World!”をJTextAreaに代入するために使用されています。このコードを実行すると、ウィンドウには”Hello, World!”というテキストが入力されたテキストボックスが表示されます。

bannerAds