2013年(92)
分类: 信息化
2013-03-04 14:43:23
[代码] [Java]代码 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class Nodepad extends JFrame { private JTextArea editor; private Container c; private Font f=new Font("sanserif",Font.PLAIN,12); //菜单栏成员特点声明 private JMenuBar mb; //菜单棒 private JMenu fileMenu; //文件菜单 private JMenu editMenu; //修改菜单 private JMenu formatMenu; //格局菜单 private JMenuItem fileMenuOpen,fileMenuSave,fileMenuExit; //文件菜单的菜单项 private JMenuItem editMenuCopy,editMenuCut,editMenuPaste; //修改菜单的菜单项 private JMenu formatSet;//格局菜单榜首级菜单 private JMenuItem lineWrap,cancleLineWrap,wrapStyleWord,cancleWrapStyleWord;//格局菜单的第二级菜单 //东西栏成员特点声明 private JToolBar toolBar; //东西棒 private JButton b1,b2,b3,b4,b5; //声明5个按钮,分别为:“翻开”、“保管”、“仿制”、“剪切”、“张贴” //弹出式菜单特点声明 private JPopupMenu pm; private JMenuItem item1,item2,item3,item4,item5; public Nodepad() { super("简略记事本"); setSize(400,300); try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e){ System.err.println("不能被设置外观的缘由:" e);} c=getContentPane(); //创立一个内容面板 editor = new JTextArea(); //创立一个文本区 c.add(new JScrollPane(editor)); //设置滚动条,并增加到内容面板 //菜单栏的完成 //文件菜单的完成 mb = new JMenuBar(); //创立菜单棒 fileMenu = new JMenu("文件(F)");//创立菜单 fileMenuOpen = new JMenuItem("翻开(O)...Ctrl O"); fileMenuSave = new JMenuItem("保管(S)...Ctrl S"); fileMenuExit = new JMenuItem("退出"); JMHandler JM=new JMHandler(); //创立监听器 fileMenuOpen.addActionListener(JM); //注册监听器 fileMenuSave.addActionListener(JM); fileMenuExit.addActionListener(JM); fileMenu.add(fileMenuOpen); fileMenu.add(fileMenuSave); fileMenu.addSeparator(); //增加分隔线 fileMenu.add(fileMenuExit); fileMenu.setFont(f); //设置菜单中文体的字体 //修改菜单的完成 editMenu = new JMenu("修改(E)"); editMenuCopy = new JMenuItem("仿制(C) Ctrl C"); editMenuCut = new JMenuItem("剪切(T) Ctrl X"); editMenuPaste = new JMenuItem("张贴(P) Ctrl v"); EMHandler EM=new EMHandler(); //创立监听器 editMenuCopy.addActionListener(EM);//注册监听器 editMenuCut.addActionListener(EM); editMenuPaste.addActionListener(EM); editMenu.add(editMenuCopy); editMenu.add(editMenuCut); editMenu.add(editMenuPaste); editMenu.setFont(f); //格局菜单的完成 formatMenu = new JMenu("格局(O)"); //创立菜单 formatSet = new JMenu("格局设置"); //二级菜单 lineWrap = new JMenuItem("主动换行"); //二级菜单项 cancleLineWrap = new JMenuItem("撤销主动换行"); wrapStyleWord = new JMenuItem("断行不断字"); cancleWrapStyleWord = new JMenuItem("撤销断行不断字"); FMHandler FM=new FMHandler(); //创立事情监听器 lineWrap.addActionListener(FM); //注册二级菜单项的监听器 cancleLineWrap.addActionListener(FM); wrapStyleWord.addActionListener(FM); cancleWrapStyleWord.addActionListener(FM); formatSet.add(lineWrap); formatSet.add(cancleLineWrap); formatSet.addSeparator(); //增加分隔线 formatSet.add(wrapStyleWord); formatSet.add(cancleWrapStyleWord); formatMenu.add(formatSet); formatMenu.setFont(f); //将菜单悉数增加菜单棒里 mb.add(fileMenu); mb.add(editMenu); mb.add(formatMenu); //东西栏的完成 //按钮分别为:“翻开”、“保管”、“仿制”、“剪切”、“张贴” toolBar =new JToolBar(); //创立东西棒 b1= new JButton(new ImageIcon("img/open.gif")); b2= new JButton(new ImageIcon("img/save.gif")); b3= new JButton(new ImageIcon("img/copy.gif")); b4= new JButton(new ImageIcon("img/cut.gif")); b5= new JButton(new ImageIcon("img/paste.gif")); TBHandler TB=new TBHandler(); //创立按钮监听器 b1.addActionListener(TB); b2.addActionListener(TB); b3.addActionListener(TB); b4.addActionListener(TB); b5.addActionListener(TB); //把按钮悉数增加到东西棒中 toolBar.add(b1); toolBar.add(b2); toolBar.add(b3); toolBar.add(b4); toolBar.add(b5); //弹出菜单的完成 pm =new JPopupMenu(); //创立弹出菜单 item1 = new JMenuItem("翻开"); item2 = new JMenuItem("保管"); item3 = new JMenuItem("仿制"); item4 = new JMenuItem("剪切"); item5 = new JMenuItem("张贴"); JPHandler JP=new JPHandler(); item1.addActionListener(JP); //注册菜单项的鼠标事情监听器 item2.addActionListener(JP); item3.addActionListener(JP); item4.addActionListener(JP); item5.addActionListener(JP); editor.addMouseListener(JP); //注册文本区的鼠标事情监听器 pm.add(item1); pm.add(item2); pm.add(item3); pm.add(item4); pm.add(item5); //把菜单栏、东西栏、弹出菜单增加到内容面板 setJMenuBar(mb); //显现菜单栏 c.add(toolBar,BorderLayout.NORTH); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /*自定义类完成文件菜单项的事情处置*/ private class JMHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==fileMenuOpen){ loadFile(); } else if(e.getSource()==fileMenuSave){ saveFile(); } else{System.exit(0);} } } public void loadFile() //翻开文件办法 { JFileChooser fc=new JFileChooser(); int r=fc.showOpenDialog(this); if(r==JFileChooser.APPROVE_OPTION) { File file=fc.getSelectedFile(); try{ editor.read(new FileReader(file),null);} catch(IOException e){} } } public void saveFile() /*保管文件的办法*/ { JFileChooser fc=new JFileChooser(); int r=fc.showSaveDialog(this); if(r==JFileChooser.APPROVE_OPTION) { File file=fc.getSelectedFile(); try{ editor.write(new FileWriter(file));} catch(IOException e){} } } //修改菜单菜单项的事情处置 private class EMHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==editMenuCopy) //完成仿制功用 { editor.copy(); editor.requestFocus(); } else if(e.getSource()==editMenuCut)//完成剪切功用 { editor.cut(); editor.requestFocus(); } else //完成张贴功用 { editor.paste(); editor.requestFocus(); } } } //格局菜单二级菜单的菜单项的事情处置 private class FMHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==lineWrap){ editor.setLineWrap(true); } else if(e.getSource()==cancleLineWrap) { editor.setLineWrap(false);} else if(e.getSource()==wrapStyleWord) { editor.setWrapStyleWord(true);} else{ editor.setWrapStyleWord(false); } } } //自定义类完成东西栏的按钮事情处置 private class TBHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==b1){ loadFile(); } //完成翻开文件功用 else if(e.getSource()==b2) {saveFile();} //完成保管文件功用 else if(e.getSource()==b3) //文件仿制 { editor.copy(); editor.requestFocus(); } else if(e.getSource()==b4) //文件剪切 { editor.cut(); editor.requestFocus(); } else //文件张贴 { editor.paste(); editor.requestFocus(); } } } //自定义类完成弹出式菜单的事情处置 private class JPHandler implements ActionListener,MouseListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==item1){ loadFile(); } //完成翻开文件功用 else if(e.getSource()==item2) {saveFile();} //完成保管文件功用 else if(e.getSource()==item3) //文件仿制 { editor.copy(); editor.requestFocus(); } else if(e.getSource()==item4) //文件剪切 { editor.cut(); editor.requestFocus(); } else //文件张贴 { editor.paste(); editor.requestFocus(); } } public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) //判别能否按下鼠标右键 pm.show(editor,e.getX(),e.getY()); //显现弹出式菜单 } public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){} } public static void main(String []args) { Nodepad N=new Nodepad(); } }