JAVA记事本的图形用户界面应用程序

JAVA记事本的图形用户界面应用程序

JAVA记事本的图形用户界面应用程序

JAVA记事本的图形用户界面应用程序-LMLPHP

整体分析:

JAVA记事本的图形用户界面应用程序-LMLPHP

代码实现:

import java.awt.EventQueue;
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 java.io.IOException; import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Font; /**记事本的图形用户界面应用程序
* @author 李祖林
* 2017-6-22
*/
public class Note1 implements ActionListener{ private JFrame frame;
JMenuItem New, Open, Save, Close; /*
* New为新建,
* Open为打开
* Save为保存
* Close为关闭
*/
JTextArea textArea;
File fileName = new File("D:\\file.txt"); //默认打开文件路径 public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Note1 window = new Note1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} public Note1() { frame = new JFrame();
frame.setFont(new Font("Dialog", Font.BOLD, 18));
frame.setBounds(100, 100, 629, 412);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); JMenu menu = new JMenu("\u6587\u4EF6");
menu.setFont(new Font("黑体", Font.BOLD, 18));
menuBar.add(menu); New = new JMenuItem("\u65B0\u5EFA");
New.setFont(new Font("黑体", Font.BOLD, 18));
menu.add(New);New.addActionListener(this); Open = new JMenuItem("\u6253\u5F00");
Open.setFont(new Font("黑体", Font.BOLD, 18));
menu.add(Open);Open.addActionListener(this); Save = new JMenuItem("\u4FDD\u5B58");
Save.setFont(new Font("黑体", Font.BOLD, 18));
menu.add(Save);Save.addActionListener(this); JSeparator separator = new JSeparator();
menu.add(separator); Close = new JMenuItem("\u5173\u95ED");
Close.setFont(new Font("黑体", Font.BOLD, 18));
menu.add(Close);Close.addActionListener(this); JMenu menu_1 = new JMenu("\u7F16\u8F91");
menu_1.setFont(new Font("黑体", Font.BOLD, 18));
menuBar.add(menu_1); JMenuItem menuItem_4 = new JMenuItem("\u590D\u5236");
menu_1.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("\u7C98\u8D34");
menu_1.add(menuItem_5); JMenuItem menuItem_6 = new JMenuItem("\u526A\u5207");
menu_1.add(menuItem_6); textArea = new JTextArea();
frame.getContentPane().add(textArea, BorderLayout.CENTER);
} public void actionPerformed(ActionEvent e) {
if(e.getSource()==Close){
System.exit(0);
}
else if (e.getSource()==Save) {
saveFile();
}else if (e.getSource()==Open){
readFile();
}else{
textArea.setText("");
} }
/*打开文件函数*/
public void readFile(){
try{
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName)); //缓存字符流
StringBuffer buffer = new StringBuffer();
String text;
while((text = bufferedReader.readLine()) != null){
// buffer.append(text+"\n");
textArea.append(text+"\n");
}
// textArea.setText(buffer.toString());
bufferedReader.close();
}catch (IOException e) {
System.err.println("读入文件发生错误!");
}
}
/*保存文件函数*/
public void saveFile(){
try{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
String string = textArea.getText();
bufferedWriter.write(string);
bufferedWriter.close();
}catch (IOException e) {
System.err.println("保存文件发生错误!");
}
} }

实验结果:

JAVA记事本的图形用户界面应用程序-LMLPHP

04-28 20:29