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

package test1;

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;
@SuppressWarnings("unused")
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;    // 当前文件的序号
	/**
	 * 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 {
					Guess01 frame = new Guess01();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public Guess01() {
		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(), "选择");  
				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(), "选择");  
				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);

		// 标签,显示带猜测学生姓名
		JLabel lbGuessName = new JLabel("\u59D3\u540D");
		lbGuessName.setBounds(259, 91, 102, 23);
		contentPane.add(lbGuessName);

		// 标签,显示第一个学生相片
		final JLabel lblImg1 = new JLabel("学生一");

		lblImg1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

			} 
		});
		lblImg1.setBounds(26, 21, 183, 200);
		contentPane.add(lblImg1);


		// 标签,显示第二个学生相片
		final JLabel lblImg2 = new JLabel("学生二");
		lblImg2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		lblImg2.setForeground(Color.BLACK);
		lblImg2.setBackground(SystemColor.inactiveCaption);
		lblImg2.setBounds(241, 21, 200, 200);
		contentPane.add(lblImg2);

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

			@Override
			public void mouseClicked(MouseEvent e) {
				
			}
		});
		lblImg3.setBounds(434,21, 200, 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 ){ 
					String strTmp = fileArray[index].toString();
					index++;
					if(index==NUM_IMG)
					index = 0;
					index++;
					index++;
					if(e.getSource()==btnGuessAgain ){ 
						String strTmp2 = fileArray[index].toString();
					index++;
					index++;
					}

				}   String strTmp1 = fileArray[index].toString();
				index++;
			}
		});
		btnGuessAgain.setBounds(250, 400, 93, 23);
		contentPane.add(btnGuessAgain);
	}


结果如图所示:

《Java课程实习》日志(周二)_第1张图片



总结:在丁老师给的代码基础上,终于做出文件选择文本框,短短十几行代码,让我找书和去百度,真够难受的,但是,做到后,发现由于有了这个文件选择文本的代码,

找文件就变的简单了,看来,java的世界真的很神奇。




 
  


你可能感兴趣的:(《Java课程实习》日志(周二))