1. 程式人生 > >《Java課程實習》日誌(週四)&&課程設計:《猜猜看》遊戲

《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.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Image;
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;
import java.util.Random;

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 lblImg1 = null;
	JLabel lblImg2 = null;
	JLabel lblImg3 = null;
	JLabel lbGuessName =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("猜猜看遊戲");
		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);


		// 選擇目錄 按鈕的處理程式
		final JButton btnDir = new JButton("目錄");
		btnDir.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==btnDir ){    
					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()){

						// 獲取資料夾下所有的檔案
						fileArray = file.listFiles();

						NUM_IMG = fileArray.length;
						if(e.getSource()==btnDir){

							File f = new File(strPath);  
							String[] names = f.list(new FilenameFilter(){
								public boolean accept(File f , String name){
									return name.endsWith(".jpg");  

								}    
							});

							Random r = new Random();  
							String strTmp = strPath+"/"+names[r.nextInt(108)];    
							String strTmp1 =strPath+"/"+names[r.nextInt(108)];    
							String strTmp2 = strPath+"/"+ names [r.nextInt(108)];  

							lblImg1.setIcon(new ImageIcon(strTmp));
							lblImg2.setIcon(new ImageIcon(strTmp1));
							lblImg3.setIcon(new ImageIcon(strTmp2));
						}
					}
				}
			}
		});

		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("班級");
		btnClass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				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());
				// 獲取檔案路徑 與檔名
				strPath = file.getAbsolutePath();
				strFileName = jfc.getSelectedFile().getName();
				//獲取資料夾下所有的檔案  
				fileArray = file.listFiles();  
				NUM_IMG = fileArray.length;  

			}
		});

		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);

		// 標籤,顯示帶猜測學生姓名
		final JLabel lbGuessName = new JLabel("學生姓名");

		lbGuessName.setFocusable(true);
		lbGuessName.setBounds(259, 91, 102, 23);
		contentPane.add(lbGuessName);

		// 標籤,顯示第一個學生相片
		lblImg1 = new JLabel("學生一");
		lblImg1.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

			}
		});
		lblImg1.setForeground(Color.BLACK);
		lblImg1.setBounds(26, 151, 183, 178);
		contentPane.add(lblImg1);

		// 標籤,顯示第二個學生相片
		lblImg2 = new JLabel("學生二");
		lblImg2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		lblImg2.setForeground(Color.BLACK);
		lblImg2.setBounds(241, 155, 183, 172);
		contentPane.add(lblImg2);

		// 標籤,顯示第三個學生相片
		lblImg3 = new JLabel("學生三");
		lblImg3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg2) {

			}
		});
		lblImg3.setForeground(Color.BLACK);
		lblImg3.setBounds(430, 155, 185, 172);
		contentPane.add(lblImg3);

		// 再猜一次 按鈕,點選則更新相應的三張圖片 與 帶猜測學生姓名
		final JButton btnGuessAgain = new JButton("再猜");
		btnGuessAgain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if(e.getSource()==btnGuessAgain){ 
					
					//當前時間作為種子,避免兩個Random物件產生同樣的數字序列
					//Random random = new Random(System.currentTimeMillis()); 
					Random random = new Random();  
					ImageIcon icon;//定義icon變數  
					for(int i=0;i<3;i++)  
					{  
						index = random.nextInt(NUM_IMG);  
						String strTmp = fileArray[index].toString();  
						try {  

							icon = new ImageIcon(ImageIO.read(new File(strTmp)));  

							Image image = icon.getImage(); // 從圖表中獲取到圖片  

							Image smallImage = image.getScaledInstance(100,100,Image.SCALE_FAST);// 縮放影象  

							icon = new ImageIcon(smallImage);//把Image檔案轉化為ImageIcon  
							//借鑑一下同學的
							if(index==NUM_IMG)  
								index = 0;  
							switch(i)  
							{  
							case 0:  
								lblImg1.setIcon(icon);  
								break;  
							case 1:  
								lblImg2.setIcon(icon);  
								break;  
							case 2:  
								lblImg3.setIcon(icon);  
								break;  
							}  
						} catch (IOException e1) {  

							e1.printStackTrace();  
						}     
					}
				}   
			}  
		});   

		btnGuessAgain.setBounds(223, 337, 93, 23);
		contentPane.add(btnGuessAgain);
	}
}

//隨機數方式顯示圖片。。。還有文字隨機顯示,最後再考慮訊息框