共計 708 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 Swing 或 JavaFX 庫來創建 GUI 界面,然后使用相應的組件來實現動態添加 TextArea 的值。以下是使用 Swing 庫的示例代碼:
import javax.swing.*;
public class DynamicTextAreaExample {public static void main(String[] args) {JFrame frame = new JFrame("Dynamic TextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
JButton button = new JButton("Add Value");
button.addActionListener(e -> {textArea.append("New Value\n");
});
JPanel panel = new JPanel();
panel.add(scrollPane);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
在上述示例中,創建了一個 JFrame 窗口,并在其中添加了一個 JTextArea 和一個 JButton。點擊按鈕時,會動態地在 JTextArea 中添加一個新的值。
請注意,這只是一個簡單的示例,實際使用中可能需要更復雜的邏輯和其他組件來實現更多功能。
丸趣 TV 網 – 提供最優質的資源集合!
正文完