📄 notepad_newfile.java
字号:
/**
* 窗内子窗体
* 每次新建都 new 一个新的对象
*/
package com.edu.sccp.snail.notepad.view;
import java.awt.BorderLayout;
import java.awt.Container;
import java.beans.PropertyVetoException;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
public class Notepad_newFile extends JInternalFrame implements InternalFrameListener ,UndoableEditListener{
private static final long serialVersionUID = 3711879703284613261L;
JTextArea jta = null;
UndoManager undo = new UndoManager();
Notepad_Font_Frame font = new Notepad_Font_Frame(); //字体设置
Notepad_Color_Frame color = new Notepad_Color_Frame(); //颜色设置窗体
String title = null; //设置名称
File file = null;
FileOutputStream fos = null;
/**
*默认的构造函数
*/
public Notepad_newFile(){
}
public Notepad_newFile(String title){
super(title,true,true,true,true);
this.setSize(300,200);
init(); //初始化
this.addInternalFrameListener(this);
this.show();
}
/**
* @param
* @return a init InterWindows
*/
public void init(){
this.setSize(400,300);
jta = new Notepad_TextArea(); //自己定义的一个JTextArea
Container c = getContentPane();
c.setLayout(new BorderLayout());
JScrollPane jsp = new JScrollPane(jta);
jta.getDocument().addUndoableEditListener(this);
c.add(jsp,BorderLayout.CENTER);
}
public void internalFrameActivated(InternalFrameEvent arg0) {
}
public void internalFrameClosed(InternalFrameEvent arg0) {
try {
this.setClosed(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public void internalFrameClosing(InternalFrameEvent arg0) {
int optionType = JOptionPane.showConfirmDialog(this,this.getTitle()+"文件是否保存","关闭文件",JOptionPane.YES_NO_CANCEL_OPTION);
if(optionType == JOptionPane.YES_OPTION)
{
Notepad_JFileChooser notepad_filechooser = new Notepad_JFileChooser(
file);
int returnVal = notepad_filechooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = notepad_filechooser.getSelectedFile();
///////////////////////////////////////////////////////////////////////////////////
String str = null;
byte buf[] = new byte[1024];
if(file !=null){
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}if(file.canWrite()){
try {
fos = new FileOutputStream(file);
str = jta.getText();
buf= str.getBytes();
fos.write(buf,0,str.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}}else {
JOptionPane.showMessageDialog(this,"没选择文件");
return;
}
}
else if(optionType == JOptionPane.CANCEL_OPTION ) {
try {
this.setClosed(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
} return;
}else if(optionType == JOptionPane.NO_OPTION){
//关闭自己的代码怎么写?
}
}
public void internalFrameDeactivated(InternalFrameEvent arg0) {
}
public void internalFrameDeiconified(InternalFrameEvent arg0) {
}
public void internalFrameIconified(InternalFrameEvent arg0) {
}
public void internalFrameOpened(InternalFrameEvent arg0) {
}
public void undoableEditHappened(UndoableEditEvent arg0) {
undo.addEdit(arg0.getEdit());
}
public UndoManager getUndo() {
return undo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -