"Java Programming" 16th Week Friday: Database connection and the use of random number && Java Course Internship Journal (Monday) __c#

Source: Internet
Author: User

Part I: Experimental Project one: Random photo display.
Objective: To understand the generation of random numbers and the display of pictures.
Goal:
(1) On the basis of existing code, complete the random (or sequential) display of three pictures of the function.

(2) When guessing accurate, or error, pop-up dialog box to respond.

Click to download the sample photo (sample photo, extract code is PUFB). Item II: Preliminary database.
Objective: To understand the steps and methods of Java connection database, as well as the installation and use of MySQL database.
Goal:
(1) The MySQL database is installed in the computer room.
(2) Create data table student (ID varchar (), name varchar (), success int, failure int), and import the corresponding data into the file. The default value for success and failure is 1.
(3) When the guess is correct, success+1 in the datasheet, failure+1 when the error is guessed.
Related courseware: 10th chapter: Database (Network disk download). Please practice database operations, familiar with the Java database Additions and deletions to check operations.

Part II: Job Blog requirements
1. In the Homework blog, use this week and 17 weeks Monday morning time, complete two projects, and the running results, code written to the blog.
2. In the Homework blog, answer the following four questions:
(1) Briefly describe the basic steps of connecting to a database in Java. The corresponding core class and code are respectively.
(2) briefly describe the SQL statement in MySQL that creates a data table.
(3) A brief description of what the core code of the random number between [1,53] is generated in Java.

(4) A brief description of what classes are in Java for reading and writing text files. What the core code is, respectively.

Project One implementation----source code:

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

	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 = ""; Filenamecalled JLabel jlbimg = 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 ("\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);
		Add a JLabel control that displays image jlbimg = new JLabel ();
		Jlbimg.setbackground (color.red);
		Jlbimg.setbounds (100, 100, 200, 200);

		This.add (JLBIMG);
		Select the handler for the Directory button final JButton btndir = new JButton ("\u9009\u62e9\u76ee\u5f55");

			Btndir.addactionlistener (new ActionListener () {private Object btnguessagain; public void actionperformed (ACTIonevent e) {if (E.getsource () ==btndir) {//If the Open button 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 ()) {//reference: Java file.listfiles (filefilter) filefilter use//http:/

						/zhouzaibao.iteye.com/blog/347557; Get all the files under the folder Filearray = FilE.listfiles ();
					num_img = Filearray.length;
					} if (E.getsource () ==btnguessagain) {//If the Next button String strtmp = filearray[index].tostring ();
					index++;
					if (index==num_img) index = 0;
				Jlbimg.seticon (New ImageIcon (strtmp));

		}  
			}
		});
		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 ("\u9009\u62e9\u73ed\u7ea7");

			Btnclass.addactionlistener (new ActionListener () {private Object btnguessagain; public void actionperformed (ActionEvent e) {if (E.getsource () ==btnclass) {//If the Open button JFileChooser jfc=new
					Lechooser ();
					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 ());

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

					strFileName = Jfc.getselectedfile (). GetName (); if (File!=null && file.istxt ()) {if (File.getname (). EndsWith (". txt")) {//reference: Java file.listfiles (file
					Filter) The use of filefilter//http://zhouzaibao.iteye.com/blog/347557;
					//Get all the files under the folder Filearray = File.listfiles ();
				num_img = Filearray.length;
					} if (E.getsource () ==btnguessagain) {//If the Next button String strtmp = filearray[index].tostring ();
					index++; if (index==nUM_IMG) index = 0;
				Jlbimg. SetIcon (New ImageIcon (strtmp));

		}  
			}
		});
		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 JLabel lbguessname = new JLabel ("\u59d3\u540d");
		Lbguessname.setbounds (259, 91, 102, 23);

		Contentpane.add (Lbguessname);
		Label, showing the first student photo JLabel lblImg1 = new JLabel ("\u56fe\u72471");
		Lblimg1.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent e) {}});
		Lblimg1.setbounds (26, 151, 183, 178);

		Contentpane.add (LBLIMG1);
		Label, showing the second student photo JLabel LblImg2 = new JLabel ("\u56fe\u72472");
		Lblimg2.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent e) {}});
		Lblimg2.setforeground (Color.Black);
		Lblimg2.setbackground (systemcolor.inactivecaption); Lblimg2.setBounds (241, 155, 183, 172);

		Contentpane.add (LBLIMG2);
		Label, showing a third student photo JLabel lblImg3 = new JLabel ("\u56fe\u72473");
		Lblimg3.addmouselistener (New Mouseadapter () {@Override public void mouseclicked (MouseEvent e) {}});
		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 JButton btnguessagain = new JButton ("\u518d\u731c\u4e00\u6b21");
		Btnguessagain.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {}});
		Btnguessagain.setbounds (223, 337, 93, 23);
	Contentpane.add (Btnguessagain);
 }
}

Preliminary operation results:


More content to join ...


Related Article

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.