《Java课程实习》日志(周四)

Posted jzdwajue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《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.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.FileFilter; 
import java.io.IOException; 
import java.util.Random; //+
import javax.swing.JOptionPane; //+
public class Guess3 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;
	String strPath = "";    //+目录路径
	String strFileName = "";    //+文件名
	JLabel jlbImg = null;
	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 {
					Guess3 frame = new Guess3();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public Guess3() {
		setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 645, 510);
		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()){					
					// 获取目录下全部的文件
					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();
				tfClass.setText(file.getAbsolutePath());

			}
		});
		btnClass.setBounds(26, 59, 93, 23);
		contentPane.add(btnClass);

		// 文本框,显示班级文件
		tfClass = new JTextField();
		tfClass.setEditable(false);
		tfClass.setBounds(125, 60, 450, 21);
		contentPane.add(tfClass);
		tfClass.setColumns(10);

		// 标签,显示带推測学生姓名
		final JLabel lbGuessName = new JLabel("姓名");
		lbGuessName.setBounds(259, 91,140, 23);
		contentPane.add(lbGuessName);

		// 标签。显示第一个学生相片
		final JLabel lblImg1 = new JLabel("图片一");
		lblImg1.addMouseListener(new MouseAdapter () {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getSource()==lblImg1){//+
					if(( lblImg1.getText().equals(lbGuessName.getText()))){
						JOptionPane.showMessageDialog(null,"哇。你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"no,猜错了","错误",JOptionPane.ERROR_MESSAGE);

					}  

				}
			} 
		});
		lblImg1.setBounds(26,155, 150, 200);
		contentPane.add(lblImg1);
		jlbImg1 = new JLabel();
		jlbImg1.setBackground(Color.RED);
		jlbImg1.setBounds(26, 151, 181,201);
		this.add(jlbImg1);


		// 标签,显示第二个学生相片
		final JLabel lblImg2 = new JLabel("图片二");
		lblImg2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg1) {
				if(arg1.getSource()==lblImg2){//+
					if(( lblImg2.getText().equals(lbGuessName.getText()))){
						JOptionPane.showMessageDialog(null,"哇。你猜对了","提示",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);
		jlbImg2 = new JLabel();
		jlbImg2.setBackground(Color.RED);
		jlbImg2.setBounds(240, 155, 183, 201);
		this.add(jlbImg2);

		// 标签。显示第三个学生相片
		final JLabel lblImg3 = new JLabel("图片三");
		lblImg3.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent arg2) {
				if(arg2.getSource()==lblImg3){//+
					if(( lblImg3.getText().equals(lbGuessName.getText()))){
						JOptionPane.showMessageDialog(null,"哇。你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
					}
					else {
						JOptionPane.showMessageDialog(null,"no,猜错了","错误",JOptionPane.ERROR_MESSAGE);
					}  
				}
			}
		});
		lblImg3.setBounds(434,155, 150, 200);
		contentPane.add(lblImg3);
		jlbImg3 = new JLabel();
		jlbImg3.setBackground(Color.RED);
		jlbImg3.setBounds(434, 155, 185, 201);
		this.add(jlbImg3);

		// 再猜一次 button,点击则更新对应的三张图片 与 带推測学生姓名
		final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
		btnGuessAgain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==btnGuessAgain ){ //假设是nextbutton
					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());
								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);
				//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);
	}
} 

技术分享

技术分享

完毕周二和周三代码的结合,其他功能不会做了,仅仅能实现到这里了。假设要使图片和名字能对上的话还要加入数组



以上是关于《Java课程实习》日志(周四)的主要内容,如果未能解决你的问题,请参考以下文章

专业实习日志10

Android课程---Android Studio使用小技巧:提取方法代码片段

实习日志二

课程实习stm32主从蓝牙计算器+温度测量

java学成什么样子可以出去实习?

argparse 代码片段只打印部分日志