第十四周课程总结&实验报告(简单记事本的实现)
实验报告(简单记事本的实现)
- 源代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.*;
public class TextPad implements ActionListener{
JFrame frame;
JTextArea jta;
File file1;
JMenuBar menubar;
JMenu menu;
JMenuItem newitem;
JMenuItem openitem;
JMenuItem closeitem;
JMenuItem saveitem;
JScrollPane scroll;
JFileChooser jfc;
public TextPad() {
frame=new JFrame("记事本");
jta=new JTextArea(10,4);
jta.setEditable(true);
scroll=new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public Test() {
frame =new JFrame("记事本");
bar =new JMenuBar();
fileMenu =new JMenu("文件");
editMenu =new JMenu("编辑");
newIcon = new ImageIcon("d:" + File.separator + "test" + File.separator +"新建.png");
openIcon = new ImageIcon("d:" + File.separator + "test" + File.separator +"打开.png");
saveIcon = new ImageIcon("d:" + File.separator + "test" + File.separator +"保存.png");
closeIcon = new ImageIcon("d:" + File.separator + "test" + File.separator +"关闭.png");
area =new JTextArea();
scroll =new JScrollPane(area);
newItem =new JMenuItem("新窗口(N)",newIcon);
openItem =new JMenuItem("打开(O)",openIcon);
saveItem =new JMenuItem("保存(S)",saveIcon);
closeItem =new JMenuItem("关闭(X)",closeIcon);
anothersaveItem=new JMenuItem("另存为(A)",saveIcon);
fnewItem =new JMenuItem("新建(N)");
//监听器
fnewItem.addActionListener(this);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
anothersaveItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
fileMenu.add(fnewItem);
fileMenu.add(anothersaveItem);
fileMenu.addSeparator();
//快捷键
fileMenu.setMnemonic(KeyEvent.VK_N);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_S);
anothersaveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_C);
fnewItem.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK));
newItem.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK|InputEvent.SHIFT_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK));
anothersaveItem.setAccelerator(KeyStroke.getKeyStroke('A',java.awt.Event.CTRL_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke('X',java.awt.Event.CTRL_MASK));
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(600,500);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object obj=event.getSource();
if(obj instanceof JMenuItem) {
JMenuItem item=(JMenuItem) obj;
//新建窗口
if(item==newItem) {
new Test();
}
// 打开文本
else if(item == openItem){
chooser=new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//保存
// else if(item == saveItem) {
// chooser=new JFileChooser();
// chooser.showSaveDialog(.this);
// }
//文件另存为
else if(item ==anothersaveItem) {
chooser=new JFileChooser();
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
try {
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fos=new FileOutputStream(file);
byte[] b = area.getText().getBytes();
fos.write(b);
fos.close();
}catch(IOException c) {
c.printStackTrace();
}
}else if(item== closeItem) {
System.exit(1);
}
}
}
}
- 1、JDBC概述
JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成
2.JDBC驱动程序分为四类:
1)JDBC-ODBC桥驱动;
2)JDBA本地驱动;
3)JDBC网络驱动;
4)本地协议纯JDBC驱动;