《JAVA程式設計》實訓第三天——《猜猜看》遊戲

來源:互聯網
上載者:User

標籤:

基本的代碼運行可以通過了。顯示圖片,猜猜看基本可以運行,遊戲整體代碼已經完成。就是統計猜對猜錯次數那個還在努力,在尋找中,不知能不能運行得了一個可以統計猜對猜錯次數的猜猜看遊戲。

<pre name="code" class="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.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; // 檔案總數目      int index = 0; // 當前檔案的序號      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);            // 選擇目錄 按鈕的處理常式          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);                    //設定提醒文字          JLabel j1 = new JLabel("猜猜我是誰,請點我相片");          j1.setBounds(5, 100, 200, 33);          this.add(j1);            // 標籤,顯示帶猜測學生姓名          final JLabel lbGuessName = new JLabel("學生姓名");          lbGuessName.setBounds(259, 91, 140, 23);          contentPane.add(lbGuessName);            // 標籤,顯示第一個學生相片          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);            // 標籤,顯示第二個學生相片          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);            // 標籤,顯示第三個學生相片          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);            // 再猜一次 按鈕,點擊則更新相應的三張圖片 與 帶猜測學生姓名          final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");            btnGuessAgain.addActionListener(new ActionListener() {              public void actionPerformed(ActionEvent e) {                  if (e.getSource() == btnGuessAgain) { // 如果是next按鈕                        Random random = new Random(System.currentTimeMillis());                      // 迴圈三次                        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檔案轉化為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);      }  }  


啟動並執行結果還算可以的。


猜猜看的遊戲代碼編寫,我在1參考老師在之前的日誌的相關代碼還有借鑒網路上的知識,再加上同學的協助,我大概可以運行猜猜看遊戲了。

統計次數那裡覺得很難,不知道從哪裡修改入手好。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

《JAVA程式設計》實訓第三天——《猜猜看》遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.