7. JAVA记事本程序怎么写 这是一个我以前写的简单的记事本,里面有新建,保存,另存,打开等功能,但是只是逻辑最简单的那种,你看看吧,希望对你有帮助; import java.awt.FileDialog; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class NotePad { public static void main(String[] args) { NotePadFrame notPadFrame = new NotePadFrame(); notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); notPadFrame.setVisible(true); } } class NotePadFrame extends JFrame { private JMenu jmb, jmb1; private JMenuBar Jmenu = new JMenuBar(); private JMenuItem fm, fm1, fm2, fm3, fm4, fe1, fe2, fe3, fe4; String fileName, copy, paste, cut; NotePadPanel notePadPanel = new NotePadPanel(this); private NotePadFrame f; public NotePadFrame() { jmb = new JMenu("文件"); this.setJMenuBar(Jmenu); fm = new JMenuItem("新建"); jmb.add(fm); jmb.addSeparator(); // 新建 fm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (e.getSource() == fm) { if (!(notePadPanel.getTa().getText()).equals("")) { Object[] options = ; int response = JOptionPane.showOptionDialog(null,"你是否保存","提示",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (response == 0) { FileDialog d = new FileDialog(f,"保存文件",FileDialog.SAVE); d.setVisible(true); fileName = d.getDirectory() + d.getFile(); FileOutputStream fout = new FileOutputStream( fileName + ".txt"); byte[] bb = notePadPanel.getTa().getText() .getBytes(); fout.write(bb); // 关闭 fout.close(); JOptionPane.showMessageDialog(null,"已保存"); notePadPanel.getTa().setText(""); } if (response == 1) { JOptionPane.showMessageDialog(null,"你选择了取消"); notePadPanel.getTa().setText(""); } } } } catch (Exception e2) { System.out.println(e2.getMessage()); } } }); fm1 = new JMenuItem("打开"); jmb.add(fm1); jmb.addSeparator(); // 打开文件 fm1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (e.getSource() == fm1) { FileDialog d = new FileDialog(f,"打开文件",FileDialog.LOAD); d.setVisible(true); File file = new File(d.getDirectory() + d.getFile()); for (int i = 0; i <= file.length(); i++) { char[] ch = new char[1024]; FileReader fr = new FileReader(file); fr.read(ch); String str = new String(ch); notePadPanel.getTa().setText(str); } } } catch (IOException e3) { System.out.println(e3.getMessage()); } } }); fm2 = new JMenuItem("保存"); jmb.add(fm2); jmb.addSeparator(); // 保存文件 fm2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (e.getSource() == fm2) { if (fileName == null) { fileName = JOptionPane.showInputDialog("请输入文件名","java"); FileOutputStream fout = new FileOutputStream( fileName + ".txt"); byte[] bb = notePadPanel.getTa().getText() .getBytes(); fout.write(bb); // 关闭 fout.close(); JOptionPane.showMessageDialog(null,"已保存"); } else { FileOutputStream fout = new FileOutputStream( fileName + ".txt"); byte[] bb = notePadPanel.getTa().getText() .getBytes(); fout.write(bb); // 关闭 fout.close(); JOptionPane.showMessageDialog(null,"已保存"); } } } catch (IOException e1) { System.out.println(e1.getMessage()); } } }); fm3 = new JMenuItem("另存为"); jmb.add(fm3); jmb.addSeparator(); // 另存为 fm3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == fm3) { try { FileDialog d = new FileDialog(f,"另存为",FileDialog.SAVE); d.setVisible(true); fileName = d.getDirectory() + d.getFile(); FileOutputStream fout = new FileOutputStream(fileName + ".txt"); byte[] bb = notePadPanel.getTa().getText().getBytes(); fout.write(bb); // 关闭 fout.close(); } catch (Exception e4) { System.out.println(e4.getMessage()); } } } }); fm4 = new JMenuItem("关闭"); jmb.add(fm4); fm4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == fm4) { System.exit(0); } } }); jmb1 = new JMenu("编辑");。
- 简历中写作怎么写
- 良好拼音怎么写
- 李欣英文怎么写
- 雅诗兰黛抗蓝光眼霜什么时候使用 雅诗兰黛抗蓝光眼霜怎么辨别真假
- 迪奥眼线笔怎么用法 迪奥眼线笔怎么用法
- 褴拼音怎么写
- 店里处理怎么写
- 衫字繁体字怎么写
- 脚步英文怎么写
- 描字的行书怎么写