《Java课程实习》日志(周四--2)
2015-07-03 13:00
615 查看
《猜猜看》游戏
在界面上,能够展示三张学生图片;能够对猜测做出反应;能够显示另外三张相片,继续玩。
运行结果:
![](http://img.blog.csdn.net/20150703104417027?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150703104424596?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150703104431002?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150703104437570?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150703104516940?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg5OTY4MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在界面上,能够展示三张学生图片;能够对猜测做出反应;能够显示另外三张相片,继续玩。
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 Huang 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; 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 { abc frame = new abc(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Huang() { setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 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()) { fileArray = file.listFiles(); NUM_IMG = fileArray.length; } } }); btnDir.setBounds(20, 20, 90, 20); contentPane.add(btnDir); tfDir = new JTextField(); tfDir.setEditable(false); tfDir.setBounds(125, 27, 363, 21); contentPane.add(tfDir); tfDir.setColumns(10); JLabel j1 = new JLabel("猜猜我是谁,请点我相片"); j1.setBounds(5, 100, 200, 35); this.add(j1); final JLabel lbGuessName = new JLabel("学生姓名"); lbGuessName.setBounds(250, 90, 150, 20); 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(25, 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(255, 155, 150, 200); contentPane.add(lblImg2); final JLabel lblImg3 = new JLabel("NO.3"); lblImg3.addMouseListener(new MouseAdapter() { 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(400, 155, 150, 200); // contentPane.add(lblImg3); tfDir = new JTextField(); tfDir.setEditable(false); tfDir.setBounds(125, 27, 363, 21); contentPane.add(tfDir); tfDir.setColumns(10); final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21"); btnGuessAgain.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == btnGuessAgain) { Random random = new Random(System.currentTimeMillis()); ImageIcon icon; 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); icon = new ImageIcon(smallImage); if (index == NUM_IMG) index = 0; switch (i) { case 0: System.out.println(fileArray[index].getName()); lblImg1.setIcon(icon); lblImg1.setText(filename1); break; case 1: System.out.println(fileArray[index].getName()); lblImg2.setIcon(icon); lblImg2.setText(filename1); break; case 2: System.out.println(fileArray[index].getName()); lblImg3.setIcon(icon); lblImg3.setText(filename1); break; } } catch (IOException e1) { e1.printStackTrace(); } } } @SuppressWarnings("unused") Random random1 = new Random(index); System.out.println(fileArray[index].getName()); String filename = fileArray[index].getName(); lbGuessName.setText(filename); } }); btnGuessAgain.setBounds(225, 400, 100, 30); contentPane.add(btnGuessAgain); } }
运行结果:
相关文章推荐
- Java、Json转换方式之三:Fastjson
- Eclipse里项目名有红叉,但是底下的每一个文件都没有红叉
- Java、Json转换方式之二:Jackson
- java 注解
- 《编程导论(Java)·4.1.3 String》乱码问题
- myeclipse快捷键
- Java中this关键字的几种用法
- 部署java程序脚本
- Java实现经典排序算法及复杂度稳定性分析
- Java、Json转换方式之一:json-lib
- JAVA的JDBC连接与sql操作
- java图片压缩,使用ImageIO代替JPEGImageEncoder
- java字符串特殊替换及无法比较字符问题
- 用Java实现九宫格
- spring jpa data笔记
- Java中的equals()
- Java中equals和==的区别
- java乱码问题
- Java泛型详解
- Spring Java-based容器配置(二)