"Java Programming" the 16th Week Thursday: GUI Programming and File dialog box use __ algorithm

Source: Internet
Author: User
Tags numeric value stub
Part I: Experimental projects
Item I: Documents and options
Objective: To understand how to use the file dialog box and familiarize yourself with the core event-driven steps in the GUI.
1. Read the blog post "jfilechooser (File selection dialog provided by Java Swing)" To understand the effect of the blog post.
2. In the IDE, enter (not copy, paste) posting code, debug and run.
3. In the above interface, add the following controls:
JLabel, display text as "Selected files or folders:";
JTextField, displays the selected file or folder when the file or folder is selected.

4. Complete the above function. Attention:
1. Master GUI commonly used in the layout of the above procedures, set the layout for FlowLayout, or borderlayout (reference material p138-144).
2. Understand the online view of a control of Java, class usage, compare the differences between Chinese and English documents. Where site:xxx represents the search at the specified URL.
Chinese: Baidu search "site:apihome.cn Java API XXX", such as "site:apihome.cn Java API JTextField"
English: Baidu search "site:docs.oracle.com Java API XXX", such as "Site:docs.oracle.com Java API JTextField"
3. Summarize the working principle of Java event-driven, refer to and rewrite the textbook example 6-1 (p115-116), rewrite the applet as JFrame.

4. Why do I need to "knock" code? See Bowen to all BBS students: How to learn a good way. It's easier to go to the stupid method of Python. Item Two: Picture display
Objective: To understand the common ways of image display in GUI.

Objective: To use Project one, to get the image files that need to be displayed in the interface (reference: In Java Swing, who controls to install a picture?) the 14th week of Java Programming Experiment assignment: GUI programming preliminary Project Three: Experiment six graphical user interface (textbook p279-280)

Objective: To complete the contents of experiment six.

Attention:
1. Understand how strings are converted to numbers, and how numbers are converted into strings.

Part II: Job Blog requirements
1. In the Homework blog, complete the above 3 projects at least 2, and the running results, code written to the blog.
2. In the Homework blog, answer the following three questions:
(1) Briefly describe the event-driven mode of GUI in Java.
(2) The common way of translating strings into numeric values and converting them into strings is described in Java.
(3) A brief description of the core code used by JFileChooser in Java.

Project One code:

