1 Internal anonymous classes and lambda expressions
2 Switching Scene
3 Informational alert Box (Alert Boxes)
Previously review:
Previous learning: Basic concepts about JAVAFX, and an introduction to the classes used by Windows
Learn how to use events to make the simplest response to a button-click me click.
1 Internal anonymous classes and lambda expressions
In the previous example, the
Button.setonaction (this);
Make changes
Button.setonaction (new eventhandler<actionevent>() { @Override public void handle (ActionEvent event) { System.out.println ("I am an Annonymous inner class"); });
At this point, the time that the button is clicked is what we later modified, instead of checking the name of each button, and invoking the inner class directly on the object's method after the object was generated, causing the event to occur. "Click Me".
But then there is a problem. According to the last idea we have a lot of buttons when we write the IF condition structure, and then go to the corresponding object in the code, but it is not convenient to use the internal anonymous class.
Oracle started adding a lambda expression in Java 8, changing the statement to the following:
Button.setonaction (e-> System.out.println ("heyyyyy, I am Lambda"));
At this point the console responds by clicking on the button: heyyyyy, I am Lambda,java automatically help us with this event. It can also be changed into
Button.setonaction (e->{ System.out.println ("heyyyyy, I am Lambda1"); System.out.println ("heyyyyy, I am Lambda2"); System.out.println ("heyyyyy, I am Lambda3"); });
2 Switching Scene
Importjavafx.application.Application;Importjavafx.event.ActionEvent;ImportJavafx.event.EventHandler;ImportJavafx.stage.Stage;ImportJavafx.scene.Scene;ImportJavafx.scene.control.Button;ImportJavafx.scene.control.Label;ImportJavafx.scene.layout.StackPane;ImportJavafx.scene.layout.VBox; Public classMainextendsapplication{Stage window; Scene Scene1,scene2; Public Static voidMain (string[] args) {launch (args); } @Override Public voidStart (Stage primarystage)throwsexception{window=Primarystage; Label Label1=NewLabel ("This is Scene1"); Button button1=NewButton ("Go to Scene2"); Button1.setonaction (e-Window.setscene (scene2)); //Layout 1-children is laid out in vertical columnVBox LAYOUT1 =NewVBox (20); Layout1.getchildren (). AddAll (Label1,button1); Scene1=NewScene (layout1,200,200);//200x200 Pixel//Button2Button button2 =NewButton ("Go to Scene1"); Button2.setonaction (e-Window.setscene (scene1)); //Layout2Stackpane Layout2 =NewStackpane (); Layout2.getchildren (). AddAll (Button2); Scene2=NewScene (Layout2, 200, 200); Window.setscene (SCENE1); Window.settitle ("This is a title"); Window.show (); }}
Studying the two different situations of Scene1 and Scene2, scene switching is achieved by clicking button. This example looks a bit like the software we usually use, such as when we want to close a Word document will find that at this time, the system pops up a window, ask whether to save. Sometimes the system goes wrong and a window pops up to prompt for errors. A specific example is described below.
3 Informational alert Box (Alert Boxes)
Click the button to pop up the dialog box
At this point it is much like we implement the Alertbox, if the new pop-up window is not resolved, such as close, then the old window can not be manipulated.
Public classAlertbox { Public Static voidDisplay (string title, String message) {Stage window=NewStage (); Make a new Stage for our Scene window.initmodality (modality.application_modal); Initiate the Mod by the using the Java Library window.settitle (title); Set the title of the new Window Window.setminwidth (250); Label Label1=NewLabel (); Make label to write some message label1.settext (message); Button CloseButton=NewButton ("Close the Window"); Closebutton.setonaction (e-window.close ()); VBox Layout=NewVBox (10); Make the Alert box Layout Layout.getchildren (). AddAll (Label1, CloseButton); ADD the Button and label to the window Layout.setalignment (pos.center); Scene Scene=NewScene (layout); Window.setscene (Scene); Window.show (); Window.showandwait (); } }
javafx--2nd Day-Window Basic class