第十四周课程总结&实验报告(简单记事本的实现)
记事本
package demo;
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.FileOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class TextPad implements ActionListener{
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu,formatMenu,lookMenu;
JMenuItem newItem,openItem,saveItem,saveAnotherItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,saveAnotherIcon,closeIcon;
JMenuItem revocationItem,cutItem,copyItem,stickItem,deleteItem,allItem,timeDateItem;
JMenuItem lineItem,typeFaceItem,zoomItem,statusItem;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser; //文件选择框
File file;
public TextPad() {
frame = new JFrame("记事本");
bar=new JMenuBar();
fileMenu=new JMenu("文件");
editMenu=new JMenu("编辑");
formatMenu=new JMenu("格式");
lookMenu=new JMenu("查看");
newIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"newIcon.png");
openIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"openIcon.png");
saveIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"saveIcon.png");
saveAnotherIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"saveAnotherIcon.png");
closeIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"closeIcon.png");
area=new JTextArea();
scroll=new JScrollPane(area);
newItem=new JMenuItem("新建(N)",newIcon);
openItem=new JMenuItem("打开(O)",openIcon);
saveItem=new JMenuItem("保存(S)",saveIcon);
saveAnotherItem=new JMenuItem("另存为(D)",saveAnotherIcon);
closeItem=new JMenuItem("关闭(X)",closeIcon);
revocationItem=new JMenuItem("撤销(Z)");
cutItem=new JMenuItem("剪切(T)");
copyItem=new JMenuItem("复制( C)");
stickItem=new JMenuItem("粘贴(V)");
deleteItem=new JMenuItem("删除(P)");
allItem=new JMenuItem("全选(A)");
timeDateItem=new JMenuItem("时间日期(Q)");
lineItem=new JMenuItem("自动换行(W)");
typeFaceItem=new JMenuItem("字体(F)");
zoomItem=new JMenuItem("缩放(R)");
statusItem=new JMenuItem("状态栏(S)");
//加上监视器
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveAnotherItem.addActionListener(this);
revocationItem.addActionListener(this);
cutItem.addActionListener(this);
copyItem.addActionListener(this);
stickItem.addActionListener(this);
deleteItem.addActionListener(this);
allItem.addActionListener(this);
timeDateItem.addActionListener(this);
lineItem.addActionListener(this);
typeFaceItem.addActionListener(this);
zoomItem.addActionListener(this);
statusItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAnotherItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
editMenu.add(revocationItem);
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(stickItem);
editMenu.add(deleteItem);
editMenu.add(allItem);
editMenu.add(timeDateItem);
formatMenu.add(lineItem);
formatMenu.add(typeFaceItem);
lookMenu.add(zoomItem);
lookMenu.add(statusItem);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_S);
saveAnotherItem.setMnemonic(KeyEvent.VK_D);
closeItem.setMnemonic(KeyEvent.VK_X);
revocationItem.setMnemonic(KeyEvent.VK_Z);
cutItem.setMnemonic(KeyEvent.VK_T);
copyItem.setMnemonic(KeyEvent.VK_C);
stickItem.setMnemonic(KeyEvent.VK_V);
deleteItem.setMnemonic(KeyEvent.VK_P);
allItem.setMnemonic(KeyEvent.VK_A);
timeDateItem.setMnemonic(KeyEvent.VK_Q);
//快捷键
newItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_S,InputEvent.CTRL_DOWN_MASK));
saveAnotherItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_D,InputEvent.CTRL_DOWN_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
revocationItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_Z,InputEvent.CTRL_DOWN_MASK));
cutItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_T,InputEvent.CTRL_DOWN_MASK));
copyItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
stickItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
deleteItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_P,InputEvent.CTRL_DOWN_MASK));
allItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_A,InputEvent.CTRL_DOWN_MASK));
timeDateItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_Q,InputEvent.CTRL_DOWN_MASK));
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(lookMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(600,400);
frame.setLocation(200, 100);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
Object obj=event.getSource();
if(obj instanceof JMenuItem) {
JMenuItem item = (JMenuItem) obj;
if(item==newItem) {
new TextPad();
}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) {
e.printStackTrace();
}
}else if(item==saveItem) {
chooser=new JFileChooser();
chooser.showSaveDialog(null);
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
byte[] b=area.getText().getBytes();
fos.write(b);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
}else if(item==saveAnotherItem) {
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 (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(item==closeItem) {
System.exit(1);
}
}
}
}
测试
package demo;
public class Test {
public static void main(String[] args) {
new TextPad();
}
}
运行截图
1、JDBC概述
JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成
2.JDBC驱动程序分为四类:
1)JDBC-ODBC桥驱动;
2)JDBA本地驱动;
3)JDBC网络驱动;
4)本地协议纯JDBC驱动;
3.JDBC的主要操作类及接口
4.MySQL常用命令
1)连接数据库:mysql -u用户名 -p密码 连接mysql数据库
2)创建数据库:CREATE DATABASE 数据库名称;
3)删除数据库:DROP DATABASE 数据库名称;
4)USE 数据库名称; 使用数据库
5)创建数据库表
CREATE TABLE 表名称(
字段名称1 字段类型 [DEFAULT 默认值][约束],
字段名称2 字段类型 [DEFAULT 默认值][约束],
...,
字段名称n 字段类型 [DEFAULT 默认值][约束]
);