import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.String;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.awt.GraphicsEnvironment;
//import javax.accessibility.*;
//import javax.swing.JToolTip;
import java.awt.Font;
//import javax.swing.text.StyleConstants;
import java.awt.event.ItemListener;
//import java.text.AttributedCharacterIterator.Attribute;
//import java.util.*;
//import java.text.*;
class WenBen extends JFrame implements ActionListener, ItemListener {
//文件菜单的新建、打开、保存、另存、退出
//编辑菜单的剪切、复制、粘贴、撤消、删除、全选
//格式菜单字体,帮助菜单
private static final long serialVersionUID = 1L;
JMenuItem miFileNew=null;
JMenuItem miFileOpen=null;
JMenuItem miFileSave=null;
JMenuItem miFileSaveAs=null;
JMenuItem miFileExit=null;
JMenuItem miEditCut=null;
JMenuItem miEditCopy=null;
JMenuItem miEditPaste=null;
JMenuItem miEditUndo=null;
JMenuItem miEditDelete=null;
JMenuItem miEditSeleteAll=null;
JMenuItem menuItemFont=null;
JMenuItem menuItemHelp=null;
//弹出式菜单的撤消、剪切、复制、粘贴、删除
JMenuItem popupUndo=null;
JMenuItem popupCut=null;
JMenuItem popupCopy=null;
JMenuItem popupPaste=null;
JMenuItem popupDelete=null;
//工具栏上的按钮
JButton btnNew=null;
JButton btnOpen=null;
JButton btnSave=null;
JButton btnBold=null;
JButton btnItalic=null;
JButton btnXiahuaxian=null;
JCheckBox cbItalic = null; //斜体复选框
JCheckBox cbBold = null; //粗体复选框
/**
* 文本框
*/
JTextArea textArea=null;
/**
* 弹出式菜单
*/
JPopupMenu popupMenu=null;
JMenuItem menuItemColor=null;
String fileName=null; //文件名
boolean isSaved=false; //文件是否已保存标志
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
JLabel label=null;
JMenuBar menuBar=new JMenuBar();
//字体名
String fontName[];
//fontSize 字号
String [] fontSize = new String[ 63 ];
JComboBox cbFontName=null;
JComboBox cbFontSize=null;
String fontNameStyle = "宋体";
int boldStyle = 0;
int italicStyle = 0;
int fontSizeStyle = 10;
Font font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
//构造函数
public WenBen() {
super("一个简单的记事本");
GraphicsEnvironment age = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = age.getAvailableFontFamilyNames();
for( int i = 0; i < fontSize.length; i++ ){
fontSize[ i ] = Integer.toString( ( i + 10 ) );
}
buildFileMenu();
buildEditMenu();
buildGeshiMenu();
buildColorMenu();
buildHelpMenu();
buildText();
buildPopupMenu();
buildToolBar();
InitListener();
this.pack();
//this.show();
this.setVisible( true );
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
/**
* 文件菜单的创建
*/
public void buildFileMenu() {
JMenu menu=new JMenu("文件");
menu.setMnemonic('F');
miFileNew=new JMenuItem("新建");
miFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
miFileOpen=new JMenuItem("打开");
miFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));
miFileSave=new JMenuItem("保存");
miFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));
miFileSaveAs=new JMenuItem("另存为...");
miFileExit=new JMenuItem("退出");
miFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));
menu.add(miFileNew);
menu.add(miFileOpen);
menu.add(miFileSave);
menu.add(miFileSaveAs);
menu.addSeparator();
menu.add(miFileExit);
menuBar.add(menu);
this.setJMenuBar(menuBar);
}
/**
* 编辑菜单的创建
*/
public void buildEditMenu() {
JMenu menu=new JMenu("编辑");
menu.setMnemonic('E');
miEditCut=new JMenuItem("剪切");
miEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK));
miEditCopy=new JMenuItem("复制");
miEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK));
miEditPaste=new JMenuItem("粘贴");
miEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK));
miEditUndo=new JMenuItem("撤消");
miEditUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK));
miEditDelete=new JMenuItem("删除");
miEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.CTRL_MASK));
miEditSeleteAll=new JMenuItem("全选");
miEditSeleteAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK));
menu.add(miEditUndo);
menu.addSeparator();
menu.add(miEditCut);
menu.add(miEditCopy);
menu.add(miEditPaste);
menu.addSeparator();
menu.add(miEditDelete);
menu.add(miEditSeleteAll);
menuBar.add(menu);
this.setJMenuBar(menuBar);
}
/**
* 格式菜单的创建
*/
public void buildGeshiMenu() {
JMenu geshi=new JMenu("格式");
menuItemFont=new JMenuItem("字体...");
geshi.add(menuItemFont);
menuBar.add(geshi);
this.setJMenuBar(menuBar);
}
/**
* 颜色菜单的创建
*/
public void buildColorMenu() {
JMenu color=new JMenu("颜色");
menuItemColor=new JMenuItem("编辑颜色...");
color.add(menuItemColor);
menuBar.add(color);
this.setJMenuBar(menuBar);
}
/**
* 帮助菜单的创建
*/
public void buildHelpMenu() {
JMenu help= new JMenu("帮助");
menuItemHelp =new JMenuItem("帮助主题");
help.add(menuItemHelp);
menuBar.add(help);
this.setJMenuBar(menuBar);
}
/**
* 创建弹出式菜单
*/
public void buildPopupMenu()
{
popupMenu=new JPopupMenu();
popupMenu.setPopupSize(new Dimension(100,150));
popupUndo=new JMenuItem("撤消");
popupUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.ALT_MASK));
popupCut=new JMenuItem("剪切");
popupCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.ALT_MASK));
popupCopy=new JMenuItem("复制");
popupCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.ALT_MASK));
popupPaste=new JMenuItem("粘贴");
popupPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.ALT_MASK));
popupDelete=new JMenuItem ("删除");
popupDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.ALT_MASK));
popupMenu.add(popupUndo);
popupMenu.addSeparator();
popupMenu.add(popupCut);
popupMenu.add(popupCopy);
popupMenu.add(popupPaste);
popupMenu.addSeparator();
popupMenu.add(popupDelete);
textArea.add(popupMenu);
textArea.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(popupMenu.isPopupTrigger(e))
popupMenu.show(textArea,e.getX(),e.getY());
}
});
}
public void buildText() {
textArea=new JTextArea(20,40);
JScrollPane scrollPanel=new JScrollPane(textArea);
this.getContentPane().add(scrollPanel);
label=new JLabel("状态栏");
label.setToolTipText((String)label.getName());
this.getContentPane().add(scrollPanel,"Center");
this.getContentPane().add(label,"South");
}
/**
* 工具栏的创建
*/
public void buildToolBar() {
JToolBar tool=new JToolBar();
tool.setFloatable(true);
btnNew=new JButton("新建",new ImageIcon("C://windows//Coffee.gif"));
btnNew.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
//btnNew.setToolTipText((String)btnNew.TOOL_TIP_TEXT_KEY.valueOf("新建文件"));
btnNew.setToolTipText( "新建文件" );
}
});
btnOpen=new JButton("打开",new ImageIcon("C://windows//Coffee.gif"));
btnOpen.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
//btnOpen.setToolTipText((String)btnOpen.TOOL_TIP_TEXT_KEY.valueOf("打开"));
btnOpen.setToolTipText( "打开" );
}
});
btnSave=new JButton("保存",new ImageIcon("C://windows//Coffee.gif"));
btnSave.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnSave.setToolTipText((String)btnSave.TOOL_TIP_TEXT_KEY.valueOf("保存"));
btnSave.setToolTipText( "保存" );
}
});
btnBold=new JButton("B");
btnBold.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnBold.setToolTipText((String)btnBold.TOOL_TIP_TEXT_KEY.valueOf("粗体"));
btnBold.setToolTipText( "粗体" );
}
});
btnItalic=new JButton("斜体");
btnItalic.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnItalic.setToolTipText((String)btnItalic.TOOL_TIP_TEXT_KEY.valueOf("斜体"));
btnItalic.setToolTipText( "斜体" );
}
});
btnXiahuaxian=new JButton("下划线");
btnXiahuaxian.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
//btnXiahuaxian.setToolTipText((String)btnXiahuaxian.TOOL_TIP_TEXT_KEY.valueOf("下划线"));
btnXiahuaxian.setToolTipText( "下划线" );
}
});
cbBold = new JCheckBox("粗体");
cbBold.addItemListener( new ItemListener( ){
public void itemStateChanged(ItemEvent e){
//实现监听选择粗体状态改变的方法
Font fontTmp = textArea.getFont();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
if( fontTmp.isBold() ){
font = fontTmp.deriveFont( fi );
}
else{
font = fontTmp.deriveFont( fi + Font.BOLD );
}
textArea.setFont( font );
}
} );
cbItalic = new JCheckBox("斜体");
cbItalic.addItemListener( new ItemListener( ){
public void itemStateChanged(ItemEvent e){
//实现监听选择粗体状态改变的方法
Font fontTmp = textArea.getFont();
int fb = fontTmp.isBold() ? Font.BOLD : 0;
if( fontTmp.isItalic() ){
font = fontTmp.deriveFont( fb );
}
else{
font = fontTmp.deriveFont( fb + Font.ITALIC );
}
textArea.setFont( font );
}
} );
JLabel label=new JLabel("字体类型:");
JLabel labe2=new JLabel("字体大小:");
cbFontSize=new JComboBox( fontSize );
cbFontSize.setEditable(true);
cbFontSize.addItemListener( new ItemListener(){
public void itemStateChanged( ItemEvent e ){
int sizeF = Integer.parseInt( (String)cbFontSize.getSelectedItem() );
font = new Font( fontNameStyle,boldStyle+italicStyle,sizeF );
textArea.setFont( font );
}
});
cbFontName=new JComboBox( fontName );
cbFontName.setEditable( true );
tool.add(btnNew);
tool.add(btnOpen);
tool.add(btnSave);
tool.addSeparator();
tool.add(btnBold);
tool.add(btnItalic);
tool.add(btnXiahuaxian);
tool.add( cbItalic );
tool.add( cbBold );
tool.addSeparator();
tool.add(labe2);
tool.add(cbFontSize);
tool.addSeparator();
tool.add(label);
tool.add(cbFontName);
add( tool, BorderLayout.NORTH);
}
/**
* 事件初始化
*/
public void InitListener(){
/**
* 文件事件
*/
miFileNew.addActionListener(this);
miFileOpen.addActionListener(this);
miFileSave.addActionListener(this);
miFileSaveAs.addActionListener(this);
miFileExit.addActionListener(this);
/**
* 工具栏上的新建,打开,保存事件
*/
btnNew.addActionListener(this);
btnOpen.addActionListener(this);
btnSave.addActionListener(this);
btnBold.addActionListener(this);
btnItalic.addActionListener(this);
btnXiahuaxian.addActionListener(this);
/**
* 编辑菜单上的事件
*/
miEditCut.addActionListener(this);
miEditCopy.addActionListener(this);
miEditPaste.addActionListener(this);
miEditDelete.addActionListener(this);
miEditSeleteAll.addActionListener(this);
menuItemFont.addActionListener(this);
/**
* 颜色菜单上的事件
*/
menuItemColor.addActionListener(this);
menuItemHelp.addActionListener(this);
/**
* 弹出菜单的事件
*/
popupCopy.addActionListener(this);
popupCut.addActionListener(this);
popupPaste.addActionListener(this);
popupDelete.addActionListener(this);
popupUndo.addActionListener(this);
cbFontName.addActionListener( this );
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==miFileNew)
fileNew();
if(e.getSource()==miFileOpen)
fileOpen();
if(e.getSource()==miFileSave)
fileSave();
if(e.getSource()==miFileSaveAs)
fileSaveAs();
if(e.getSource()==miFileExit)
fileQuit();
/**
* 以下为工具栏菜单上的按钮
*/
if(e.getSource()==btnNew)
toolNew();
if(e.getSource()==btnOpen)
toolOpen();
if(e.getSource()==btnSave)
toolSave();
if(e.getSource()==btnBold)
toolBold();
if(e.getSource()==btnItalic)
toolXieti();
if(e.getSource()==btnXiahuaxian)
toolXia();
/**
* 以下为编辑上的菜单
*/
if(e.getSource()==miEditCut)
editCut();
if(e.getSource()==miEditCopy)
editCopy();
if(e.getSource()==miEditPaste)
editPaste();
if(e.getSource()==miEditDelete)
editDelete();
if(e.getSource()==miEditSeleteAll)
editSelectAll();
if(e.getSource()==cbFontSize)
fontSelect();
if(e.getSource()==cbFontName)
fontType();
if(e.getSource()==menuItemHelp)
helpHelp();
if(e.getSource()==menuItemColor)
colorChooser();
if(e.getSource()==popupCopy)
popupCopy();
if(e.getSource()==popupCut)
popupCut();
if(e.getSource()==popupPaste)
popupPaste();
if(e.getSource()==popupDelete)
popupDelete();
}
/**
* 文件新建事件
*/
public void fileNew() {
if(isSaved) {
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("记事本");
}
else {
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_OPTION);//保存选择按钮
if(JOptionPane.YES_OPTION==result) {
fileSaveAs();
}
else if(JOptionPane.NO_OPTION==result) {
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("记事本");
}
else{
}
}
}
/**
* 文件打开事件
*/
public void fileOpen() {
String openFileName="";
JFileChooser jFileChooser=new JFileChooser();
if(isSaved) {
try
{
if(JFileChooser.APPROVE_OPTION==jFileChooser.showOpenDialog(this)){
openFileName=jFileChooser.getSelectedFile().getPath();
File file=new File(openFileName);
int flength=(int)file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[]data=new char[flength];
while(fReader.ready()){
num+=fReader.read(data,num,flength-num);
}
fReader.close();
textArea.setText(new String(data,0,num));
fileName=openFileName;
this.setTitle(fileName.substring(fileName.lastIndexOf("//")));
this.repaint();
isSaved=true;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"打开文件出错","错误",JOptionPane.ERROR_MESSAGE);
}
}
else {
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.YES_OPTION==result) {
fileSave();
fileOpen();
}
else if(JOptionPane.NO_OPTION==result) {
isSaved=true;
fileOpen();
}
else {}
}
}
/**
* 添加文件保存事件
*/
public void fileSave()
{
if(fileName==null)
{
fileSaveAs();
}
else
{
if(!isSaved)
{
try
{
File saveFile=new File(fileName);
FileWriter fw=new FileWriter(saveFile);
fw.write(textArea.getText());
fw.close();
isSaved=true;
this.setTitle(fileName.substring(fileName.lastIndexOf("//")));
this.repaint();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"保存文件出错","错误",JOptionPane.ERROR);
}
}
else
{
fileSaveAs();
}
}
}
/**
* 添加另存为事件
*/
public void fileSaveAs() {
JFileChooser jFileChooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this)) {
fileName=jFileChooser.getSelectedFile().getPath();
fileSave();
}
}
/**
* 当文件没有保存时,要提醒用户你的文件没有保存,
*/
public void fileQuit(){
if(!isSaved){
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_OPTION);
if(JOptionPane.YES_OPTION==result)
fileSave();
else if(JOptionPane.NO_OPTION==result)
System.exit(0);
/**
* 良好的编程让if和else匹配
*/
else {}
}
else {
System.exit(0);
}
}
/**
* 工具栏上的新建
*/
public void toolNew(){
fileNew();
}
/**
* 工具栏上的打开
*/
public void toolOpen(){
fileOpen();
}
/**
* 工具栏上的保存
*/
public void toolSave(){
fileSave();
}
/**
* 工具栏上的斜体
*/
public void toolXieti(){
Font fontTmp = textArea.getFont();
int fb = fontTmp.isBold() ? Font.BOLD : 0;
if( fontTmp.isItalic() ){
font = fontTmp.deriveFont( fb );
}
else{
font = fontTmp.deriveFont( fb + Font.ITALIC );
}
textArea.setFont( font );
}
/**
* 工具栏上的粗体
*/
public void toolBold(){
/*Font fontTmp = textArea.getFont();
String fn = fontTmp.getFontName();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
int fs = fontTmp.getSize();
if ( fontTmp.isBold() ){
textArea.setFont( new Font( fn, fi, fs ) );
}
else{
textArea.setFont( new Font( fn, fi + Font.BOLD, fs ) );
}
*/
Font fontTmp = textArea.getFont();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
if( fontTmp.isBold() ){
font = fontTmp.deriveFont( fi );
}
else{
font = fontTmp.deriveFont( fi + Font.BOLD );
}
textArea.setFont( font );
}
/**
*字号选择
*/
public void fontSelect(){
float fontSize=Float.parseFloat( ( String )cbFontSize.getSelectedItem() );
Font fontTmp = textArea.getFont();
font = fontTmp.deriveFont( fontSize );
textArea.setFont( font );
}
/**
*字体名的选择
*/
public void fontType(){
String fontType=(String)cbFontName.getSelectedItem();
Font fontTmp = textArea.getFont();
int fbi = fontTmp.getStyle();
int fsize = fontTmp.getSize();
textArea.setFont( new Font( fontType, fbi, fsize ) );
}
/**
* 工具栏上的下划线
*/
public void toolXia(){
//Font f=new Font("下划线",tab.UNDERLINE_LOW_DASHED,12);
//textArea.setFont(f);
}
/**
* 编辑栏上的剪切
*/
public void editCut(){
try
{
String str=textArea.getSelectedText();
if(str.length()!=0)
{
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
this.textArea.replaceRange("",this.textArea.getSelectionStart(),this.textArea.getSelectionEnd());
isSaved=false;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"剪切文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的复制
*/
public void editCopy(){
try
{
String str=this.textArea.getSelectedText();
if(str.length()!=0)
{
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"复制文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的粘贴
*/
public void editPaste(){
try {
Transferable tr=cb.getContents(this);
if(tr!=null) {
String s=(String)tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null) {
textArea.replaceRange(s,textArea.getSelectionStart(),textArea.getSelectionEnd());
}
isSaved=false;
}
}
catch(Exception err) {
JOptionPane.showMessageDialog(this," 粘贴文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的删除
*/
public void editDelete(){
textArea.replaceRange(" ",textArea.getSelectionStart(),textArea.getSelectionEnd());
isSaved=false;
}
/**
* 编辑栏上的全选
*/
public void editSelectAll(){
textArea.setSelectionStart(0);
textArea.setSelectionEnd(this.textArea.getText().length());
}
public void helpHelp(){
JOptionPane.showMessageDialog(this,"这是一个很简单的记事本/n作者:sljLiuan/n版本:V 1.0/n2007年4月 ","记事本",JOptionPane.INFORMATION_MESSAGE);
}
/**
* 以下几个为弹出菜单
*/
public void popupCut(){
editCut();
}
public void popupCopy(){
editCopy();
}
public void popupPaste(){
editPaste();
}
public void popupDelete(){
editDelete();
}
/**
* 颜色栏上的颜色编辑
*/
public void colorChooser(){
Color bcolor=textArea.getForeground();
JColorChooser jColor=new JColorChooser();
jColor.setColor(bcolor);
textArea.setForeground(JColorChooser.showDialog(textArea,"选择颜色",bcolor));
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange()==ItemEvent.SELECTED) {
/**
* 当用户的选择改变时,则在JLabel上会显示出Swing目前字形大小信息.
*/
//int fontsizea=0;
try{
//fontsizea=Integer.parseInt((String)e.getItem());
textArea.setFont(cbFontSize.getFont());
}catch(NumberFormatException ne){
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.String;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.awt.GraphicsEnvironment;
//import javax.accessibility.*;
//import javax.swing.JToolTip;
import java.awt.Font;
//import javax.swing.text.StyleConstants;
import java.awt.event.ItemListener;
//import java.text.AttributedCharacterIterator.Attribute;
//import java.util.*;
//import java.text.*;
class WenBen extends JFrame implements ActionListener, ItemListener {
//文件菜单的新建、打开、保存、另存、退出
//编辑菜单的剪切、复制、粘贴、撤消、删除、全选
//格式菜单字体,帮助菜单
private static final long serialVersionUID = 1L;
JMenuItem miFileNew=null;
JMenuItem miFileOpen=null;
JMenuItem miFileSave=null;
JMenuItem miFileSaveAs=null;
JMenuItem miFileExit=null;
JMenuItem miEditCut=null;
JMenuItem miEditCopy=null;
JMenuItem miEditPaste=null;
JMenuItem miEditUndo=null;
JMenuItem miEditDelete=null;
JMenuItem miEditSeleteAll=null;
JMenuItem menuItemFont=null;
JMenuItem menuItemHelp=null;
//弹出式菜单的撤消、剪切、复制、粘贴、删除
JMenuItem popupUndo=null;
JMenuItem popupCut=null;
JMenuItem popupCopy=null;
JMenuItem popupPaste=null;
JMenuItem popupDelete=null;
//工具栏上的按钮
JButton btnNew=null;
JButton btnOpen=null;
JButton btnSave=null;
JButton btnBold=null;
JButton btnItalic=null;
JButton btnXiahuaxian=null;
JCheckBox cbItalic = null; //斜体复选框
JCheckBox cbBold = null; //粗体复选框
/**
* 文本框
*/
JTextArea textArea=null;
/**
* 弹出式菜单
*/
JPopupMenu popupMenu=null;
JMenuItem menuItemColor=null;
String fileName=null; //文件名
boolean isSaved=false; //文件是否已保存标志
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
JLabel label=null;
JMenuBar menuBar=new JMenuBar();
//字体名
String fontName[];
//fontSize 字号
String [] fontSize = new String[ 63 ];
JComboBox cbFontName=null;
JComboBox cbFontSize=null;
String fontNameStyle = "宋体";
int boldStyle = 0;
int italicStyle = 0;
int fontSizeStyle = 10;
Font font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
//构造函数
public WenBen() {
super("一个简单的记事本");
GraphicsEnvironment age = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = age.getAvailableFontFamilyNames();
for( int i = 0; i < fontSize.length; i++ ){
fontSize[ i ] = Integer.toString( ( i + 10 ) );
}
buildFileMenu();
buildEditMenu();
buildGeshiMenu();
buildColorMenu();
buildHelpMenu();
buildText();
buildPopupMenu();
buildToolBar();
InitListener();
this.pack();
//this.show();
this.setVisible( true );
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
/**
* 文件菜单的创建
*/
public void buildFileMenu() {
JMenu menu=new JMenu("文件");
menu.setMnemonic('F');
miFileNew=new JMenuItem("新建");
miFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
miFileOpen=new JMenuItem("打开");
miFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));
miFileSave=new JMenuItem("保存");
miFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK));
miFileSaveAs=new JMenuItem("另存为...");
miFileExit=new JMenuItem("退出");
miFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));
menu.add(miFileNew);
menu.add(miFileOpen);
menu.add(miFileSave);
menu.add(miFileSaveAs);
menu.addSeparator();
menu.add(miFileExit);
menuBar.add(menu);
this.setJMenuBar(menuBar);
}
/**
* 编辑菜单的创建
*/
public void buildEditMenu() {
JMenu menu=new JMenu("编辑");
menu.setMnemonic('E');
miEditCut=new JMenuItem("剪切");
miEditCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK));
miEditCopy=new JMenuItem("复制");
miEditCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK));
miEditPaste=new JMenuItem("粘贴");
miEditPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK));
miEditUndo=new JMenuItem("撤消");
miEditUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK));
miEditDelete=new JMenuItem("删除");
miEditDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.CTRL_MASK));
miEditSeleteAll=new JMenuItem("全选");
miEditSeleteAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK));
menu.add(miEditUndo);
menu.addSeparator();
menu.add(miEditCut);
menu.add(miEditCopy);
menu.add(miEditPaste);
menu.addSeparator();
menu.add(miEditDelete);
menu.add(miEditSeleteAll);
menuBar.add(menu);
this.setJMenuBar(menuBar);
}
/**
* 格式菜单的创建
*/
public void buildGeshiMenu() {
JMenu geshi=new JMenu("格式");
menuItemFont=new JMenuItem("字体...");
geshi.add(menuItemFont);
menuBar.add(geshi);
this.setJMenuBar(menuBar);
}
/**
* 颜色菜单的创建
*/
public void buildColorMenu() {
JMenu color=new JMenu("颜色");
menuItemColor=new JMenuItem("编辑颜色...");
color.add(menuItemColor);
menuBar.add(color);
this.setJMenuBar(menuBar);
}
/**
* 帮助菜单的创建
*/
public void buildHelpMenu() {
JMenu help= new JMenu("帮助");
menuItemHelp =new JMenuItem("帮助主题");
help.add(menuItemHelp);
menuBar.add(help);
this.setJMenuBar(menuBar);
}
/**
* 创建弹出式菜单
*/
public void buildPopupMenu()
{
popupMenu=new JPopupMenu();
popupMenu.setPopupSize(new Dimension(100,150));
popupUndo=new JMenuItem("撤消");
popupUndo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.ALT_MASK));
popupCut=new JMenuItem("剪切");
popupCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.ALT_MASK));
popupCopy=new JMenuItem("复制");
popupCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.ALT_MASK));
popupPaste=new JMenuItem("粘贴");
popupPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.ALT_MASK));
popupDelete=new JMenuItem ("删除");
popupDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.ALT_MASK));
popupMenu.add(popupUndo);
popupMenu.addSeparator();
popupMenu.add(popupCut);
popupMenu.add(popupCopy);
popupMenu.add(popupPaste);
popupMenu.addSeparator();
popupMenu.add(popupDelete);
textArea.add(popupMenu);
textArea.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(popupMenu.isPopupTrigger(e))
popupMenu.show(textArea,e.getX(),e.getY());
}
});
}
public void buildText() {
textArea=new JTextArea(20,40);
JScrollPane scrollPanel=new JScrollPane(textArea);
this.getContentPane().add(scrollPanel);
label=new JLabel("状态栏");
label.setToolTipText((String)label.getName());
this.getContentPane().add(scrollPanel,"Center");
this.getContentPane().add(label,"South");
}
/**
* 工具栏的创建
*/
public void buildToolBar() {
JToolBar tool=new JToolBar();
tool.setFloatable(true);
btnNew=new JButton("新建",new ImageIcon("C://windows//Coffee.gif"));
btnNew.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
//btnNew.setToolTipText((String)btnNew.TOOL_TIP_TEXT_KEY.valueOf("新建文件"));
btnNew.setToolTipText( "新建文件" );
}
});
btnOpen=new JButton("打开",new ImageIcon("C://windows//Coffee.gif"));
btnOpen.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
//btnOpen.setToolTipText((String)btnOpen.TOOL_TIP_TEXT_KEY.valueOf("打开"));
btnOpen.setToolTipText( "打开" );
}
});
btnSave=new JButton("保存",new ImageIcon("C://windows//Coffee.gif"));
btnSave.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnSave.setToolTipText((String)btnSave.TOOL_TIP_TEXT_KEY.valueOf("保存"));
btnSave.setToolTipText( "保存" );
}
});
btnBold=new JButton("B");
btnBold.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnBold.setToolTipText((String)btnBold.TOOL_TIP_TEXT_KEY.valueOf("粗体"));
btnBold.setToolTipText( "粗体" );
}
});
btnItalic=new JButton("斜体");
btnItalic.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
//btnItalic.setToolTipText((String)btnItalic.TOOL_TIP_TEXT_KEY.valueOf("斜体"));
btnItalic.setToolTipText( "斜体" );
}
});
btnXiahuaxian=new JButton("下划线");
btnXiahuaxian.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
//btnXiahuaxian.setToolTipText((String)btnXiahuaxian.TOOL_TIP_TEXT_KEY.valueOf("下划线"));
btnXiahuaxian.setToolTipText( "下划线" );
}
});
cbBold = new JCheckBox("粗体");
cbBold.addItemListener( new ItemListener( ){
public void itemStateChanged(ItemEvent e){
//实现监听选择粗体状态改变的方法
Font fontTmp = textArea.getFont();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
if( fontTmp.isBold() ){
font = fontTmp.deriveFont( fi );
}
else{
font = fontTmp.deriveFont( fi + Font.BOLD );
}
textArea.setFont( font );
}
} );
cbItalic = new JCheckBox("斜体");
cbItalic.addItemListener( new ItemListener( ){
public void itemStateChanged(ItemEvent e){
//实现监听选择粗体状态改变的方法
Font fontTmp = textArea.getFont();
int fb = fontTmp.isBold() ? Font.BOLD : 0;
if( fontTmp.isItalic() ){
font = fontTmp.deriveFont( fb );
}
else{
font = fontTmp.deriveFont( fb + Font.ITALIC );
}
textArea.setFont( font );
}
} );
JLabel label=new JLabel("字体类型:");
JLabel labe2=new JLabel("字体大小:");
cbFontSize=new JComboBox( fontSize );
cbFontSize.setEditable(true);
cbFontSize.addItemListener( new ItemListener(){
public void itemStateChanged( ItemEvent e ){
int sizeF = Integer.parseInt( (String)cbFontSize.getSelectedItem() );
font = new Font( fontNameStyle,boldStyle+italicStyle,sizeF );
textArea.setFont( font );
}
});
cbFontName=new JComboBox( fontName );
cbFontName.setEditable( true );
tool.add(btnNew);
tool.add(btnOpen);
tool.add(btnSave);
tool.addSeparator();
tool.add(btnBold);
tool.add(btnItalic);
tool.add(btnXiahuaxian);
tool.add( cbItalic );
tool.add( cbBold );
tool.addSeparator();
tool.add(labe2);
tool.add(cbFontSize);
tool.addSeparator();
tool.add(label);
tool.add(cbFontName);
add( tool, BorderLayout.NORTH);
}
/**
* 事件初始化
*/
public void InitListener(){
/**
* 文件事件
*/
miFileNew.addActionListener(this);
miFileOpen.addActionListener(this);
miFileSave.addActionListener(this);
miFileSaveAs.addActionListener(this);
miFileExit.addActionListener(this);
/**
* 工具栏上的新建,打开,保存事件
*/
btnNew.addActionListener(this);
btnOpen.addActionListener(this);
btnSave.addActionListener(this);
btnBold.addActionListener(this);
btnItalic.addActionListener(this);
btnXiahuaxian.addActionListener(this);
/**
* 编辑菜单上的事件
*/
miEditCut.addActionListener(this);
miEditCopy.addActionListener(this);
miEditPaste.addActionListener(this);
miEditDelete.addActionListener(this);
miEditSeleteAll.addActionListener(this);
menuItemFont.addActionListener(this);
/**
* 颜色菜单上的事件
*/
menuItemColor.addActionListener(this);
menuItemHelp.addActionListener(this);
/**
* 弹出菜单的事件
*/
popupCopy.addActionListener(this);
popupCut.addActionListener(this);
popupPaste.addActionListener(this);
popupDelete.addActionListener(this);
popupUndo.addActionListener(this);
cbFontName.addActionListener( this );
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==miFileNew)
fileNew();
if(e.getSource()==miFileOpen)
fileOpen();
if(e.getSource()==miFileSave)
fileSave();
if(e.getSource()==miFileSaveAs)
fileSaveAs();
if(e.getSource()==miFileExit)
fileQuit();
/**
* 以下为工具栏菜单上的按钮
*/
if(e.getSource()==btnNew)
toolNew();
if(e.getSource()==btnOpen)
toolOpen();
if(e.getSource()==btnSave)
toolSave();
if(e.getSource()==btnBold)
toolBold();
if(e.getSource()==btnItalic)
toolXieti();
if(e.getSource()==btnXiahuaxian)
toolXia();
/**
* 以下为编辑上的菜单
*/
if(e.getSource()==miEditCut)
editCut();
if(e.getSource()==miEditCopy)
editCopy();
if(e.getSource()==miEditPaste)
editPaste();
if(e.getSource()==miEditDelete)
editDelete();
if(e.getSource()==miEditSeleteAll)
editSelectAll();
if(e.getSource()==cbFontSize)
fontSelect();
if(e.getSource()==cbFontName)
fontType();
if(e.getSource()==menuItemHelp)
helpHelp();
if(e.getSource()==menuItemColor)
colorChooser();
if(e.getSource()==popupCopy)
popupCopy();
if(e.getSource()==popupCut)
popupCut();
if(e.getSource()==popupPaste)
popupPaste();
if(e.getSource()==popupDelete)
popupDelete();
}
/**
* 文件新建事件
*/
public void fileNew() {
if(isSaved) {
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("记事本");
}
else {
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_OPTION);//保存选择按钮
if(JOptionPane.YES_OPTION==result) {
fileSaveAs();
}
else if(JOptionPane.NO_OPTION==result) {
this.textArea.setText("");
this.textArea.setFocusable(true);
this.setTitle("记事本");
}
else{
}
}
}
/**
* 文件打开事件
*/
public void fileOpen() {
String openFileName="";
JFileChooser jFileChooser=new JFileChooser();
if(isSaved) {
try
{
if(JFileChooser.APPROVE_OPTION==jFileChooser.showOpenDialog(this)){
openFileName=jFileChooser.getSelectedFile().getPath();
File file=new File(openFileName);
int flength=(int)file.length();
int num=0;
FileReader fReader=new FileReader(file);
char[]data=new char[flength];
while(fReader.ready()){
num+=fReader.read(data,num,flength-num);
}
fReader.close();
textArea.setText(new String(data,0,num));
fileName=openFileName;
this.setTitle(fileName.substring(fileName.lastIndexOf("//")));
this.repaint();
isSaved=true;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"打开文件出错","错误",JOptionPane.ERROR_MESSAGE);
}
}
else {
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.YES_OPTION==result) {
fileSave();
fileOpen();
}
else if(JOptionPane.NO_OPTION==result) {
isSaved=true;
fileOpen();
}
else {}
}
}
/**
* 添加文件保存事件
*/
public void fileSave()
{
if(fileName==null)
{
fileSaveAs();
}
else
{
if(!isSaved)
{
try
{
File saveFile=new File(fileName);
FileWriter fw=new FileWriter(saveFile);
fw.write(textArea.getText());
fw.close();
isSaved=true;
this.setTitle(fileName.substring(fileName.lastIndexOf("//")));
this.repaint();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"保存文件出错","错误",JOptionPane.ERROR);
}
}
else
{
fileSaveAs();
}
}
}
/**
* 添加另存为事件
*/
public void fileSaveAs() {
JFileChooser jFileChooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==jFileChooser.showSaveDialog(this)) {
fileName=jFileChooser.getSelectedFile().getPath();
fileSave();
}
}
/**
* 当文件没有保存时,要提醒用户你的文件没有保存,
*/
public void fileQuit(){
if(!isSaved){
int result=JOptionPane.showConfirmDialog(this,"想保存文件么","记事本",JOptionPane.YES_NO_OPTION);
if(JOptionPane.YES_OPTION==result)
fileSave();
else if(JOptionPane.NO_OPTION==result)
System.exit(0);
/**
* 良好的编程让if和else匹配
*/
else {}
}
else {
System.exit(0);
}
}
/**
* 工具栏上的新建
*/
public void toolNew(){
fileNew();
}
/**
* 工具栏上的打开
*/
public void toolOpen(){
fileOpen();
}
/**
* 工具栏上的保存
*/
public void toolSave(){
fileSave();
}
/**
* 工具栏上的斜体
*/
public void toolXieti(){
Font fontTmp = textArea.getFont();
int fb = fontTmp.isBold() ? Font.BOLD : 0;
if( fontTmp.isItalic() ){
font = fontTmp.deriveFont( fb );
}
else{
font = fontTmp.deriveFont( fb + Font.ITALIC );
}
textArea.setFont( font );
}
/**
* 工具栏上的粗体
*/
public void toolBold(){
/*Font fontTmp = textArea.getFont();
String fn = fontTmp.getFontName();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
int fs = fontTmp.getSize();
if ( fontTmp.isBold() ){
textArea.setFont( new Font( fn, fi, fs ) );
}
else{
textArea.setFont( new Font( fn, fi + Font.BOLD, fs ) );
}
*/
Font fontTmp = textArea.getFont();
int fi = fontTmp.isItalic() ? Font.ITALIC : 0;
if( fontTmp.isBold() ){
font = fontTmp.deriveFont( fi );
}
else{
font = fontTmp.deriveFont( fi + Font.BOLD );
}
textArea.setFont( font );
}
/**
*字号选择
*/
public void fontSelect(){
float fontSize=Float.parseFloat( ( String )cbFontSize.getSelectedItem() );
Font fontTmp = textArea.getFont();
font = fontTmp.deriveFont( fontSize );
textArea.setFont( font );
}
/**
*字体名的选择
*/
public void fontType(){
String fontType=(String)cbFontName.getSelectedItem();
Font fontTmp = textArea.getFont();
int fbi = fontTmp.getStyle();
int fsize = fontTmp.getSize();
textArea.setFont( new Font( fontType, fbi, fsize ) );
}
/**
* 工具栏上的下划线
*/
public void toolXia(){
//Font f=new Font("下划线",tab.UNDERLINE_LOW_DASHED,12);
//textArea.setFont(f);
}
/**
* 编辑栏上的剪切
*/
public void editCut(){
try
{
String str=textArea.getSelectedText();
if(str.length()!=0)
{
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
this.textArea.replaceRange("",this.textArea.getSelectionStart(),this.textArea.getSelectionEnd());
isSaved=false;
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"剪切文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的复制
*/
public void editCopy(){
try
{
String str=this.textArea.getSelectedText();
if(str.length()!=0)
{
StringSelection s=new StringSelection(str);
cb.setContents(s,s);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"复制文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的粘贴
*/
public void editPaste(){
try {
Transferable tr=cb.getContents(this);
if(tr!=null) {
String s=(String)tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null) {
textArea.replaceRange(s,textArea.getSelectionStart(),textArea.getSelectionEnd());
}
isSaved=false;
}
}
catch(Exception err) {
JOptionPane.showMessageDialog(this," 粘贴文件出错","错误",JOptionPane.ERROR);
}
}
/**
* 编辑栏上的删除
*/
public void editDelete(){
textArea.replaceRange(" ",textArea.getSelectionStart(),textArea.getSelectionEnd());
isSaved=false;
}
/**
* 编辑栏上的全选
*/
public void editSelectAll(){
textArea.setSelectionStart(0);
textArea.setSelectionEnd(this.textArea.getText().length());
}
public void helpHelp(){
JOptionPane.showMessageDialog(this,"这是一个很简单的记事本/n作者:sljLiuan/n版本:V 1.0/n2007年4月 ","记事本",JOptionPane.INFORMATION_MESSAGE);
}
/**
* 以下几个为弹出菜单
*/
public void popupCut(){
editCut();
}
public void popupCopy(){
editCopy();
}
public void popupPaste(){
editPaste();
}
public void popupDelete(){
editDelete();
}
/**
* 颜色栏上的颜色编辑
*/
public void colorChooser(){
Color bcolor=textArea.getForeground();
JColorChooser jColor=new JColorChooser();
jColor.setColor(bcolor);
textArea.setForeground(JColorChooser.showDialog(textArea,"选择颜色",bcolor));
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange()==ItemEvent.SELECTED) {
/**
* 当用户的选择改变时,则在JLabel上会显示出Swing目前字形大小信息.
*/
//int fontsizea=0;
try{
//fontsizea=Integer.parseInt((String)e.getItem());
textArea.setFont(cbFontSize.getFont());
}catch(NumberFormatException ne){
}
}
}
}