示例在eclipse中JFrame添加jtextarea
时间: 2024-06-09 08:05:13 浏览: 6
好的,以下是在 Eclipse 中创建 JFrame 并添加 JTextArea 的简单示例代码:
```java
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MyFrame extends JFrame {
private JTextArea textArea;
public MyFrame() {
setTitle("My Frame");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
```
在上面的代码中,我们创建了继承自 JFrame 的 MyFrame 类,并添加了一个 JTextArea 控件。我们使用 JScrollPane 将 JTextArea 包装起来,以便我们可以在文本内容超出 JTextArea 大小时滚动查看。最后我们将 JScrollPane 添加到 JFrame 中,并设置 JFrame 可见。通过运行 main 方法,我们可以看到一个简单的窗口,其中包含一个 JTextArea 控件:
![JFrame with JTextArea in Eclipse](https://i.imgur.com/RRzVbX7.png)
希望这个简单的示例对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)