日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《JAVA课程设计》实训第四天——《猜猜看》游戏

發(fā)布時間:2025/3/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《JAVA课程设计》实训第四天——《猜猜看》游戏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第四天,本來想進一步去改進《猜猜看》游戲的。可是非常多問題都不理解。也不熟悉怎么去弄到連接數(shù)據(jù)庫。統(tǒng)計猜對次數(shù),所以并沒有進行再多的改動。

基本上就是這種執(zhí)行結(jié)果了

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.Graphics; 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.FileFilter; import java.io.IOException; import java.util.Random; public class weiwei extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField tfDir; private JTextField tfClass; File[] fileArray; // 目錄下全部文件 int NUM_IMG = 0; // 文件總數(shù)目 int index = 0; // 當(dāng)前文件的序號 int i = 0; JLabel jlbImg1 = null; JLabel jlbImg2 = null; JLabel jlbImg3 = null; private Graphics g; /** * Launch the application. */ class myFileFilter implements FileFilter { @Override public boolean accept(File pathname) { String filename = pathname.getName().toLowerCase(); if (filename.contains(".jpg")) { return false; } else { return true; } } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { weiwei frame = new weiwei(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public weiwei() { setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 645, 500); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); // 選擇目錄 button的處理程序 JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55"); btnDir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jfc.showDialog(new JLabel(), "選擇"); File file = jfc.getSelectedFile(); tfDir.setText(file.getAbsolutePath()); if (file != null && file.isDirectory()) { // 參考: java中File.listFiles(FileFilter) FileFilter的使用 // http://zhouzaibao.iteye.com/blog/347557 ; // 獲取目錄下全部的文件 fileArray = file.listFiles(); NUM_IMG = fileArray.length; } } }); btnDir.setBounds(26, 26, 93, 23); contentPane.add(btnDir); // 文本框,顯示目錄 tfDir = new JTextField(); tfDir.setEditable(false); tfDir.setBounds(125, 27, 450, 21); contentPane.add(tfDir); tfDir.setColumns(10); // 選擇班級 button的處理程序JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");btnClass.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jfc.showDialog(new JLabel(), "選擇"); File file = jfc.getSelectedFile(); tfDir.setText(file.getAbsolutePath()); if (file != null && file.isDirectory()) { // 參考: java中File.listFiles(FileFilter) FileFilter的使用 // http://zhouzaibao.iteye.com/blog/347557 ; // 獲取目錄下全部的文件 fileArray = file.listFiles(); NUM_IMG = fileArray.length; }}});btnClass.setBounds(26, 59, 93, 23);contentPane.add(btnClass);//設(shè)置提醒文字 JLabel j1 = new JLabel("猜猜我是誰,請點我相片"); j1.setBounds(5, 100, 200, 33); this.add(j1); // 標(biāo)簽,顯示帶推測學(xué)生姓名 final JLabel lbGuessName = new JLabel("學(xué)生姓名"); lbGuessName.setBounds(259, 91, 140, 23); contentPane.add(lbGuessName); // 標(biāo)簽,顯示第一個學(xué)生相片 final JLabel lblImg1 = new JLabel("NO.1"); lblImg1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { if (arg0.getSource() == lblImg1) { if ((lblImg1.getText().equals(lbGuessName.getText()))) { JOptionPane.showMessageDialog(null, "YES!

你猜對了。", "提示", JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null, "NO!你猜錯了!", "錯誤", JOptionPane.ERROR_MESSAGE); } } } }); lblImg1.setBounds(26, 155, 150, 200); contentPane.add(lblImg1); // 標(biāo)簽,顯示第二個學(xué)生相片 final JLabel lblImg2 = new JLabel("NO.2"); lblImg2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg1) { if (arg1.getSource() == lblImg2) { if ((lblImg2.getText().equals(lbGuessName.getText()))) { JOptionPane.showMessageDialog(null, "YES。你猜對了。", "提示", JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null, "NO!

你猜錯了!

", "錯誤", JOptionPane.ERROR_MESSAGE); } } } }); lblImg2.setForeground(Color.BLACK); lblImg2.setBackground(SystemColor.inactiveCaption); lblImg2.setBounds(241, 155, 150, 200); contentPane.add(lblImg2); // 標(biāo)簽,顯示第三個學(xué)生相片 final JLabel lblImg3 = new JLabel("NO.3"); lblImg3.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg2) { if (arg2.getSource() == lblImg3) { if ((lblImg3.getText().equals(lbGuessName.getText()))) { JOptionPane.showMessageDialog(null, "YES!你猜對了。", "提示", JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null, "NO!你猜錯了!

", "錯誤", JOptionPane.ERROR_MESSAGE); } } } }); lblImg3.setBounds(434, 155, 150, 200); contentPane.add(lblImg3); // 再猜一次 button,點擊則更新對應(yīng)的三張圖片 與 帶推測學(xué)生姓名 final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21"); btnGuessAgain.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == btnGuessAgain) { // 假設(shè)是nextbutton Random random = new Random(System.currentTimeMillis()); // 循環(huán)三次 ImageIcon icon; // int k[] = null; for (int i = 0; i < 3; i++) { index = random.nextInt(NUM_IMG); String strTmp = fileArray[index].toString(); String filename1 = fileArray[index].getName(); try { icon = new ImageIcon(ImageIO.read(new File(strTmp))); // 從圖表中獲取到圖片 Image image = icon.getImage(); // 縮放圖像 Image smallImage = image.getScaledInstance(150, 200, Image.SCALE_FAST); // 把Image文件轉(zhuǎn)化為ImageIcon icon = new ImageIcon(smallImage); if (index == NUM_IMG) index = 0; switch (i) { case 0: System.out.println(fileArray[index].getName()); // k[0]=index; lblImg1.setIcon(icon); lblImg1.setText(filename1); break; case 1: System.out.println(fileArray[index].getName()); lblImg2.setIcon(icon); lblImg2.setText(filename1); // k[1]=index; break; case 2: System.out.println(fileArray[index].getName()); lblImg3.setIcon(icon); lblImg3.setText(filename1); // k[2]=index; break; } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } @SuppressWarnings("unused") Random random1 = new Random(index); // i = index; System.out.println(fileArray[index].getName()); String filename = fileArray[index].getName(); lbGuessName.setText(filename); } }); btnGuessAgain.setBounds(223, 400, 93, 23); contentPane.add(btnGuessAgain); } }



課程實訓(xùn)的過程是艱辛的,本來就不是非常熟悉JAVA代碼編程,僅僅能靠網(wǎng)絡(luò)上的知識還有老師發(fā)的提示代碼進行改動還有修飾。完畢《猜猜看》游戲。

基本上已經(jīng)完畢。盡管執(zhí)行了起來。可是僅僅是非常主要的。沒有統(tǒng)計次數(shù)。沒有弄到連接數(shù)據(jù)庫或者保存文件。



總結(jié)

以上是生活随笔為你收集整理的《JAVA课程设计》实训第四天——《猜猜看》游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。