Practice
Premise, the company's strategic direction to change the need for technology from. NET to Java so I learned a little bit of Java to find the basic part of a lot of similarities, of course, there are a lot of differences, such as the single quotation mark and double quotation marks are sensitive, and so on here do not explain, to see this first write a small program.
Environment information: Win Server R2 development tool Eclipse 4.3.0
Requirements: Implement simple picture viewing function
Directly on the code:
Import java.awt.eventqueue;import java.awt.event.*;import java.io.*;import javax.swing.*;p ublic class HelloWord { public static void Main (string[] args) {//TODO auto-generated method stub eventqueue.invokelater (new Runnable () {@Overridepublic void R Un () {//start running JFrame frame=new imageviewerframe (); frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setvisible (True);}});}} Display Picture class Imageviewerframe extends Jframe{public imageviewerframe () {settitle ("Picture View"); SetSize (Default_width, Default_height);//create a label to store the image label=new JLabel (); Add (label);//Select File Chooser=new jfilechooser (); Chooser.setcurrentdirectory (New File (".")); /Set button JMenuBar menubar=new jmenubar (); Setjmenubar (MenuBar); JMenu menu=new jmenu ("file"); Menubar.add (menu); JMenuItem opitem=new JMenuItem ("open"); Menu.add (Opitem); Opitem.addactionlistener (new ActionListener () {@ overridepublic void actionperformed (ActionEvent e) {//Display selection button int result=chooser.showopendialog (null);// If the selected file is displayed in lab if (result==jfilechooser.approve_option) {String name=chooser.geTselectedfile (). GetPath (); Label.seticon (new ImageIcon (name));}}); JMenuItem exititem=new JMenuItem ("exit"); Menu.add (Exititem); Exititem.addactionlistener (new ActionListener () {@ overridepublic void actionperformed (ActionEvent arg0) {//Execute exit system.exit (0);}});} Private JLabel label;private jfilechooser chooser;private static final int default_width=300;private static final int DEFA ult_height=400;}
This code comes from the <java core technology-Volume 1 Basics >, when it starts to look a lot of places to understand (probably because of the. NET Foundation) feel particularly like. NET write WebForm include Create button, add button function, etc., where the reference dialect. dll file is equivalent to The package in Java, the reference way is roughly the same, from the beginning to analyze this snippet code:
The first is the program reference,. NET called reference Assembly. NET implements the reuse of methods, calls between programs first need to add the program's reference to the assembly, and then add the. dll file address from the bin directory so that the file references to the current project are regenerated. csproj, Any project that adds this reference can be accessed to this method or project, and the Java reference package has four references, roughly the same as. NET, for example, importing many packages in the same project can be replaced by *, such as import java.lang.* That is, referencing all the packages under Java.lang, a bit like the. Net namespace, without having to write specifically which namespace which class Balabala.
The other main function ah, void static and so on not much to say, here on the Eventqueue.invokelater this, first introduce eventqueue this check the relevant information personally this means, it is swing Important part of the GUI he is mainly responsible for the AWTEvent event distribution mechanism, like a logistics center, when there is an event triggered by the system will send this event to the Logistics center to facilitate his redistribution, and the reference article mentions that this event distribution mechanism is actually a synchronous distribution mechanism, It means that if there is more time to wait, when I first see invokelater this method, the first reaction is the common Begininvoke/invoke asynchronous/synchronous for delegates in. Net The reason for the use of delegates is often due to the fact that there is a callback function that coincides with the GUI mechanism of windows, and the benefits are clear automatically after execution, guaranteeing the program is deterministic and reducing other program class intrusions.
The program continues to execute Imageviewerframe , create the current window Close button, display the current window, it is important to create the window below with. NET create the same way, including buttons, button function settings, this code overrides the open file and Close method, the language skills of the child hard very easy to read, Listens for read file events and then re-displays inside the orebody.
The Java Primer for applets