📄 mainframe.java
字号:
copy_tool.addActionListener(this);
paste_tool.addActionListener(this);
delete_tool.addActionListener(this);
find_tool.addActionListener(this);
//findnext_tool.addActionListener(this);
datetime_tool.addActionListener(this);
//repalcenext_tool.addActionListener(this);
//格式栏
fontfamily_format.addActionListener(this);
fontsize_format.addActionListener(this);
fontbold_format.addActionListener(this);
fontitcal_format.addActionListener(this);
fontu_format.addActionListener(this);
forcolor_format.addActionListener(this);
///鼠标右键事件监听注册
/*
Mouse_Back.addActionListener(this);
Mouse_Cut.addActionListener(this);
Mouse_Copy.addActionListener(this);
Mouse_Paste.addActionListener(this);
Mouse_Del.addActionListener(this);
Mouse_Font.addActionListener(this);
Mouse_Color.addActionListener(this);
Mouse_Find.addActionListener(this);
Mouse_Replace.addActionListener(this);
Mouse_SelectAll.addActionListener(this);
*/
}
//设置组件的可见性
void SetItemStatus()
{
///设置工具栏、格式栏、状态栏的状态为不可见
MenuBar.setVisible(true);
//StatusBar.setVisible(true);
ToolBar.setVisible(false);
FormatBar.setVisible(false);
StatusBar.setVisible(false);
File_PageSet.setEnabled(false);
File_Print.setEnabled(false);
print_tool.setEnabled(false);
duixiang.setEnabled(false);
print_tool.setEnabled(false);
fontu_format.setEnabled(false);
Edit_Back.setEnabled(false);
back_tool.setEnabled(false);
Help_Name.setEnabled(false);
}
//窗体布局,构造函数
void SetFrame() {
//设置文本域属性
TextArea.setFont(new Font("宋体",0,17));
TextArea.setSelectedTextColor(Color.blue);
TextArea.setSelectionColor(Color.BLACK);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add("North",MenuBar);
panel.add("Center",ToolBar);
panel.add("South",FormatBar);
getContentPane().add("North",panel);
getContentPane().add("Center",jsp);
getContentPane().add("South",StatusBar); //添加状态栏
setSize(600,400);
setExtendedState(JFrame.MAXIMIZED_BOTH);//窗口大小设置
setVisible(true); //显示窗口
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
File_Exit.doClick();
}});
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
// </editor-fold>
public MainFrame()
{
super("文档--写字板");
SetMenu();//设置菜单
SetBar();//设置工具、格式条
SetEventListen();//注册监听
SetFrame();//设置布局
SetItemStatus();//组件可见与可用性
// SetMouse();
}
// <editor-fold defaultstate="collapsed" desc="逻辑函数 ">
//新建文件
void NewFile(){
//情况(一)如果没有建立这个文件,并且文档内容不为空
if(file == null && !TextArea.getText().equals("")){
//弹出对话框提示保存
int i = JOptionPane.showConfirmDialog(this,"文件---无标题 的文字已改变。\n" + "想保存文件吗?","计事本",1);
if(i == 0) { ////返回的是确定,则保存文件。并将文本内容清空。
SaveAsFile();
TextArea.setText("");
}
else if(i == 1) TextArea.setText("");////返回的是取消
//不进行保存,清空file的信息,保存状态设为false,完全初始化新文档
checksave = false;
file = null;
}
//情况(二)如果文件建立情况不为空,但文本内容进行了改变,提示是否保存,保存后初始化新文档
if(file != null && !checksave){
//showConfirmDialog警告对话框,最后一个参数表明按钮类型
int i = JOptionPane.showConfirmDialog(this,"文件 " + file.getPath() + " 的文字已改变。\n" + "想保存文件吗?","计事本",1);
if(i == 0) {
SaveFile();
TextArea.setText("");
file = null;
setTitle("新建文本文档----记事本");
}
else if(i == 1) {
TextArea.setText("");
file = null;
setTitle("新建文本文档----记事本");
}
}
Get_Status();
}
//打开事件
void OpenFile(){
//如果文本内容为空,直接打开文件,并改变其保存状态
if(TextArea.getText().equals(""))
{
openFile_1();
checksave = false;
}
//如果文本内容不为空,并且没保存
else if(!checksave&&!TextArea.getText().equals(""))
{
int i = JOptionPane.showConfirmDialog(this,"记事本 的文字已改变。\n" + "想保存文件吗?","计事本",1);
if(i == 0) { SaveAsFile(); openFile_1(); }//确定
else if(i == 1)
{
openFile_1();
checksave = false;
}
}
}
void openFile_1(){
final JFileChooser fc = new JFileChooser();//构建文件选择对话框
int returnVal = fc.showOpenDialog(this);
/*当用户按下"打开文件"按钮时,JFileChooser的showOpenDialog()方法会输出文件对话框
*当使用选择完后,会将选择结果存到result变量中.
*/
if(returnVal == JFileChooser.CANCEL_OPTION)//如用户点击取消,则返回
return;
if(returnVal == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();//选择文件
setTitle(file.getName() + "---写字板");
//TextArea.append("Opening:" + file.getName() + "." + newline);
//TextArea.setText("");
if(file.exists())
{
try
{//流读取
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
//读取到最后
while ((line = in.readLine()) != null)
TextArea.append(line + newline);//显示出
in.close();
TextArea.setCaretPosition(0);//光标置于最末尾
}
catch(IOException ioe)
{
System.err.println(ioe);//读取错误输出
}
}
else
{
JOptionPane.showMessageDialog(null,"文件不存在","错误",0);
}
}
Get_Status();//状态栏事件
}
//保存事件
void SaveFile(){
if( file != null ){//如果已经存在就用现在的新内容进行覆盖,否者就用另存为的方式写入流
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);//写入当前文档
}
catch(FileNotFoundException fe)
{
System.err.println("文件未找到");
return;
}
String content=TextArea.getText();//取得内容
try
{
fos.write(content.getBytes());//写入
}
catch(IOException ioe)
{
System.err.println(ioe);
}
finally
{
try
{
if (fos != null)
fos.close();
}
catch(IOException ioe2){}
}
}
//如果不存在文件,就打开另存为对话框进行保存
else{
SaveAsFile();
}
Get_Status();
}
//文件另存为事件 ,两重情况,一种是文件已经存在,剩下的就是直接存
void SaveAsFile(){
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
file = fc.getSelectedFile();
if(returnVal == JFileChooser.APPROVE_OPTION){
setTitle("新建文档--写字板");
}
//写入文件流
FileOutputStream fos = null;
//.getCurrentDirectory()当前目录路径。file1另存为文件保存的文件状态
File file1 = new File(fc.getCurrentDirectory(),file.getName());
if(file1.exists()){
int i = JOptionPane.showConfirmDialog(this,"文件 " + file.getPath() + " 已存在\n" + "要替换它吗?","另存为",JOptionPane.YES_NO_OPTION);
if(i == 1 ){
//如果确定,则将保存状态设为已经保存
checksave = true;}
}
//如果文件是第一次存,就是第二种情况,而且也可以让保存文件事件公用这个步骤
if (file != null && !checksave){
try
{
fos = new FileOutputStream(file);
}
catch(FileNotFoundException fe)
{
System.err.println("文件未找到");
return;
}
String content = TextArea.getText();
try
{
fos.write(content.getBytes());
}
catch(IOException ioe)
{
System.err.println(ioe);
}
finally//关闭流
{
try
{
if (fos != null)
fos.close();
}
catch(IOException ioe2){}
}
}
file = file1;
}
//退出事件
void Exit()
{
if(file == null && TextArea.getText().equals("")) System.exit(0);
if (! checksave && !TextArea.getText().equals("")){
int i = JOptionPane.showConfirmDialog(null,"文件尚未保存\n想保存文件吗?","写字板",1,2);
if (i == 0){ SaveAsFile(); System.exit(0); }
else if (i == 1) System.exit(0);
}
if(file != null){
int i = JOptionPane.showConfirmDialog(null,"文件 " + file.getName() + "已经改变\n想保存当前已修改文件吗?","写字板",1,2);
if(i == 0){ SaveFile(); System.exit(0);}
else if(i == 1) System.exit(0);
}
}
//"关于"记事本
void AboutMe()
{
JOptionPane.showMessageDialog(null,"写字板,制作人邓秋","关于作者",3,new ImageIcon("pic/aboutme.GIF"));
}
//状态栏事件 ---- 显示光标所在位置
void Get_Status(){
//如果状态栏被选中将状态栏可见,并判断此文档的状态,已经建立的则写此状态,没有就else
if(Status_Bar.isSelected()){
StatusBar.setVisible(true);
if(file != null)
StatusBar.setText("写字板------" + file.getPath() + "--------------------------------------------------");
else
StatusBar.setText("写字板------未命名文档" + "-------------------------------------------");
}
else StatusBar.setVisible(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -