JavaFx (Notepad) Demo

Source: Internet
Author: User
Tags readfile



First we download JavaFX Scene Builder from the official website.



To open the program, you can see the following screen:












, the top left is the JavaFX control list, the bottom left is the UI layer structure, the middle is the visual design area, and the right is the control property.






So, let's build a simple Notepad program!






First use JavaFX Scene Builder to create the following interface.









This is the interface of a simple notepad. Above is a menubar, in the middle is a textarea used to display open text content. In



A ContextMenu is added to the textarea, the so-called right-click menu.






One thing to note here is that Fx:id is a very important attribute that needs to be obtained through fx:id in order to get the controls in the XML that JavaFX Scene Builder edited in the event logic layer.






Also, specify the method of the event in the events of the right property.






As shown, the Open event in menu corresponds to the Onmenuopen method.









In this way, a simple Notepad interface is created. We saved it as Study.xml in JavaFX Scene Builder.






Next, we create a new JavaFX Project in E (FX) Clipse. Remember to set the location of the JavaFX SDK (similar to Android development) in preference.



Create a class of MyApp that inherits from Javafx.application.Application.





[Java]View Plaincopy

Import javafx.application.Application;
Import Javafx.fxml.FXMLLoader;
Import javafx.scene.Parent;
Import Javafx.scene.Scene;
Import Javafx.stage.Stage;
Import Javafx.stage.StageStyle;
public class MyApp extends application {
public static void Main (string[] args) {
Application.launch (Myapp.class, args);
}
@Override
public void start (stage stage) throws Exception {
Parent root = Fxmlloader.load (GetClass (). GetResource ("Study.fxml"));
Scene scene = new Scene (root, 600, 400);
Stage.initstyle (stagestyle.decorated);
Stage.setscene (Scene);
Stage.settitle ("JavaFX notepad");
Stage.show ();
}
}





As shown, we use the Fxmlloader provided in JavaFX to load our edited JavaFX interface. Study.fxml should be placed in the same directory as the MyApp class, with GetClass (). GetResource () to get the resources for the class directory.



The above MyApp class also appears in several JavaFX classes, Parent, Scene, Stage. So what is the use of these classes?






The stage is the topmost container of JavaFX, and the original stage (the parameter after the Start method) is created based on the system platform (which is also a cross-platform focus). Of course, you can also create stage in other parts of the program.






Scene is a container for everything, including controls, and the application must specify the root node of the scene. You can either pass in the root node as it was initialized in the code above, or you can set the root node by using the Setroot method.






The parent is the base class for all nodes that contain child nodes. It is an abstract class that inherits from node. So the loader is actually used in the upward transformation.



From the above explanation, it is easy to know the tree structure used in JavaFX.



In addition, JavaFX uses a very common reflection mechanism to completely separate the UI layer from the event layer. Looking at the study.xml above, you can see that the root node has a Fx:controller property. This property is the class that specifies the event handling. For example, we now have the class for handling events in the application as Test.java. Then modify Fx:controller = "Org.wing.javafx.project01.Test" before the package name.



So, let's write our event-handling class below.


[Java]


Import Java.io.File;
Import Javax.swing.JOptionPane;
Import javafx.event.ActionEvent;
Import Javafx.fxml.FXML;
Import Javafx.scene.Scene;
Import Javafx.scene.control.TextArea;
Import Javafx.scene.layout.AnchorPane;
Import Javafx.stage.FileChooser;
public class Test {
@FXML
Private Anchorpane Layoutpane;
@FXML
Private TextArea filecontent;
Private File result;
@FXML
private void Onmenuopen (ActionEvent event) {
Filechooser filechooser = new Filechooser ();
result = Filechooser.showopendialog (Layoutpane.getscene (). GetWindow ());
if (result! = null) {
Filecontent.settext (Filetools.readfile (result));
}
}
@FXML
private void Onmenusave (ActionEvent event) {
if (result! = null) {
Filetools.writefile (result, Filecontent.gettext ());
}
}
@FXML
private void Onmenuclose (ActionEvent event) {
System.exit (0);
}
@FXML
private void Onmenudelete (ActionEvent event) {
Filecontent.replaceselection ("");
}
@FXML
private void Onmenuabout (ActionEvent event) {
Joptionpane.showmessagedialog (NULL, "JavaFX Notepad is a notepad developed using JavaFX. "," about ", joptionpane.plain_message);
}
@FXML
private void Oncontextselectall (ActionEvent event) {
Filecontent.selectall ();
}
}


Looking at the code above, you will find that the variables and methods that are mapped to JavaFX are @fxml labeled. The name of the variable needs to correspond to the Fx:id property of the control in Study.xml. The method of event handling is also the name of the event defined in the corresponding XML.



Opens a file selector in the Menuopen event, then gets the selected file, reads the contents of the text file, and finally sets it to textarea. As for Filetools, it is the class that is read temporarily under the text file.



In the Menusave event, save the contents of textarea to the file you just opened.



The above also invokes the method of Joptionpane in swing to display the message. This shows that the Java Api can be easily used in today's JavaFX.



In addition, the following is the Filetools code, very simple text file read and write.


[Java]

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileReader;
Import Java.io.FileWriter;
public class Filetools {
public static String readFile (file file) {
StringBuilder resultstr = new StringBuilder ();
try {
BufferedReader breader = new BufferedReader (new FileReader (file));
String line = Breader.readline ();
while (line! = null) {
Resultstr.append (line);
line = Breader.readline ();
}
Breader.close ();
} catch (Exception e) {
E.printstacktrace ();
}
return resultstr.tostring ();
}
public static void WriteFile (file file, String str) {
try {
BufferedWriter bwriter = new BufferedWriter (new FileWriter (file));
Bwriter.write (str);
Bwriter.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}


Finally, you can run the JavaFX program.



JavaFx (Notepad) Demo


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.