简易记事本
一:实验代码
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import static javax.swing.KeyStroke.*; public class pluto implements ActionListener{ JFrame frame; JMenuBar bar; JMenu fileMenu, editMenu,formatMenu,viewMenu,helpMenu; JMenuItem newItem, openItem, saveItem, closeItem; ImageIcon newIcon, openIcon, saveIcon, closeIcon; JScrollPane scroll; JTextArea area; JFileChooser chooser; File file; public pluto() { frame = new JFrame("ZY"); bar = new JMenuBar(); fileMenu = new JMenu("文件(F)"); editMenu = new JMenu("编辑(E)"); formatMenu= new JMenu("格式(O)"); viewMenu=new JMenu("查看(V)"); helpMenu= new JMenu("帮助(H)"); newIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "A.png"); openIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "B.png"); saveIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "C.png"); closeIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "D.png"); newItem = new JMenuItem("新建(N)", newIcon); openItem = new JMenuItem("打开(O)", openIcon); saveItem = new JMenuItem("另存为(A)", saveIcon); closeItem = new JMenuItem("关闭(X)", closeIcon); area = new JTextArea(); scroll = new JScrollPane(area); newItem.setMnemonic(KeyEvent.VK_N); openItem.setMnemonic(KeyEvent.VK_O); saveItem.setMnemonic(KeyEvent.VK_A); closeItem.setMnemonic(KeyEvent.VK_X); newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK)); closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.addSeparator(); fileMenu.add(closeItem); newItem.addActionListener((ActionListener) this); openItem.addActionListener((ActionListener) this); saveItem.addActionListener((ActionListener) this); closeItem.addActionListener((ActionListener) this); bar.add(fileMenu); bar.add(editMenu); bar.add(formatMenu); bar.add(viewMenu); bar.add(helpMenu); frame.setJMenuBar(bar); frame.add(scroll); frame.setVisible(true); frame.pack(); } public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); if (obj instanceof JMenuItem) { JMenuItem item = (JMenuItem) obj; //加入动作监听 if (item == newItem) { new pluto(); } else if(item==openItem){ JFileChooser chooser=new JFileChooser(); chooser.showOpenDialog(null); file=chooser.getSelectedFile();//返回选择的文件 try { FileInputStream fis=new FileInputStream(file); try { byte[] b=new byte[fis.available()]; fis.read(b); area.append(new String(b)); fis.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } else if (item==saveItem){ JFileChooser chooser = new JFileChooser(); chooser.showSaveDialog(null); file=chooser.getSelectedFile(); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); try { FileOutputStream fos=new FileOutputStream(file); byte[] b = area.getText().getBytes(); try { fos.write(b); } catch (IOException ex) { ex.printStackTrace(); } try { fos.close(); } catch (IOException ex) { ex.printStackTrace(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } } } } else if(item==closeItem){ System.exit(1); } } } }
测试:
public class crush {
public static void main(String[] args) {
// TODO Auto-generated method stub
new pluto();
// TODO Auto-generated method stub
new pluto();
}
}
截图:
学习总结:去找了别人的记事本瞅瞅,还是有很多不懂的地方,还是要多练多学。听不大懂数据库。。。
后面那节课老师讲了很多选择题,正处于备考阶段,继续学习吧。。。。