javafx--2nd Day-Window Basic class

Source: Internet
Author: User
Tags addall

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 (); }        }
    • Showandwait official explanation
      public void showandwait ()
      Shows this stage and waits for it to is hidden (closed) before returning to the caller. This method temporarily blocks processing of the "current event" and starts a nested event loop to handle other events. This method must is called on the FX application thread.

      Stage A is hidden (closed) by one of the following means:

      • The application calls the Window.hide() or close() method on this stage
      • This stage have a Non-null owner window, and its owner is closed
      • The user closes the window via the window system (for example, by pressing the Close button in the window decoration)

javafx--2nd Day-Window Basic class

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.