Java course practice log (Thursday) and java practice log

Source: Internet
Author: User

Java course practice log (Thursday) and java practice log

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. syste MColor; 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 c OntentPane; private JTextField tfDir; private JTextField tfClass; File [] fileArray; // int NUM_IMG = 0 for all files in the folder; // int index = 0 for the total number of files; // The serial number of the current file int I = 0; String strPath = ""; // + folder path String strFileName = ""; // + file name JLabel jlbImg = null; JLabel jlbImg1 = null; JLabel jlbImg2 = null; JLabel jlbImg3 = null;/*** Launch the application. */public static void main (String [] args) {EventQueue. invokeLater (new R Unnable () {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"); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,645,510); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5); setContentPane (conte NtPane); contentPane. setLayout (null); // select the JButton whose handler is JButton btnDir = new JButton ("\ u9009 \ u62E9 \ u76EE \ u5F55"); btnDir. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent arg0) {JFileChooser jfc = new JFileChooser (); jfc. setFileSelectionMode (JFileChooser. FILES_AND_DIRECTORIES); jfc. showDialog (new JLabel (), "select"); File file = jfc. getSelectedFile (); tfDir. setText (file. getAbsolut EPath (); if (file! = Null & file. isDirectory () {// obtain all files in the folder fileArray = file. listFiles (); NUM_IMG = fileArray. length ;}}); btnDir. setBounds (26, 26, 93, 23); contentPane. add (btnDir); // text box that displays the directory tfDir = new JTextField (); tfDir. setEditable (false); tfDir. setBounds (125, 27,450, 21); contentPane. add (tfDir); tfDir. setColumns (10); // select the processing program JButton btnClass = new JButton ("\ u9009 \ u62E9 \ u73ED \ u7EA7") for the class button; btnClass. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {JFileChooser jfc = new JFileChooser (); jfc. setFileSelectionMode (JFileChooser. FILES_AND_DIRECTORIES); jfc. showDialog (new JLabel (), "select"); File file = jfc. getSelectedFile (); tfClass. setText (file. getAbsolutePath () ;}}); btnClass. setBounds (26, 59, 93, 23); contentPane. add (btnClass); // text box that displays the class file tfClass = new JTextField (); tfClass. setEditable (false); tfClass. setBounds (125, 60,450, 21); contentPane. add (tfClass); tfClass. setColumns (10); // tag, displays the Student name final JLabel lbGuessName = new JLabel ("name"); lbGuessName. setBounds (259, 91,140, 23); contentPane. add (lbGuessName); // tag, display the first student photo final JLabel lblImg1 = new JLabel ("Image 1"); lblImg1.addMouseListener (new MouseAdapter () {@ Overridepublic void mouseClicked (MouseEvent arg0) {if (arg0.getSource () = lblImg1) {// + if (lblImg1.getText (). equals (lbGuessName. getText () {JOptionPane. showMessageDialog (null, "Wow, you guessed it", "prompt", JOptionPane. PLAIN_MESSAGE);} else {JOptionPane. showMessageDialog (null, "no, guessed wrong", "error", 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); // tag, showing the second student photo final JLabel lblimg = new JLabel ("Image 2"); lblImg2.addMouseListener (new MouseAdapter () {@ Overridepublic void mouseClicked (MouseEvent arg1) {if (arg1.getSource () = lblimg) {// + if (lblImg2.getText (). equals (lbGuessName. getText () {JOptionPane. showMessageDialog (null, "Wow, you guessed it", "prompt", JOptionPane. PLAIN_MESSAGE);} else {JOptionPane. showMessageDialog (null, "no, guessed wrong", "error", JOptionPane. ERROR_MESSAGE) ;}}}); lblImg2.setForeground (Color. BLACK); lblImg2.setBackground (SystemColor. inactiveCaption); lblImg2.setBounds (241,155,150,200); contentPane. add (lblimg); jlbImg2 = new JLabel (); jlbImg2.setBackground (Color. RED); jlbImg2.setBounds (240,155,183,201); this. add (jlbImg2); // tag, showing the third student photo final JLabel lblImg3 = new JLabel ("picture 3"); lblImg3.addMouseListener (new MouseAdapter () {@ Overridepublic void mouseClicked (MouseEvent arg2) {if (arg2.getSource () = lblImg3) {// + if (lblImg3.getText (). equals (lbGuessName. getText () {JOptionPane. showMessageDialog (null, "Wow, you guessed it", "prompt", JOptionPane. PLAIN_MESSAGE);} else {JOptionPane. showMessageDialog (null, "no, guessed wrong", "error", 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); // click it again, click to update the three images and the final JButton btnGuessAgain = new JButton ("\ u518D \ u731C \ u4E00 \ u6B21"); btnGuessAgain. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (e. getSource () = btnGuessAgain) {// if it is the next button, 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); // obtain the Image image = icon from the chart. getImage (); // scale the Image image smallImage = Image. getScaledInstance (150,200, Image. SCALE_FAST); // convert the Image file to ImageIconicon = 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 );}}



After the code is combined on Tuesday and Wednesday, other functions will not be implemented. This can only be implemented here. If you want to match images and names, you need to add arrays.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.