"Java Course Internship" Journal (Thursday) && course Design: "Guess what" game __java

Source: Internet
Author: User

The game did not finish as expected, and then try again, hoping to be able to do it all. Time hastily, write so much for a while.

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.Image;
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.FilenameFilter;
Import java.io.IOException;

Import Java.util.Random;

	public class Guess01 extends JFrame {/** * */private static final long serialversionuid = 1L;
	Private JPanel ContentPane;
	Private JTextField Tfdir;

	Private JTextField Tfclass;   File[] Filearray;    All files under folder int num_img = 0; 

	Total number of documents int index = 0;    String strpath = "";    Folder path String strFileName = ""; //File name JLabel LBLIMG1 = null;
	JLabel lblImg2 = null;
	JLabel LBLIMG3 = null;
	JLabel Lbguessname =null;
	 /** * Launch the application.
					*/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 ("Guess the Game");
		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);
		Select the handler for the Directory button final JButton btndir = new JButton ("directory"); Btndir.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {if (E.getsource () ==btn
					Dir) {JFileChooser jfc=new jfilechooser (); Jfc.setfileselectionmode (Jfilechooser.files_and_directories);
					Jfc.setfileselectionmode (jfilechooser.directories_only);
					Jfc.showdialog (New JLabel (), "selection");

					File File=jfc.getselectedfile ();

					if (File.isdirectory ()) {System.out.println ("folder:" +file.getabsolutepath ());
					}else if (File.isfile ()) {System.out.println ("File:" +file.getabsolutepath ());

					} System.out.println (Jfc.getselectedfile (). GetName ());
					Display the file path in the text box Tfdir.settext (File.getabsolutepath ());

					Jlbimg.seticon (New ImageIcon (File.getabsolutepath ()));
					Gets the file path and filename strpath = File.getabsolutepath ();

					strFileName = Jfc.getselectedfile (). GetName ();

						if (File!=null && file.isdirectory ()) {//Get all Files under folder Filearray = File.listfiles ();
						num_img = Filearray.length;  
							if (E.getsource () ==btndir) {file F = new file (strpath); string[] names = f.list (new FilenameFilter () {public Boolean accept (File F, String name) {return Name.en  

			Dswith (". jpg");					}    
							});  
							Random r = new Random ();    
							String strtmp = strpath+ "/" +names[r.nextint (108)];    
							String strTmp1 =strpath+ "/" +names[r.nextint (108)];  

							String STRTMP2 = strpath+ "/" + names [R.nextint (108)];
							Lblimg1.seticon (New ImageIcon (strtmp));
							Lblimg2.seticon (New ImageIcon (STRTMP1));
						Lblimg3.seticon (New ImageIcon (STRTMP2));

		}
					}
				}
			}
		});
		Btndir.setbounds (26, 26, 93, 23);

		Contentpane.add (Btndir);
		text box, showing directory tfdir = new JTextField ();
		Tfdir.seteditable (FALSE);
		Tfdir.setbounds (125, 27, 363, 21);
		Contentpane.add (Tfdir);

		Tfdir.setcolumns (10);
		Select the handler for the Class button final JButton Btnclass = new JButton ("class");  Btnclass.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {JFileChooser jfc=new
				JFileChooser ();
				Jfc.setfileselectionmode (jfilechooser.files_and_directories);
				Jfc.setfileselectionmode (jfilechooser.directories_only); Jfc.setfileselecTionmode (jfilechooser.files_only);
				File File=jfc.getselectedfile ();
				Jfc.showdialog (New JLabel (), "selection");  

				if (File.isdirectory ()) {System.out.println ("folder:" +file.getabsolutepath ());  
				}else if (File.isfile ()) {System.out.println ("File:" +file.getabsolutepath ());

				} System.out.println (Jfc.getselectedfile (). GetName ());
				Display the file path in the text box Tfclass.settext (File.getabsolutepath ());
				Gets the file path and filename strpath = File.getabsolutepath ();
				strFileName = Jfc.getselectedfile (). GetName ();  
				Gets all the files under the folder Filearray = File.listfiles ();  

			num_img = Filearray.length;

		}
		});
		Btnclass.setbounds (26, 59, 93, 23);

		Contentpane.add (Btnclass);
		text box, showing class file Tfclass = new JTextField ();
		Tfclass.seteditable (FALSE);
		Tfclass.setbounds (125, 60, 363, 21);
		Contentpane.add (Tfclass);

		Tfclass.setcolumns (10);

		Label, show with guess student name final JLabel lbguessname = new JLabel ("Student name");
		Lbguessname.setfocusable (TRUE); Lbguessname.setbounds (259, 91, 102, 23);

		Contentpane.add (Lbguessname);
		Label, showing the first student photo LBLIMG1 = new JLabel ("Student One");
		Lblimg1.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent e) {}});
		Lblimg1.setforeground (Color.Black);
		Lblimg1.setbounds (26, 151, 183, 178);

		Contentpane.add (LBLIMG1);
		Label, showing a second student photo LblImg2 = new JLabel ("Student Two");
		Lblimg2.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent e) {}});
		Lblimg2.setforeground (Color.Black);
		Lblimg2.setbounds (241, 155, 183, 172);

		Contentpane.add (LBLIMG2);
		Label, showing a third student photo LBLIMG3 = new JLabel ("Student Three");
		Lblimg3.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent arg2) {}});
		Lblimg3.setforeground (Color.Black);
		Lblimg3.setbounds (430, 155, 185, 172);

		Contentpane.add (LBLIMG3);
		Once again, click the button, then update the corresponding three pictures with a guess student name final JButton btnguessagain = new JButton ("Guess Again"); Btnguessagain.addactionlistener (New ActionListener () {public void actionperformed (ActionEvent e) {if (E.getsource () ==btnguessagain) {//Current time as seed, avoid two Rando 
					The M object produces the same number sequence//random Random = new Random (System.currenttimemillis ());  
					Random Random = new Random ();  
						ImageIcon icon;//defines the icon variable for (int i=0;i<3;i++) {index = Random.nextint (NUM_IMG);  
						String strtmp = filearray[index].tostring ();  

							try {icon = new ImageIcon (imageio.read) (New File (strtmp)); Image image = Icon.getimage (); Get the picture from the chart image smallimage = Image.getscaledinstance (100,100,image.scale_fast);//Zoom image icon = new I  
							Mageicon (smallimage)//Convert image file to ImageIcon//learn from classmate's if (index==num_img) index = 0;  
								switch (i) {case 0:lblimg1.seticon (icon);  
							Break  
								Case 1:lblimg2.seticon (icon);  
							Break  
								Case 2:lblimg3.seticon (icon);  
							Break  
				}		catch (IOException E1) {e1.printstacktrace ();   

		}     
					}
				}   
			}  
		});
		Btnguessagain.setbounds (223, 337, 93, 23);
	Contentpane.add (Btnguessagain); }///random number mode display Picture ...
 There are text randomly displayed, and finally consider the message box


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.