第十四周课程总结和实验报告
记事本
package demo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 Test implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu,formatMenu,searchMenu,helpMenu;
JMenuItem newItem,openItem,saveItem,closeItem,pageItem,printItem;
JMenuItem withdrawItem,shearItem,copyItem,stickItem,delateItem,seekItem,seeknextItem,replaceItem,turnItem,checkallItem,timeItem;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
public Test() {
frame=new JFrame("记事本");
bar=new JMenuBar();
fileMenu=new JMenu("文件");
editMenu=new JMenu("编辑");
formatMenu=new JMenu("格式");
searchMenu=new JMenu("查找");
helpMenu=new JMenu("帮助");
newItem=new JMenuItem("新建(N)");
openItem=new JMenuItem("打开(O)");
saveItem=new JMenuItem("另存为(A)");
pageItem=new JMenuItem("页面设置(U)");
printItem=new JMenuItem("打印(P)");
closeItem=new JMenuItem("关闭(X)");
withdrawItem=new JMenuItem("撤回(U)");
shearItem=new JMenuItem("剪切(T)");
copyItem=new JMenuItem("复制(C)");
stickItem=new JMenuItem("粘贴(P)");
delateItem=new JMenuItem("删除(L))");
seekItem=new JMenuItem("查找(F)");
seeknextItem=new JMenuItem("查找下一个(N)");
replaceItem=new JMenuItem("替换(R)");
turnItem=new JMenuItem("转到(G)");
checkallItem=new JMenuItem("全选(A)");
timeItem=new JMenuItem("时间(D)");
area=new JTextArea();
scroll=new JScrollPane(area);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
pageItem.addActionListener(this);
printItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(pageItem);
fileMenu.addSeparator();
fileMenu.add(printItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
editMenu.add(withdrawItem);
editMenu.addSeparator();
editMenu.add(shearItem);
editMenu.addSeparator();
editMenu.add(copyItem);
editMenu.addSeparator();
editMenu.add(stickItem);
editMenu.addSeparator();
editMenu.add(delateItem);
editMenu.addSeparator();
editMenu.add(seekItem);
editMenu.addSeparator();
editMenu.add(seeknextItem);
editMenu.addSeparator();
editMenu.add(replaceItem);
editMenu.addSeparator();
editMenu.add(turnItem);
editMenu.addSeparator();
editMenu.add(checkallItem);
editMenu.addSeparator();
editMenu.add(timeItem);
newItem.setMnemonic(KeyEvent.VK_N);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,java.awt.Event.CTRL_MASK));
openItem.setMnemonic(KeyEvent.VK_O);
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,java.awt.Event.CTRL_MASK));
saveItem.setMnemonic(KeyEvent.VK_A);
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,java.awt.Event.CTRL_MASK));
closeItem.setMnemonic(KeyEvent.VK_X);
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,java.awt.Event.CTRL_MASK));
pageItem.setMnemonic(KeyEvent.VK_U);
pageItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,java.awt.Event.CTRL_MASK));
printItem.setMnemonic(KeyEvent.VK_P);
printItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,java.awt.Event.CTRL_MASK));
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(750,500);
frame.setVisible(true);
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(searchMenu);
bar.add(helpMenu);
}
@Override
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);
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) {
chooser=new JFileChooser();
chooser.showOpenDialog(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 e) {
e.printStackTrace();
}
}else if(item==pageItem)
{
PageFormat pf = new PageFormat();
PrinterJob.getPrinterJob().pageDialog(pf);
}
else if(item==printItem) {
Print();
}
else if(item==closeItem) {
System.exit(1);
}
}
}
private void Print() {
// TODO Auto-generated method stub
}
}
运行截图
本周学了关于数据库的知识
1.对于jdbc的一个了解
主要常用操作类与接口:
java.sql.DriverManager 用于管理jdbc驱动程序
Java.sql.ResultSet 用于创建表示sql语句检索结果的结果集,用户通过结果集完成对数据库的访问
2.MySQL的常用命令
1)mysql -u用户名 -p密码 连接mysql数据库
2)创建数据库:CREATE DATABASE 数据库名称;
4.配置mysql数据库的驱动程序
package textshujuku;
public class Text {
public static final String DBDRIVER="ora.gjt.mm.mysql.Driver";
public static void main(String[] args) {
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}