Import Java.awt.FlowLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.File;
Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JTextField;
	public class FileChooser2 extends JFrame implements actionlistener{JButton;

	JTextField Jtfpath = null;
	public static void Main (string[] args) {new FileChooser2 ();

		Public FileChooser2 () {this.setlayout (New FlowLayout ());
		button to initialize open=new JButton ("open");
		Add listening Open.addactionlistener (this);

		Add the button to the JFrame container this.add (open);
		Add a text box control Jtfpath = new JTextField ("Selected file", 40);     Jtfpath.seteditable (FALSE);    Cannot edit jtfpath.sethorizontalalignment (jtextfield.center);

		Center This.add (Jtfpath);
		Set the size of the jframe to display, the default Close button This.setbounds (400, 200, 700, 500);
		This.setvisible (TRUE);
	This.setdefaultcloseoperation (Jframe.exit_on_close); } @Override public void ActionperformeD (ActionEvent e) {//TODO auto-generated method stub jfilechooser jfc=new jfilechooser ();
		Jfc.setfileselectionmode (jfilechooser.files_and_directories);
		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 Jtfpath.settext (File.getabsolutepath ());
 }
}

Result diagram:

Item two code:

Import Java.awt.Color;
Import Java.awt.FlowLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.File;
Import Java.io.FileFilter;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JTextField; Class Myfilefilter implements filefilter{@Override public boolean accept (File pathname) {String filename = pa  
		Thname.getname (). toLowerCase ();  
		if (Filename.contains (". jpg")) {return false;  
		}else{return true; }} public class Filechooser extends JFrame implements actionlistener{/** * */private static final long

	Serialversionuid = 1L;
	JButton Open=null;

	JTextField Jtfpath = null;
	JLabel jlbimg = null;

	JButton btnnext = null;    String strpath = "";    Folder path String strFileName = "";   File name file[] Filearray;    All files under folder int num_img = 0;    Total number of documents int index = 0;

	Serial number of the current filepublic static void Main (string[] args) {new Filechooser ();
		Public Filechooser () {this.settitle ("Week16");

		Set layout mode this.setlayout (new FlowLayout ());
		button to initialize open=new JButton ("select Directory");
		Add listening Open.addactionlistener (this);

		Add the button to the JFrame container this.add (open);
		Add a text box control Jtfpath = new JTextField ("Selected file", 40);     Jtfpath.seteditable (FALSE);    Cannot edit jtfpath.sethorizontalalignment (jtextfield.center);

		Center This.add (Jtfpath);
		Show Next Picture btnnext = new JButton ("Show Next");
		This.add (Btnnext);

		Btnnext.addactionlistener (this);
		Add a JLabel control that displays image jlbimg = new JLabel ();
		Jlbimg.setbackground (color.red);
		Jlbimg.setbounds (100, 100, 200, 200);

		This.add (JLBIMG);
		Set the size of the jframe to display, the default Close button This.setbounds (400, 200, 700, 500);
		This.setvisible (TRUE);
	This.setdefaultcloseoperation (Jframe.exit_on_close); @Override public void actionperformed (ActionEvent e) {/TODO auto-generated method Stub//multiple components need to be monitored, the encoding side of the event response Type if (e.getSource () ==open) {//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 Jtfpath.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://zho

				uzaibao.iteye.com/blog/347557;
				Gets all the files under the folder Filearray = File.listfiles ();
			num_img = Filearray.length; } if (E.getsouRce () ==btnnext) {//If the Next button String strtmp = filearray[index].tostring ();
			index++;
			if (index==num_img) index = 0;
		Jlbimg.seticon (New ImageIcon (strtmp)); }       
	}
}
Run Result:


One:

Swing is an indispensable window tool group in Java and a powerful tool for users to build graphical user interface (GUI) programs. The Java swing component automatically generates various events to respond to user behavior. When a user clicks a button or selects a menu item, the Swing component produces a actionevent. Swing components produce many events, such as Actionevents,changeevents,itemevents, to respond to the user's mouse clicks, change the value in the list box, and start the timer. In Java Swing programming, by registering listeners, we can listen to events generated by event sources, thus handling the user behavior we need to handle in the event handler.

In this program, a registered listener is used to listen for events generated by the event source (such as by ActionListener to respond to a user click button) or to define methods for handling events (such as defining the corresponding method in the actionperformed in ActionListener).

Two:

The string is converted to a numeric value: Integer.parseint (String str), double.parsedouble (string str);

Converting to a string can be either string.valueof (String str) or (string str). toString

can also go to look for JDK_API_1_6_ZH_CN. A string class in a CHM document, as I saw on the Internet

int i = ten;
Boolean bo = false;
float f = 32.5f;
byte B =;
Double d = 3456.98
char c = ' f ';
Long L = 3456789;
Short s = 128;
 
StringBuffer result = new StringBuffer ();
Result.append (i);
Result.append (bo);
Result.append (f);
Result.append (b);
Result.append (d);
Result.append (c);
Result.append (l);
Result.append (s);
Cast: such as plastic: Integer.parseint ();
Floating point type: Float.parsefloat ();
Double type: double.parsedouble ();
Long type: Lang.parselang ();

Three: The core code used by JFileChooser

JFileChooser jfc=new JFileChooser ();
        Jfc.setfileselectionmode (jfilechooser.files_and_directories);
File File=jfc.getselectedfile ();
        
The above code constructs a jfilechooser that points to the user's default directory, constructs a jfilechooser with the given file as a path, and returns a list of selected files (file[]).
Item three pending additional * * *


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.