import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.io.*;
import java.util.*;
public class myEditor extends JFrame
{
JTextArea textarea=new JTextArea();
JScrollPane roll=new JScrollPane();
JOptionPane dialogwindow=new JOptionPane();
JToolBar toolbar=new JToolBar();
JMenuBar menubar=new JMenuBar();
//定义工具条上的按钮*****************************
JButton newbutton=new JButton(new ImageIcon("images/new.gif"));
JButton openbutton=new JButton(new ImageIcon("images/open.gif"));
JButton savebutton=new JButton(new ImageIcon("images/save.gif"));
JButton saveasbutton=new JButton(new ImageIcon("images/saveas.gif"));
JButton cutbutton=new JButton(new ImageIcon("images/cut.gif"));
JButton copybutton=new JButton(new ImageIcon("images/copy.gif"));
JButton pastebutton=new JButton(new ImageIcon("images/paste.gif"));
JButton undobutton=new JButton(new ImageIcon("images/undo.gif"));
JButton findbutton=new JButton(new ImageIcon("images/find.gif"));
JButton exitbutton=new JButton(new ImageIcon("images/close.gif"));
//定义文件菜单***********************************
JMenu file=new JMenu("文件");
JMenuItem newfile=new JMenuItem("新建");
JMenuItem openfile=new JMenuItem("打开");
JMenuItem savefile=new JMenuItem("保存");
JMenuItem saveasfile=new JMenuItem("另存为");
JMenuItem print=new JMenuItem("打印");
JMenuItem printpreview=new JMenuItem("打印预览");
JMenuItem quit=new JMenuItem("退出");
//***********************************************
//定义编辑菜单***********************************
JMenu edit=new JMenu("编辑");
JMenuItem textcopy=new JMenuItem("复制");
JMenuItem textcut =new JMenuItem("剪切");
JMenuItem textpaste=new JMenuItem("粘贴");
JMenuItem textdelete=new JMenuItem("删除");
//***********************************************
//定义格式菜单***********************************
JMenu format=new JMenu("格式");
JMenuItem checkall=new JMenuItem("全选");
JMenu setting=new JMenu("设置");
JMenuItem setfont=new JMenuItem("字体");
JMenuItem setcolor=new JMenuItem("颜色");
JMenuItem timeanddate=new JMenuItem("时间/日期");
JCheckBoxMenuItem autonewline=new JCheckBoxMenuItem("自动换行");
//***********************************************
//定义窗口样式菜单*******************************
ButtonGroup group=new ButtonGroup();
JMenu windowface=new JMenu("窗口外观");
JRadioButtonMenuItem javaface=new JRadioButtonMenuItem("Java外观感觉");
JRadioButtonMenuItem motifface=new JRadioButtonMenuItem("Motif外观感觉");
JRadioButtonMenuItem windowsface=new JRadioButtonMenuItem("Windows外观感觉");
//***********************************************
//定义帮助菜单***********************************
JMenu help=new JMenu("帮助");
JMenuItem helpmotif=new JMenuItem("帮助主题");
JMenuItem about=new JMenuItem("关天...");
//***********************************************
public myEditor()
{
javastyle();
Container c=this.getContentPane();
roll.getViewport().add(textarea);
c.add(roll,BorderLayout.CENTER);
c.add(toolbar,BorderLayout.NORTH);
//添加按钮到工具条***************************
toolbar.add(newbutton);
toolbar.addSeparator();
toolbar.add(openbutton);
toolbar.add(savebutton);
toolbar.add(saveasbutton);
toolbar.addSeparator();
toolbar.add(cutbutton);
toolbar.add(copybutton);
toolbar.add(pastebutton);
toolbar.addSeparator();
toolbar.add(undobutton);
toolbar.addSeparator();
toolbar.add(findbutton);
toolbar.add(exitbutton);
//*******************************************
//添加子菜单到文件菜单项*********************
file.add(newfile);
file.add(openfile);
file.addSeparator();
file.add(savefile);
file.add(saveasfile);
file.addSeparator();
file.add(print);
file.add(printpreview);
file.addSeparator();
file.add(quit);
//*******************************************
edit.add(textcopy);
edit.add(textcut);
edit.add(textpaste);
edit.add(textdelete);
setting.add(setfont);
setting.add(setcolor);
format.add(checkall);
format.add(setting);
format.add(timeanddate);
format.addSeparator();
format.add(autonewline);
//将单选菜单按钮定义为一个组*******************
group.add(javaface);
group.add(motifface);
group.add(windowsface);
//*********************************************
windowface.add(javaface);
javaface.setSelected(true);
javaface.setMnemonic(KeyEvent.VK_J);
windowface.add(motifface);
windowface.add(windowsface);
help.add(helpmotif);
help.addSeparator();
help.add(about);
menubar.add(file);
menubar.add(edit);
menubar.add(format);
menubar.add(windowface);
menubar.add(help);
this.setJMenuBar(menubar);
//给文件菜单下的退出子菜单添加活动帧听************************
File_close_actionAdapter close=new File_close_actionAdapter();
quit.addActionListener(close);
//给窗口外观菜单下的javaface子菜单添加活帖听
javaface_actionAdapter jface=new javaface_actionAdapter();
javaface.addActionListener(jface);
motifface_actionAdapter mface=new motifface_actionAdapter();
motifface.addActionListener(mface);
windowsface_actionAdapter wface=new windowsface_actionAdapter();
windowsface.addActionListener(wface);
//************************************************************
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
Fileclose();
}
});
}
public static void main(String args[])
{
myEditor mainFrame=new myEditor();
//对主窗口的一些设置*************
mainFrame.setTitle("莫名记事本");
mainFrame.setBounds(200,150,600,450);
mainFrame.setVisible(true);
//*******************************
}
//定义窗口面板显示风格是windows风格********
void javastyle()
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception el)
{
el.printStackTrace();
}
}
//定义退出此程序的方法***************
void Fileclose()
{
if(textarea.getText().equals(""))
{
dispose();
System.exit(0);
}
else
{
if(dialogwindow.OK_OPTION==dialogwindow.showConfirmDialog(myEditor.this,
"退出此程序之前,请检查文件是否已经保存!!!","退出程序",
dialogwindow.OK_CANCEL_OPTION,dialogwindow.WARNING_MESSAGE))
{
dispose();
System.exit(0);
}
}
}
//***********************************
//定义退出程序的方法*********************************
public void File_close_actionPerformed(ActionEvent e)
{
Fileclose();
}
//***************************************************
//定义窗口为java面板风格的方法***********************
public void javaface_actionPerformed(ActionEvent e)
{
if(javaface.isSelected())
{
javastyle();
}
}
//定义窗口为motif面板风格的方法
public void motifface_actionPerformed(ActionEvent e)
{
if(motifface.isSelected())
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
//定义窗口为windows面板风格的方法
public void windowsface_actionPerformed(ActionEvent e)
{
if(windowsface.isSelected())
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
//***************************************************
//定义退出程序的类***************************************
class File_close_actionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
File_close_actionPerformed;
}
}
//**********************************************************
//定义窗口外观菜单javaface子菜单的活动帧听类******************************
class javaface_actionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
javaface_actionPerformed;
}
}
//定义窗口外观菜单motifface子菜单的活动帧听类
class motifface_actionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
motifface_actionPerformed;
}
}
//定义窗口外观菜单windowsface子菜单的活动帧听类
class windowsface_actionAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
windowsface_actionPerformed;
}
}
}