JAVA 基本实现记事本功能

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import javax.swing.colorchooser.*;
import javax.swing.border.TitledBorder;

class MyTXT
{
private JFrame f;
private JMenuBar mb;
private JMenu fileMenu,editMenu,styleMenu,helpMenu;
private JMenuItem newfileItem,openfileItem,savefileItem,printItem,closeItem,copyItem,cutItem,pasteItem,deleteItem,selectAllItem,findItem,fontItem,colorItem,aboutItem,copyPopupItem,cutPopupItem,pastePopupItem;
private JTextArea jta;
private JFileChooser jfc;
private JScrollPane jsp;
private JCheckBoxMenuItem word_wrap;
private DefaultColorSelectionModel model;
private JColorChooser colorChooser;
private Color newColor;
private JComboBox fontCombo,typeCombo,sizeCombo,cb;
private JButton confirmButton,cancelButton;
private JPanel showPanel;
private int sizeInt,typeInt;
private String fontString,typeString,sizeString,name="记事本";
private JLabel previewLabel;
private PrintJob p=null;
private Graphics g=null;
private JDialog colorDialog,fontDialog;
private JPopupMenu popupMenu;

MyTXT()
{
setEverything();
}

public void setEverything()
{
new_frame(); //新建窗口

file_Menu(); //文件菜单

edit_Menu(); //编辑菜单

style_Menu(); //格式菜单

help_Menu(); //帮助菜单

popup_Menu(); //鼠标右击菜单

}


private void new_frame() //新建窗口
{

f=new JFrame(name);//创建窗口f
f.setBounds(300,100,700,500);//设置窗口f大小位置
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

mb=new JMenuBar(); //创建菜单栏
f.setJMenuBar(mb); //把菜单栏加入窗口

jta=new JTextArea(); //创建文本区jta
jta.setFont(new Font("宋体",Font.PLAIN,20));//设置文本区jta字体
jsp=new JScrollPane(jta); //把文本区jta放入滚动窗格jsp
f.add(jsp,BorderLayout.CENTER); //把滚动窗格jsp加入窗口f

}

private void file_Menu() //文件菜单
{
fileMenu=new JMenu("文件(F)"); //创建文件菜单fileMenu
fileMenu.setMnemonic('F'); //为文件菜单fileMenu设置热键F
mb.add(fileMenu); //把文件菜单fileMenu加入菜单栏
jfc=new JFileChooser(new File("."));//创建文件选择对话框jfc

newfile_Item(); //新建文件菜单项
openfile_Item(); //打开文件菜单项
savefile_Item(); //保存文件菜单项
//print_Item();
close_Item(); //退出菜单项菜单项
}

private void newfile_Item() //新建文件菜单项
{
newfileItem=new JMenuItem("新建(N)"); //创建新建文件菜单项newfileItem
newfileItem.setMnemonic('N'); //为新建文件菜单项newfileItem设置热键N
fileMenu.add(newfileItem); //把新建文件菜单项newfileItem加入文件菜单

newfileItem.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
int n=JOptionPane.showConfirmDialog(null,"是否新建文件",
"新建?",JOptionPane.YES_NO_OPTION); //创建确认对话框
if(n==JOptionPane.YES_OPTION) //单击确认对话框的"是"
{
f.setTitle(name);
jta.setText("");
}
}
});
}

private void openfile_Item() //打开文件菜单项
{
openfileItem=new JMenuItem("打开(O)"); //创建打开文件菜单项openfileItem
openfileItem.setMnemonic('O'); //为打开文件菜单项openfileItem设置热键O
fileMenu.add(openfileItem); //把打开文件菜单项openfileItem加入文件菜单

openfileItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int status=jfc.showOpenDialog(null); //创建并显示打开类型的文件选择对话框
if(status==JFileChooser.APPROVE_OPTION)//单击文件选择框中的"打开"按钮
{
try
{
File file1=jfc.getSelectedFile(); //将被选中的文件保存为文件对象file1
Scanner scan=new Scanner(file1); //用file1创建一个Scanner对象scan
String info="";
while(scan.hasNext()) //判断文件中是否还有数据
{
String str=scan.nextLine();
info+=str+"\r\n";
}
jta.setText(info); //将读取到的数据放入文本区jta中
if (file1.getName() != null)
f.setTitle(file1.getName()+"--"+name);
}
catch(FileNotFoundException ioe)
{
};
}
}
});
}

private void savefile_Item() //保存文件菜单项
{
savefileItem=new JMenuItem("保存(S)"); //创建保存文件savefileItem菜单项
savefileItem.setMnemonic('S'); //为保存文件菜单项savefileItem设置热键O
fileMenu.add(savefileItem); //把保存文件菜单项savefileItem加入文件菜单

savefileItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int re=jfc.showSaveDialog(null); //创建并显示保存类型的文件选择对话框
if(re==JFileChooser.APPROVE_OPTION) //单击文件选择框中的"保存"按钮
{
try
{
File file2=jfc.getSelectedFile(); //返回用于保存文件的对象
FileOutputStream ff=new FileOutputStream(file2);
BufferedOutputStream out=new BufferedOutputStream(ff);
byte[] b =(jta.getText()).getBytes();
out.write(b,0,b.length); //将jta中的内容转化为字节,存入数组b中
out.close(); //将数组b中的全部内容写入流out所对应的文件中
}
catch(IOException ioe)
{
};
}
}
});
}

