第十四周课程总结&实验报告(简单记事本的实现)
作者:互联网
实验报告
(简单记事本的实现)
源代码
package JDBC;
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.imageio.*;
import javax.swing.*;
public class Test implements ActionListener{
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem,fnewItem,anothersaveItem;
ImageIcon newIcon,saveIcon,closeIcon,openIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
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);
}
}
}
}
截图:
二、JDBC
JDBC,提供了一种与平台无关的用于执行SQL语句的标准Java API,可以方便地实现多种关系型数据库统一操作,它由用java一组语言编写的类和接口组成
JDBC主要的操作类及接口
- JDBC操作步骤
(1)加载驱动
(2)连接数据库
(3)通过Statement发送sql语句
通过Prepared Statement发送sql语句
(4)关闭数据库
1.加载驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //MySQL中的数据库驱动程序路径
Class.forName(DBDRIVER); //利用class类加载驱动程序,需要处理异常
2.连接数据库
........接上
Connection conn = null;
conn = DriverManager.getConnetion(连接地址,用户名,密码);//需处理异常
3.执行数据库
需要使用Statement 接口完成
标签:awt,java,import,fileMenu,JMenuItem,第十四,new,实验报告,记事本 来源: https://www.cnblogs.com/edg4396/p/11957309.html