//package note;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//因为JTextArea不是继承TextArea,所以没有addTextListener方法,选用键盘监听
//要对窗口,按钮,文本进行监听,如果都继承的话,WindowListener和KeyListener有很多方法要重写
//而ActionListener只有一个方法要重写,所以选择窗口匿名内部类,继承KeyAdapter,实现ActionListener
public class Note2 extends KeyAdapter implements ActionListener{
//明确变量
private JFrame frame;
private JTextArea text;
private JPanel panel;
private JButton open,newFile,save,saveAs,about;
private String textName;
private String fpath;
private String fname;
private Boolean textChange=false;
private Boolean openTrueNewFalse=false;
public Note2(){
frame=new JFrame();
text=new JTextArea();
panel=new JPanel();
open=new JButton("打开");
newFile=new JButton("新建");
save=new JButton("保存");
saveAs=new JButton("另存为");
about=new JButton("关于");
textName="无标题 - 小事本";
}
public void displayWindow(){
frame.setTitle(textName);
frame.setSize(600,400);
frame.setLocation(300,200);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
text.setFont(new Font("Fixedsys",0,18));
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setCursor(new Cursor(2));//可以改变光标的形状
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
save.setEnabled(false);
saveAs.setEnabled(false);
panel.add(open);
panel.add(newFile);
panel.add(save);
panel.add(saveAs);
panel.add(about);
frame.add(new JScrollPane(text));
frame.add(panel,BorderLayout.NORTH);//这是哪个类里找到的方法
open.addActionListener(this);
newFile.addActionListener(this);
about.addActionListener(this);
save.addActionListener(this);
saveAs.addActionListener(this);
text.addKeyListener(this);
frame.addWindowListener(new WindowAdapter()//匿名内部类,很不懂,所以只用了一个
{
public void windowClosing(WindowEvent e)
{
if(textChange){
int option=JOptionPane.showConfirmDialog(frame, "文件内容已经改变,是否保存?");
if(option==JOptionPane.YES_OPTION){
saveText();
if(!textChange){
System.exit(0);
}
} else if(option==JOptionPane.NO_OPTION){
System.exit(0);
} else if(option==JOptionPane.CANCEL_OPTION){
//点取消的话窗口关闭系统不关闭,预想效果窗口系统都不关闭
//解决方法:frame.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
}
} else {
System.exit(0);
}
}
}
);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand()=="打开"){
this.openText();
}else if(ae.getActionCommand()=="新建"){
this.creatNewText();
}else if(ae.getActionCommand()=="保存"){
this.saveText();
}else if(ae.getActionCommand()=="另存为"){
this.saveAnthorText();
}else if(ae.getActionCommand()=="关于"){
String message = " ----小事本----\n 版本:Version 2.0\n 感谢您的使用!";
JOptionPane.showMessageDialog(frame, message, "关于...",JOptionPane.PLAIN_MESSAGE);
}
}
//打开
private void openText() {
if(textChange){
int option=JOptionPane.showConfirmDialog(frame, "文件内容已经改变,是否保存?");
if(option==JOptionPane.YES_OPTION){
this.saveText();
//之前是新建,弹出另存为,没有选择保存,判断是否保存,是打开对话框,否什么都不做
if(!textChange){
this.openDialog();
}
} else if(option==JOptionPane.NO_OPTION){
this.openDialog();
}
} else {
this.openDialog();
}
}
//打开对话框,打开方法中用到
private void openDialog() {
JFileChooser fchooser=new JFileChooser();
int result=fchooser.showDialog(frame,"打开");
if(result==JFileChooser.APPROVE_OPTION){
File file=fchooser.getSelectedFile();
fpath=file.getAbsolutePath();
fname=file.getName();
textName=fname+" - 小事本";
frame.setTitle(textName);
text.setText("");
BufferedReader br=null;
String line;
try{
br=new BufferedReader(new FileReader(file));
while((line=br.readLine())!=null){
text.append(line+System.getProperty("line.separator"));
}
}
catch(Exception e ){
}
finally{
textChange=false;
this.openTrueNewFalse=true;
try{
br.close();
}
catch(IOException e){
}
}
}
}
//新建
private void creatNewText() {
if(textChange){
int option=JOptionPane.showConfirmDialog(frame, "文件内容已经改变,是否保存?");
if(option==JOptionPane.YES_OPTION){
this.saveText();
//这里要判断下是否保存,如果保存,textChange为false
if(!textChange){
this.textName="无标题 - 小事本";//“打开”的时候这个变量会改变
frame.setTitle(textName);
text.setText("");
save.setEnabled(false);
saveAs.setEnabled(false);
this.openTrueNewFalse=false;
}
} else if(option==JOptionPane.NO_OPTION){
this.textName="无标题 - 小事本";//“打开”的时候这个变量会改变
frame.setTitle(textName);
text.setText("");
save.setEnabled(false);
saveAs.setEnabled(false);
this.openTrueNewFalse=false;
textChange=false;
}
} else {
this.textName="无标题 - 小事本";//“打开”的时候这个变量会改变
frame.setTitle(textName);
text.setText("");
save.setEnabled(false);
saveAs.setEnabled(false);
this.openTrueNewFalse=false;
}
}
//保存
private void saveText() {
//判断之前是打开还是新建
if(openTrueNewFalse){
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter(fpath));
bw.write(text.getText());
bw.flush();
}
catch(Exception e ){
}
finally{
textChange=false;
try{
bw.close();
}
catch(IOException e){
}
}
} else
this.saveAnthorText();
}
//另存为
private void saveAnthorText() {
JFileChooser fchooser=new JFileChooser();
int result=fchooser.showDialog(frame,"另存为");
if(result==JFileChooser.APPROVE_OPTION){
File file=fchooser.getSelectedFile();
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter(file));
bw.write(text.getText(), 0, text.getText().length());
bw.flush();
}
catch(Exception e ){
}
finally{
textChange=false;
try{
bw.close();
}
catch(IOException e){
}
}
}
}
public void keyPressed(KeyEvent ke) {
textChange=true;
save.setEnabled(true);
saveAs.setEnabled(true);
}
public static void main(String[] args){
Note2 note2=new Note2();
note2.displayWindow();
}
}
08-21
08-21
08-21