仿照Windows中的记事本,编写一个Java程序记事本。代码要求:有打开和保存文本文件,设置字体功能
时间: 2024-03-22 10:41:57 浏览: 117
好的,我可以为您提供一个简单的Java程序记事本,实现打开和保存文本文件的功能,以及设置字体的功能。
代码如下:
```java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class JavaNotepad extends JFrame implements ActionListener {
private JTextArea textArea;
private JFileChooser fileChooser;
private Font font;
public JavaNotepad() {
super("Java Notepad");
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
// 创建文件菜单
JMenu fileMenu = new JMenu("文件");
JMenuItem openMenuItem = new JMenuItem("打开");
openMenuItem.addActionListener(this);
JMenuItem saveMenuItem = new JMenuItem("保存");
saveMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
// 创建格式菜单
JMenu formatMenu = new JMenu("格式");
JMenuItem fontMenuItem = new JMenuItem("字体");
fontMenuItem.addActionListener(this);
formatMenu.add(fontMenuItem);
// 将菜单栏添加到窗口中
menuBar.add(fileMenu);
menuBar.add(formatMenu);
setJMenuBar(menuBar);
// 创建文本区域
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
// 创建文件选择器
fileChooser = new JFileChooser();
// 设置窗口大小和可见性
setSize(800, 600);
setVisible(true);
}
// 处理菜单项的动作
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("打开")) {
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (command.equals("保存")) {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (command.equals("字体")) {
font = JFontChooser.showDialog(this, "选择字体", font);
if (font != null) {
textArea.setFont(font);
}
}
}
public static void main(String[] args) {
new JavaNotepad();
}
}
// 字体选择器
class JFontChooser extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static JDialog dialog;
private static Font font;
private static JLabel previewLabel;
private static JComboBox<String> fontNameComboBox;
private static JComboBox<String> fontStyleComboBox;
private static JComboBox<Integer> fontSizeComboBox;
private static JButton okButton;
private static JButton cancelButton;
public JFontChooser() {
super(new BorderLayout());
// 字体名称选择框
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = env.getAvailableFontFamilyNames();
fontNameComboBox = new JComboBox<String>(fontNames);
fontNameComboBox.setSelectedItem(font.getName());
// 字体样式选择框
String[] fontStyles = {"常规", "粗体", "斜体", "粗斜体"};
fontStyleComboBox = new JComboBox<String>(fontStyles);
fontStyleComboBox.setSelectedIndex(font.getStyle());
// 字体大小选择框
Integer[] fontSizes = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72};
fontSizeComboBox = new JComboBox<Integer>(fontSizes);
fontSizeComboBox.setSelectedItem(font.getSize());
// 预览标签
previewLabel = new JLabel("这是一个字体预览标签", JLabel.CENTER);
previewLabel.setFont(font);
// 确定和取消按钮
okButton = new JButton("确定");
okButton.addActionListener(this);
cancelButton = new JButton("取消");
cancelButton.addActionListener(this);
// 创建面板
JPanel panel1 = new JPanel(new GridLayout(3, 2));
panel1.add(new JLabel("字体名称:"));
panel1.add(fontNameComboBox);
panel1.add(new JLabel("字体样式:"));
panel1.add(fontStyleComboBox);
panel1.add(new JLabel("字体大小:"));
panel1.add(fontSizeComboBox);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(previewLabel, BorderLayout.CENTER);
JPanel panel3 = new JPanel(new FlowLayout());
panel3.add(okButton);
panel3.add(cancelButton);
// 将面板添加到窗口中
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
}
// 显示字体选择器对话框
public static Font showDialog(JFrame parent, String title, Font font) {
JFontChooser.font = font;
dialog = new JDialog(parent, title, true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(new JFontChooser());
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
return JFontChooser.font;
}
// 处理按钮的动作
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
String fontName = (String)fontNameComboBox.getSelectedItem();
int fontStyle = fontStyleComboBox.getSelectedIndex();
int fontSize = (Integer)fontSizeComboBox.getSelectedItem();
font = new Font(fontName, fontStyle, fontSize);
previewLabel.setFont(font);
dialog.dispose();
} else if (e.getSource() == cancelButton) {
dialog.dispose();
}
}
}
```
这个Java记事本程序使用了Swing组件,包括JFrame、JMenuBar、JMenu、JMenuItem、JTextArea、JScrollPane、JFileChooser、JLabel、JComboBox、JButton等。它提供了打开、保存和设置字体的功能。您可以根据自己的需求进行修改和扩展。
阅读全文