java记事本代码 爱心
时间: 2023-11-11 09:00:13 浏览: 110
以下是一个简单的Java记事本代码,其中包含一个爱心图案:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Notepad extends JFrame implements ActionListener {
private JTextArea textArea = new JTextArea("", 0,0);
private JScrollPane scrollPane = new JScrollPane(textArea);
private JFileChooser fileChooser = new JFileChooser();
private String currentFile = "Untitled";
public Notepad() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setTitle(currentFile + " - Java Notepad");
setLocationRelativeTo(null);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
JMenuItem cutMenuItem = new JMenuItem("Cut");
cutMenuItem.addActionListener(this);
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.addActionListener(this);
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.addActionListener(this);
editMenu.add(pasteMenuItem);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
JMenuItem aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("New")) {
new Notepad();
} else if (action.equals("Open")) {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
openFile(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (action.equals("Save")) {
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
saveFile(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (action.equals("Exit")) {
dispose();
} else if (action.equals("Cut")) {
textArea.cut();
} else if (action.equals("Copy")) {
textArea.copy();
} else if (action.equals("Paste")) {
textArea.paste();
} else if (action.equals("About")) {
JOptionPane.showMessageDialog(this, "Java Notepad\n\nA simple notepad application written in Java.", "About Java Notepad", JOptionPane.INFORMATION_MESSAGE);
}
}
private void openFile(String fileName) {
try {
textArea.setText("");
java.io.FileReader in = new java.io.FileReader(fileName);
int c;
while ((c = in.read()) != -1) {
textArea.append(String.valueOf((char)c));
}
in.close();
currentFile = fileName;
setTitle(currentFile + " - Java Notepad");
} catch (java.io.IOException e) {
JOptionPane.showMessageDialog(this, "Error opening file:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void saveFile(String fileName) {
try {
java.io.FileWriter out = new java.io.FileWriter(fileName);
out.write(textArea.getText());
out.close();
currentFile = fileName;
setTitle(currentFile + " - Java Notepad");
} catch (java.io.IOException e) {
JOptionPane.showMessageDialog(this, "Error saving file:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new Notepad();
}
// 爱心图案
private static final String HEART =
" * * * * * * * * *\n" +
" * * * * * * * * * * *\n" +
" * * * * * * * * * * * * *\n" +
"* * * * * * * * * * * * * *\n" +
"* * * * * * * * * * * * * *\n" +
"* * * * * * * * @* * * * *\n" +
"* * * * * *@* @* * *\n" +
"* * *@* @@* **\n" +
" ** @@* @@@*\n" +
" ** @@@@@*\n" +
" **@*\n";
}
```
阅读全文