import java.awt.BorderLayout; import java.awt.FileDialog; import java.awt.event.*; import java.io.*; import javax.swing.*; import gongjubao.wenjiangongju; class jishibenmianban extends JFrame implements ActionListener { JMenuBar jmb; JMenu jm1,jm2,jm3,jm4,jm5; JTextArea jta; jishibenmianban() { String[]menu1={"新建","打开","保存","退出"}; String[]menu2={"剪切","复制","贴贴","删除","全选"}; String[]menu3={"颜色","自动换行"}; String[]menu4={"时间/日期"}; String[]menu5={"关于记事本"}; jmb=new JMenuBar(); jm1=new JMenu("文件"); jm2=new JMenu("编辑"); jm3=new JMenu("格式"); jm4=new JMenu("查看"); jm5=new JMenu("帮助"); jta=new JTextArea(20,60); for(String du1:menu1) { JMenuItem jmix=new JMenuItem(du1); jm1.add(jmix); jmix.addActionListener(this); } for(String du2:menu2) { JMenuItem jmix=new JMenuItem(du2); jm2.add(jmix); jmix.addActionListener(this); } for(String du3:menu3) { JMenuItem jmix=new JMenuItem(du3); jm3.add(jmix); jmix.addActionListener(this); } for(String du4:menu4) { JMenuItem jmix=new JMenuItem(du4); jm4.add(jmix); jmix.addActionListener(this); } for(String du5:menu5) { JMenuItem jmix=new JMenuItem(du5); jm5.add(jmix); jmix.addActionListener(this); } jmb.add(jm1); jmb.add(jm2); jmb.add(jm3); jmb.add(jm4); jmb.add(jm5); setJMenuBar(jmb); add(jta); setSize(800,600); setVisible(true); setLocation(600,200); setTitle("无标题"); } public void actionPerformed(ActionEvent e) { String dedao=e.getActionCommand(); switch(dedao) { case "新建": setTitle("无标题"); jta.setText(null); break; case "打开": dakai(); break; case "保存": baocun(); break; case "退出": System.exit(0); break; case "剪切": jta.cut(); break; case "复制": jta.copy(); break; case "贴贴": jta.paste(); break; case "删除": jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd()); break; case "全选": jta.selectAll(); break; case "颜色": jta.setForeground(JColorChooser.showDialog(this, "颜色", null)); break; case "时间/日期": String time=wenjiangongju.shijian(); jta.append(time); break; case "关于记事本": tanchuang("记事本,为银河版权所有,电话0728-0000000"); break; } } public void dakai() { FileDialog fd=new FileDialog(this,"打开",FileDialog.LOAD); fd.setVisible(true); String path=fd.getDirectory(); String wenjianming=fd.getFile(); if(fd.getDirectory()==null||fd.getFile()==null) { this.setTitle("无标题"); } else{ String xianshi=wenjiangongju.duqu(path, wenjianming); jta.setText(xianshi); this.setTitle(path+wenjianming); tanchuang("文件打开成功!"); } } public void baocun() { if(this.getTitle().equals("无标题")) { FileDialog fd=new FileDialog(this,"保存",FileDialog.SAVE); fd.setVisible(true); String path=fd.getDirectory(); String wenjianming=fd.getFile(); wenjiangongju.baocun(path,wenjianming,jta.getText()); tanchuang("文件保存成功!"); } else { wenjiangongju.baocun(this.getTitle(),jta.getText()); tanchuang("文件保存成功!"); } } public void tanchuang(String neirong) { JOptionPane.showMessageDialog(this, neirong); } } public class jishiben { public static void main(String[] args) { // TODO Auto-generated method stub new jishibenmianban(); } }