java Gui中如何获取TextArea的值

如题所述

可以使用 JTextArea 的 getText() 方法获取文件内容,如下:

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class App extends JFrame {

private JTextArea textArea;

public App() {

this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JToolBar toolBar = new JToolBar();
this.add(toolBar, BorderLayout.NORTH);

JButton btnTest = new JButton("获取TextArea文本");
btnTest.addActionListener(e -> {

String text = textArea.getText();

JOptionPane.showMessageDialog(this, text);

});
toolBar.add(btnTest);

textArea = new JTextArea();
this.add(textArea, BorderLayout.CENTER);
}

public static void main(String[] args) {
new App().setVisible(true);
}
}

运行结果;

温馨提示:答案为网友推荐,仅供参考
相似回答