完成简易记事本。能打开txt文件,保存文件。
package GUI;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URI;
public class NotepadDemo {
public JPanel jp;
public JFrame jf;
public JMenuBar jMenuBar;
public JMenu menu_File, menu_Edit, menu_View, menu_About;
public JMenuItem menuItem_Open, menuItem_Exit1, menuItem_Exit2,
menuItem_Exit3, menuItem_Exit4, menuItem_Save, menuItem_SaveAs,
menuItem_Cut, menuItem_Copy, menuItem_MyBlog;
public JTextArea jTextArea;
public JScrollPane jScrollPane;
public File oneFile;
public FileReader fr;
public FileWriter fw;
public BufferedReader br;
public String line;
public NotepadDemo(){
jf = new JFrame("菜单");
jf.setSize(800, 600);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
//jf.setResizable(false);
jp = new JPanel(new GridLayout(1, 1));
jMenuBar = new JMenuBar();
menu_File = new JMenu("文件");
menu_Edit = new JMenu("编辑");
menu_View = new JMenu("视图");
menu_About = new JMenu("关于");
jMenuBar.add(menu_File);
jMenuBar.add(menu_Edit);
jMenuBar.add(menu_View);
jMenuBar.add(menu_About);
//-=-=-=-=-=-=-文件-=-=-=-=-=-=-
menuItem_Open = new JMenuItem("打开");
menuItem_Save = new JMenuItem("保存");
menuItem_SaveAs = new JMenuItem("另存为");
menuItem_Exit1 = new JMenuItem("退出");
menu_File.add(menuItem_Open);
menu_File.add(menuItem_Save);
menu_File.add(menuItem_SaveAs);
menu_File.addSeparator();
menu_File.add(menuItem_Exit1);
//-=-=-=-=-=-=-编辑-=-=-=-=-=-=-
menuItem_Copy = new JMenuItem("复制");
menuItem_Cut = new JMenuItem("裁剪");
menuItem_Exit2 = new JMenuItem("退出");
menu_Edit.add(menuItem_Copy);
menu_Edit.add(menuItem_Cut);
menu_Edit.addSeparator();
menu_Edit.add(menuItem_Exit2);
//-=-=-=-=-=-=-视图-=-=-=-=-=-=-
menuItem_Exit3 = new JMenuItem("退出");
menu_View.add(menuItem_Exit3);
//-=-=-=-=-=-=-关于-=-=-=-=-=-=-
menuItem_MyBlog = new JMenuItem("银月のBlog");
menuItem_Exit4 = new JMenuItem("退出");
menu_About.add(menuItem_MyBlog);
menu_About.addSeparator();
menu_About.add(menuItem_Exit4);
ActL actL = new ActL();
menuItem_Open.addActionListener(actL);
menuItem_Save.addActionListener(actL);
menuItem_SaveAs.addActionListener(actL);
menuItem_Exit1.addActionListener(actL);
menuItem_Exit2.addActionListener(actL);
menuItem_Exit3.addActionListener(actL);
menuItem_Exit4.addActionListener(actL);
menuItem_MyBlog.addActionListener(actL);
jTextArea = new JTextArea();
jTextArea.setLineWrap(true);
jScrollPane = new JScrollPane(jTextArea);
jp.add(jScrollPane);
jf.setJMenuBar(jMenuBar);
jf.setContentPane(jp);
jf.setVisible(true);
}
class ActL implements ActionListener {
@Override
public void actionPerformed(ActionEvent actionEvent) {
String m = actionEvent.getActionCommand();
if(m.equals("退出")) {
System.exit(0);
} else if(m.equals("打开")) {
//↓判断用户单击的是否为“打开”按钮
if (fileChooser.showOpenDialog(jf.getContentPane()) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
oneFile = new File(selectedFile.getAbsolutePath());
try {
fr = new FileReader(oneFile);
br = new BufferedReader(fr);
line = "";
while((line = br.readLine()) != null) {
jTextArea.append(line + "\r\n");
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
} else if(m.equals("保存")) {
try {
if (oneFile != null) {
fw = new FileWriter(oneFile, false);
fw.write(jTextArea.getText());
fw.flush();
} else {
System.out.println("未打开文件");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fw != null){
fw.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
} else if(m.equals("另存为")) {
//↓判断用户单击的是否为“打开”按钮
if (fileChooser.showSaveDialog(jf.getContentPane()) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
oneFile = new File(selectedFile.getAbsolutePath());
try {
if (oneFile.createNewFile()) {
fw = new FileWriter(oneFile, false);
fw.write(jTextArea.getText());
fw.flush();
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else if(m.equals("银月のBlog")) {
URI uri=java.net.URI.create("https://i.loli.link");
Desktop dp=java.awt.Desktop.getDesktop();
//↓获取系统默认浏览器打开链接
if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
try {
dp.browse(uri);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
System.out.println(m);
}
}
JFileChooser fileChooser;
{
fileChooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter
("文本文件(txt/html/htm/xml/php/js/java/c/cpp/py/vb/vba/vbs)",
"txt", "html", "htm", "xml", "php", "js",
"java", "c", "cpp", "py", "vb", "vba", "vbs");
fileChooser.setFileFilter(filter);
}
}
public static void main (String[] args){
new NotepadDemo();
}
}
大部分代码都是从12.2中搬过来的,
也就只改了文件读写部分的代码
也就只改了文件读写部分的代码