版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013907419/article/details/46688469
第一部分:实验项目
项目一:随机相片显示。目的:了解随机数的生成,及图片的显示。
目标:
(1)在已有代码的基础上,完成随机(或 顺序)显示三张图片的功能。
(2)当猜测准确,或错误时,弹出对话框进行回应。
点击,下载示例相片(示例相片,提取码为 pufb)。
项目二:数据库初步。目的:了解Java连接数据库的步骤与方法,以及MySQL数据库的安装与使用。
目标:
(1)在机房安装上MySQL数据库。
(2)创建数据表 student(id varchar(12),name varchar(20), success int, failure int ),导入相应的数据到文件中。success 与 failure的默认值为1。
(3)当猜猜正确时,数据表中,success+1;猜测错误时,failure+1。
相关课件: 第10章:数据库(网盘下载)。请大家多练习数据库操作,熟悉Java对数据库的增删改查操作。
第二部分:作业博客要求
1. 在作业博客中,利用这周与17周周一上午的时间,完成两个项目,并把运行结果、代码写到博客中。
2. 在作业博客中,回答以下四个问题:
(1)简述Java中,连接数据库有哪几个基本步骤? 其相应的核心类与代码分别是什么?
(2)简述MySQL中,创建一个数据表的SQL语句是什么?
(3) 简述Java中,生成[1,53]之间的随机数的核心代码分别是什么?
(4)简述Java中,读写文本文件的类分别是什么?核心代码分别是什么?
项目一实现----源代码:
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class Guess01 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0;
String strPath = ""; //文件夹路径
String strFileName = ""; //文件名称
JLabel jlbImg = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guess01 frame = new Guess01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Guess01() {
setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 添加显示Image的JLabel控件
jlbImg = new JLabel();
jlbImg.setBackground(Color.RED);
jlbImg.setBounds(100, 100, 200, 200);
this.add(jlbImg);
// 选择目录 按钮的处理程序
final JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");
btnDir.addActionListener(new ActionListener() {
private Object btnGuessAgain;
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnDir ){ //如果是open按钮
JFileChooser jfc=new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
// 把文件路径显示在文本框中
tfDir.setText(file.getAbsolutePath());
//jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));
// 获取文件路径 与文件名
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory()){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用
// http://zhouzaibao.iteye.com/blog/347557 ;
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
if(e.getSource()==btnGuessAgain){ //如果是next按钮
String strTmp = fileArray[index].toString();
index++;
if(index==NUM_IMG)
index = 0;
jlbImg.setIcon(new ImageIcon(strTmp));
}
}
});
btnDir.setBounds(26, 26, 93, 23);
contentPane.add(btnDir);
// 文本框,显示目录
tfDir = new JTextField();
tfDir.setEditable(false);
tfDir.setBounds(125, 27, 363, 21);
contentPane.add(tfDir);
tfDir.setColumns(10);
// 选择班级 按钮的处理程序
final JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");
btnClass.addActionListener(new ActionListener() {
private Object btnGuessAgain;
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnClass){ //如果是open按钮
JFileChooser jfc=new JFileChooser();
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
//jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
File file=jfc.getSelectedFile();
jfc.showDialog(new JLabel(), "选择");
if(file.isDirectory()){
System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
System.out.println("文件:"+file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
// 把文件路径显示在文本框中
tfClass.setText(file.getAbsolutePath());
//jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));
// 获取文件路径 与文件名
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
//if(file!=null && file.istxt()){
if (file.getName().endsWith(".txt")){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用
// http://zhouzaibao.iteye.com/blog/347557 ;
}
//获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
if(e.getSource()==btnGuessAgain){ //如果是next按钮
String strTmp = fileArray[index].toString();
index++;
if(index==NUM_IMG)
index = 0;
jlbImg .setIcon(new ImageIcon(strTmp));
}
}
});
btnClass.setBounds(26, 59, 93, 23);
contentPane.add(btnClass);
// 文本框,显示班级文件
tfClass = new JTextField();
tfClass.setEditable(false);
tfClass.setBounds(125, 60, 363, 21);
contentPane.add(tfClass);
tfClass.setColumns(10);
// 标签,显示带猜测学生姓名
JLabel lbGuessName = new JLabel("\u59D3\u540D");
lbGuessName.setBounds(259, 91, 102, 23);
contentPane.add(lbGuessName);
// 标签,显示第一个学生相片
JLabel lblImg1 = new JLabel("\u56FE\u72471");
lblImg1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
}
});
lblImg1.setBounds(26, 151, 183, 178);
contentPane.add(lblImg1);
// 标签,显示第二个学生相片
JLabel lblImg2 = new JLabel("\u56FE\u72472");
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(241, 155, 183, 172);
contentPane.add(lblImg2);
// 标签,显示第三个学生相片
JLabel lblImg3 = new JLabel("\u56FE\u72473");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
lblImg3.setBounds(430, 155, 185, 172);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnGuessAgain.setBounds(223, 337, 93, 23);
contentPane.add(btnGuessAgain);
}
}
初步运行结果:
更多内容有待加入。。。