1. 程式人生 > >《Java程式設計》第16週週五:資料庫連線 與 隨機數的使用&&《Java課程實習》日誌(週一)

《Java程式設計》第16週週五:資料庫連線 與 隨機數的使用&&《Java課程實習》日誌(週一)

 第一部分:實驗專案

專案一:隨機相片顯示。
目的:瞭解隨機數的生成,及圖片的顯示。
目標:
(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);
	}
}

初步執行結果:


更多內容有待加入。。。