/*private void print_Item() //打印菜单项
{
printItem=new JMenuItem("打印");
fileMenu.add(printItem);

printItem.addActionListener(new ActionListener() //单击打印菜

单项
{
public void actionPerformed(ActionEvent e)
{
p=getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
g.translate(120,200);
jta.printAll(g);
g.dispose();
p.end();
}
});
}*/

private void close_Item() //退出菜单项
{
closeItem=new JMenuItem("退出(X)"); //创建退出菜单项closeItem
closeItem.setMnemonic('X'); //为退出菜单项closeItem设置热键X
fileMenu.add(closeItem); //把退出菜单项closeItem加入文件菜单

closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); //单击退出时关闭窗口
}
});
}


private void edit_Menu() //编辑菜单
{
editMenu=new JMenu("编辑(E)"); //创建编辑菜单editMenu
editMenu.setMnemonic('E'); //为编辑菜单editMenu设置热键H
mb.add(editMenu); //把编辑菜单editMenu加入菜单栏

cut_Item();
copy_Item();
paste_Item();
delete_Item();
selectAll_Item();
}

private void cut_Item() //剪切菜单项
{
cutItem=new JMenuItem("剪切 Ctrl+X");
editMenu.add(cutItem);
cutItem.addActionListener(new ActionListener() //单击剪切菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
}
});
}

private void copy_Item() //复制菜单项
{
copyItem=new JMenuItem("复制 Ctrl+C");
editMenu.add(copyItem);
copyItem.addActionListener(new ActionListener() //单击复制菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
});
}

private void paste_Item() //粘贴菜单项
{
pasteItem=new JMenuItem("粘贴 Ctrl+V");
editMenu.add(pasteItem);
pasteItem.addActionListener(new ActionListener() //单击粘贴菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
});
}

private void delete_Item() //删除菜单项
{
deleteItem=new JMenuItem("删除 del");
editMenu.add(deleteItem);
deleteItem.addActionListener(new ActionListener() //单击删除菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
}
});
}

private void selectAll_Item() //全选菜单项
{
selectAllItem=new JMenuItem("全选 Ctrl+A");
editMenu.add(selectAllItem);
selectAllItem.addActionListener(new ActionListener() //单击全选菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
}
});
}

private void find_Item()
{

}


private void style_Menu() //格式菜单
{
styleMenu=new JMenu("格式(S)"); //创建格式菜单styleMenu
styleMenu.setMnemonic('S'); //为格式菜单styleMenu设置热键H
mb.add(styleMenu); //把格式菜单styleMenu加入菜单栏

word_wrap_Item(); //自动换行菜单项
font_Item(); //字体菜单项
color_Item(); //颜色菜单项

}

private void word_wrap_Item() //自动换行菜单项
{
word_wrap=new JCheckBoxMenuItem("自动换行(W)"); //创建自动换行菜单项word_wrap
word_wrap.setMnemonic('W'); //为自动换行菜单项word_wrap设置热键A
styleMenu.add(word_wrap); //把自动换行菜单项word_wrap加入格式菜单
word_wrap.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
jta.setLineWrap(true); // 设置文本区的换行策略
}
});
}

