Java程序设计第十七周之猜猜看

来源:互联网 发布:外交 知乎 编辑:程序博客网 时间:2024/04/28 08:58
import java.awt.EventQueue;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;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.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.IOException;import java.util.Random;import javax.swing.JOptionPane;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;    private int idImg = 1;    JLabel jlbImg1 = null;    JLabel jlbImg2 = null;    JLabel jlbImg3 = null;    /**     * Launch the application.     */    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                try {                    Guess01 frame = new Guess01(null);                    frame.setVisible(true);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }    /**     * Create the frame.     * @param JButton      */    public Guess01(JButton JButton) {        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);                // 选择目录 按钮的处理程序        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(), "open");                   File file=jfc.getSelectedFile();                  tfDir.setText(file.getAbsolutePath());                 if(file!=null && file.isDirectory()){                     // 获取文件夹下所有的文件                     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, 363, 21);        contentPane.add(tfDir);        tfDir.setColumns(10);        // 选择班级 按钮的处理程序        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(), "open");                  File file=jfc.getSelectedFile();                tfClass.setText(file.getAbsolutePath());                            }        });        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);        // 选择班级 按钮的处理程序            JButton  = new JButton("班级");            btnClass.addActionListener(new ActionListener() {                                public void actionPerformed(ActionEvent e) {                    JFileChooser i=new JFileChooser();                      i.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//                    i.showDialog(new JLabel(), "选择班级");                    File file=i.getSelectedFile();                    tfClass.setText(file.getAbsolutePath());                    fileArray = file.listFiles();                    NUM_IMG = fileArray.length;                                                           }             });        btnClass.setBounds(26, 59, 93, 23);            contentPane.add(btnClass);         // 标签,显示带猜测学生姓名        JLabel lbGuessName = new JLabel("\u59D3\u540D");        lbGuessName.setBounds(259, 91, 102, 23);        contentPane.add(lbGuessName);                // 标签,显示第一个学生相片        final JLabel lblImg1 = new JLabel("\u56FE\u72471");        lblImg1.setBackground(Color.RED);          this.add(lblImg1);            lblImg1.addMouseListener(new MouseAdapter() {              @Override              public void mouseClicked(MouseEvent arg0) {                                               if(arg0.getSource()==lblImg1){                      if((lblImg1.getText().equals(lbGuessName.getText()))){                          JOptionPane.showMessageDialog(null,"恭喜你猜对了",strFileName, JOptionPane.PLAIN_MESSAGE);                        }                      else {                          JOptionPane.showMessageDialog(null,"你猜错了!",strFileName, JOptionPane.ERROR_MESSAGE);                      }                      }                                                                               }          });        lblImg1.setBounds(26, 21, 183, 190);        contentPane.add(lblImg1);        jlbImg1 = new JLabel();        jlbImg1.setBackground(Color.RED);        jlbImg1.setBounds(26, 151, 181,201);        this.add(jlbImg1);         // 标签,显示第二个学生相片        final JLabel lblImg2 = new JLabel("\u56FE\u72472");        lblImg2.addMouseListener(new MouseAdapter() {                @Override                public void mouseClicked(MouseEvent arg1) {                    if(arg1.getSource()==lblImg2){                     if(( lblImg2.getText().equals(lbGuessName.getText()))){                         JOptionPane.showMessageDialog(null,"恭喜你猜对了",strFileName, JOptionPane.PLAIN_MESSAGE);                              }                      else {                          JOptionPane.showMessageDialog(null,"你猜错了!",strFileName, JOptionPane.WARNING_MESSAGE);                          }                      }                }            });        lblImg2.setForeground(Color.BLACK);        lblImg2.setBackground(SystemColor.inactiveCaption);        lblImg2.setBounds(241, 21, 100, 190);        contentPane.add(lblImg2);        jlbImg2 = new JLabel();        jlbImg2.setBackground(Color.RED);        jlbImg2.setBounds(240, 155, 183, 201);        this.add(jlbImg2);        // 标签,显示第三个学生相片        final JLabel lblImg3 = new JLabel("\u56FE\u72473");        lblImg3.addMouseListener(new MouseAdapter() {                            @Override                public void mouseClicked(MouseEvent arg2) {                    if(arg2.getSource()==lblImg3){                         if(( lblImg3.getText().equals(lbGuessName.getText()))){                              JOptionPane.showMessageDialog(null,"恭喜你猜对了",strFileName, JOptionPane.PLAIN_MESSAGE);                              }                      else {                          JOptionPane.showMessageDialog(null,"你猜错了!",strFileName, JOptionPane.WARNING_MESSAGE);                          }                      }                        }            });        lblImg3.setBounds(434,21, 100, 190);        contentPane.add(lblImg3);        jlbImg3 = new JLabel();        jlbImg3.setBackground(Color.RED);        jlbImg3.setBounds(434, 155, 185, 201);        this.add(jlbImg3);        // 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名        JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");          btnGuessAgain.addActionListener(new ActionListener() {              public void actionPerformed(ActionEvent e) {                  ImageIcon icon = null;                    Random random = new Random(System.currentTimeMillis());                   index = random.nextInt(NUM_IMG);                   String strTmp = fileArray[index].toString();                  index++;                  if(index==NUM_IMG)                                         index =0;                                  try {                                             icon = new ImageIcon(ImageIO.read(new File(strTmp)));                  } catch (IOException e1) {                      // TODO Auto-generated catch block                      e1.printStackTrace();                  }                                        Image image = icon.getImage();                                       Image smallImage = image.getScaledInstance(100,100,Image.SCALE_FAST);                icon = new ImageIcon(smallImage);                                      lblImg1.setIcon(icon);                                                                              index = random.nextInt(NUM_IMG);                   String strTmp1 = fileArray[index].toString();                  index++;                  if(index==NUM_IMG)                                         index =0;                                                          index = random.nextInt(NUM_IMG);                   String strTmp2 = fileArray[index].toString();                  index++;                  if(index==NUM_IMG)                                         index =0;                                       try {                                                 icon = new ImageIcon(ImageIO.read(new File(strTmp2)));                  } catch (IOException e1) {                      // TODO Auto-generated catch block                      e1.printStackTrace();                  }                                         Image image2 = icon.getImage();                                       Image smallImage2 = image2.getScaledInstance(100,100,Image.SCALE_FAST);                icon = new ImageIcon(smallImage2);                                      lblImg3.setIcon(icon);                  }          });          btnGuessAgain.setBounds(223, 337, 93, 23);          contentPane.add(btnGuessAgain);      }        private void add(JLabel lblImg1) {          // TODO Auto-generated method stub        }  }



部分实现了猜猜看程序,但是还是不能显示班级文件,布局也乱了,总体是实现了一点点

0 0