简单记事本的实现
实验代码
package demo6; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Notepad implements ActionListener{ JFrame frame; JScrollPane pan; JMenu menufile; JMenuBar jmenubar; JMenuItem newitem, openitem, saveitem, closeitem; JTextArea area; File file; public Notepad(){ frame=new JFrame("LY"); menufile=new JMenu("文件"); jmenubar=new JMenuBar(); newitem=new JMenuItem("新建(N)"); openitem=new JMenuItem("打开(O)"); saveitem=new JMenuItem("另保存(S)"); closeitem=new JMenuItem("退出(X)"); pan=new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); newitem.setMnemonic(KeyEvent.VK_N); openitem.setMnemonic(KeyEvent.VK_O); saveitem.setMnemonic(KeyEvent.VK_S); closeitem.setMnemonic(KeyEvent.VK_X); newitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.ALT_DOWN_MASK)); openitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.ALT_DOWN_MASK)); saveitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.ALT_DOWN_MASK)); closeitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.ALT_DOWN_MASK)); newitem.addActionListener(this); openitem.addActionListener(this); saveitem.addActionListener(this); closeitem.addActionListener(this); jmenubar.add(menufile); menufile.add(newitem); menufile.add(openitem); menufile.add(saveitem); menufile.add(closeitem); frame.add(pan); frame.setJMenuBar(jmenubar); frame.setVisible(true); frame.setSize(400,300); } public void actionPerformed(ActionEvent event) { Object ob = event.getSource(); if (ob instanceof JMenuItem) { JMenuItem item = (JMenuItem) ob; if (item == newitem) { new Notepad(); } else if (item == openitem) { JFileChooser ch = new JFileChooser(); ch.showOpenDialog(null); file = ch.getSelectedFile(); try { FileInputStream fis = new FileInputStream(file); byte[] b = new byte[fis.available()]; fis.read(b); area.append(new String(b)); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else if (item == saveitem) { JFileChooser ch = new JFileChooser(); ch.showSaveDialog(null); file = ch.getSelectedFile(); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } else if (item == closeitem) { System.exit(0); } } } }
实验截图
总结:本次实验是对前几周所学知识的一次综合实验,讲组件、窗口等充分利用整合起来;让我对之前一些不怎么理解的东西加深了印象/
JDBC总结:
讲了MySQL数据库的安装配置以及使用;如:增加数据(INSERT INTO 表名字[字段]VALUES),删除数据(DELETE FROM 表名称 [删除条件]),更新数据(UPDATE 表名称 SET 字段 [WHERE 更新条件]),
查询数据
对JDBC还是不怎么了解,要多加了解
原文地址:https://www.cnblogs.com/liuz98/p/11958006.html
时间: 2024-10-10 06:22:47