private void font_Item() //字体菜单项
{
fontItem=new JMenuItem("字体(F)"); //创建字体菜单项fontItem
fontItem.setMnemonic('F'); //为字体菜单项fontItem设置热键F
styleMenu.add(fontItem); //把字体菜单项fontItem加入格式菜单

fontItem.addActionListener(new ActionListener() //单击字体菜单项
{
public void actionPerformed(ActionEvent e)
{
fontDialog=new JDialog(f,"字体",false); //创建字体对话框fontDialog
fontDialog.setBounds(200,200,300,400);
fontDialog.setLayout(null);
fontDialog.setResizable(false); //设置字体对话框不可改变大小
fontDialog.setVisible(true);


String[]fonts={"宋体","楷体","隶书","微软雅黑"}; //设置下拉列表框fontCombo的选项
String[]types={"正常","粗体","斜体","粗体+斜体"}; //设置下拉列表框typeCombo的选项
String[]sizes={"16","17","18","19","20","22","24","26","28","36","48","72"}; //设置下拉列表框sizeCombo的选项


fontCombo=new JComboBox(fonts); //创建下拉列表框fontCombo
fontCombo.setBounds(50,20,150,20);
fontDialog.add(fontCombo);
fontString="宋体"; //fontString的初始值为宋体
fontCombo.addItemListener(new ItemListener() //单击下拉按钮
{
public void itemStateChanged(ItemEvent e)
{
fontString=(String)fontCombo.getSelectedItem(); //获得被选中选项的名称
previewLabel.setFont(new Font(fontString,typeInt,sizeInt)); //设置标签字体
}
});


typeCombo=new JComboBox(types); //创建下拉列表框typeCombo
typeCombo.setBounds(50,60,150,20);
fontDialog.add(typeCombo);
typeInt=0;
typeCombo.addItemListener(new ItemListener() //单击下拉按钮
{
public void itemStateChanged(ItemEvent e)
{
typeString=(String)typeCombo.getSelectedItem(); //获得被选中选项的名称
typeInt=0; //typeInt的初始值为0

if(typeString=="正常")
typeInt=0;
else if(typeString=="粗体")
typeInt=1;
else if(typeString=="斜体")
typeInt=2;
else if(typeString=="粗体+斜体")
typeInt=3;

previewLabel.setFont(new Font(fontString,typeInt,sizeInt)); //设置标签字体
}
});


sizeCombo=new JComboBox(sizes); //创建

下拉列表框sizeCombo
sizeCombo.setBounds(50,100,150,20);
fontDialog.add(sizeCombo);
sizeInt=16; //sizeInt的初始值为16
sizeCombo.addItemListener(new ItemListener() //单击下拉按钮
{
public void itemStateChanged(ItemEvent e)
{
sizeString=(String)sizeCombo.getSelectedItem(); //获得被选中选项的名称
sizeInt=Integer.parseInt(sizeString);
previewLabel.setFont(new Font(fontString,typeInt,sizeInt)); //设置标签字体
}
});

showPanel=new JPanel(); //创建面板showPanel
showPanel.setBounds(25,140,250,100);
showPanel.setBorder(new TitledBorder("预览")); //设置面板边框
fontDialog.add(showPanel); //把面板加入字体窗口

previewLabel=new JLabel("我的文本文档",JLabel.CENTER); //创建预览标签
previewLabel.setFont(new Font("宋体",Font.PLAIN,16)); //设置标签初始字体
showPanel.add(previewLabel); //把标签加入面板


confirmButton=new JButton("确认"); //创建确认按钮
confirmButton.setBounds(100,330,80,20);
fontDialog.add(confirmButton); //把确认按钮加入字体窗口
confirmButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.setFont(new Font(fontString,typeInt,sizeInt)); //设置文本区字体
fontDialog.dispose();
}
});


cancelButton=new JButton("取消"); //创建取消按钮
cancelButton.setBounds(190,330,80,20);
fontDialog.add(cancelButton); //把取消按钮加入字体窗口
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fontDialog.dispose(); //单击取消按钮时关闭窗口
}
});

}
});


}

private void color_Item() //颜色菜单项
{
colorItem=new JMenuItem("颜色(C)"); //创建颜色菜单项colorItem
colorItem.setMnemonic('C'); //为颜色菜单项colorItem设置热键C
styleMenu.add(colorItem); //把颜色菜单项colorItem加入格式菜单

colorItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
colorDialog=new JDialog(f,"颜色",false); //创建颜色对话框colorDialog
colorDialog.setBounds(300,200,600,400);
colorDialog.setVisible(true);

model=new DefaultColorSelectionModel(); //设置调色板对象类型
colorChooser=new JColorChooser(model); //创建JColorChooser对象

colorDialog.add(colorChooser);


model.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
newColor=colorChooser.getColor();
jta.setForeground(newColor); //设置字体颜色
}
});

}
});
}


private void help_Menu() //帮助菜单
{
he

lpMenu=new JMenu("帮助(H)"); //创建帮助菜单helpMenu
helpMenu.setMnemonic('H'); //为帮助菜单helpMenu设置热键H
mb.add(helpMenu); //把帮助菜单helpMenu加入菜单栏


about_Item(); //关于菜单项
}

private void about_Item() //关于菜单项
{
aboutItem=new JMenuItem("关于..(A)");//创建关于菜单项aboutItem
aboutItem.setMnemonic('A'); //为关于菜单项aboutItem设置热键A
helpMenu.add(aboutItem); //把关于菜单项aboutItem加入帮助菜单

aboutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"如有雷同~纯属巧合",
"我的记事本",JOptionPane.WARNING_MESSAGE);
//关于菜单的消息对话框

}
});
}


private void popup_Menu() //鼠标右击菜单
{
popupMenu=new JPopupMenu();

copyPopupItem=new JMenuItem("复制 Ctrl+C");
cutPopupItem=new JMenuItem("剪切 Ctrl+X");
pastePopupItem=new JMenuItem("粘贴 Ctrl+V");

popupMenu.add(copyPopupItem);
popupMenu.add(cutPopupItem);
popupMenu.add(pastePopupItem);

copyPopupItem.addActionListener(new ActionListener() //单击复制菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
});

cutPopupItem.addActionListener(new ActionListener() //单击剪切菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
}
});

pastePopupItem.addActionListener(new ActionListener() //单击粘贴菜单项
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
});




jta.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent mce)
{
if(mce.isPopupTrigger())
popupMenu.show((Component)mce.getSource(),mce.getX(),mce.getY());
}
});
}


public static void main(String[]args)
{
new MyTXT();
}
}

相关文档
最新文档