请编写一个Java程序实现如下图所示的“我的记事本”窗口(TextEditor),窗口 1 中包括一个菜单栏(JMenuBar),其中有三个菜单(JMennu),分别是文件菜单、编辑 菜单和查看菜单,窗口的主体部分是一个可编辑区域,用到了多行多列的文本框 (JTextArea)和滚动条面板(JScrollPane):
时间: 2024-02-01 21:13:39 浏览: 24
为了方便起见,我将代码分成三部分,分别是菜单栏、可编辑区域和主窗口。
菜单栏部分代码如下:
```java
import javax.swing.*;
import java.awt.event.*;
public class TextEditorMenuBar extends JMenuBar {
private JMenu fileMenu, editMenu, viewMenu;
private JMenuItem newMenuItem, openMenuItem, saveMenuItem, exitMenuItem;
public TextEditorMenuBar() {
fileMenu = new JMenu("文件(F)");
editMenu = new JMenu("编辑(E)");
viewMenu = new JMenu("查看(V)");
newMenuItem = new JMenuItem("新建(N)");
openMenuItem = new JMenuItem("打开(O)");
saveMenuItem = new JMenuItem("保存(S)");
exitMenuItem = new JMenuItem("退出(Q)");
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
add(fileMenu);
add(editMenu);
add(viewMenu);
}
public JMenuItem getNewMenuItem() {
return newMenuItem;
}
public JMenuItem getOpenMenuItem() {
return openMenuItem;
}
public JMenuItem getSaveMenuItem() {
return saveMenuItem;
}
public JMenuItem getExitMenuItem() {
return exitMenuItem;
}
}
```
可编辑区域部分代码如下:
```java
import javax.swing.*;
public class TextEditorTextArea extends JScrollPane {
private JTextArea textArea;
public TextEditorTextArea() {
textArea = new JTextArea();
setViewportView(textArea);
}
public JTextArea getTextArea() {
return textArea;
}
}
```
主窗口部分代码如下:
```java
import javax.swing.*;
import java.awt.*;
public class TextEditor extends JFrame {
private TextEditorMenuBar menuBar;
private TextEditorTextArea textArea;
public TextEditor() {
setTitle("我的记事本");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new TextEditorMenuBar();
setJMenuBar(menuBar);
textArea = new TextEditorTextArea();
add(textArea, BorderLayout.CENTER);
setVisible(true);
}
public JMenuItem getNewMenuItem() {
return menuBar.getNewMenuItem();
}
public JMenuItem getOpenMenuItem() {
return menuBar.getOpenMenuItem();
}
public JMenuItem getSaveMenuItem() {
return menuBar.getSaveMenuItem();
}
public JMenuItem getExitMenuItem() {
return menuBar.getExitMenuItem();
}
public JTextArea getTextArea() {
return textArea.getTextArea();
}
}
```
最后,我们可以在主函数中创建一个 TextEditor 对象来展示窗口:
```java
public class Main {
public static void main(String[] args) {
TextEditor textEditor = new TextEditor();
}
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)