javaFx 学习笔记

1、每个javaFx程序定义在一个继承自javafx.application.Application的类中

Button:用于设置一个按钮,Button btOK = new Button("Button show name");

Scene:设置一个场景,设置那个Button的大小。Scene scene = new Scene(btok, 200, 250);

    scene(Node, width, height);

Stage:用于设置在窗口中放什么东西的,就是包括Title之类firstStage.setTitle("The first program")

    和放置那个场景,firstStage.setScene(scene)。最后输出firstStage.show();

继承了一个抽象类,需要Override它的start方法,start方法一般用于将UI组件放入一个场景,并且在舞台中显示该场景,

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Liu
 */
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class MyJavaFx extends Application {
    @Override //Override the start method in the Application in class
    public void start(Stage firstStage) {
        Button buttonOK = new Button("stupid_one");
        Scene scene = new Scene(buttonOK, 20, 250);
        firstStage.setTitle("The first program");
        firstStage.setScene(scene);
        firstStage.show();
    }
//    public static void main(String[] args) {
//        Application.launch(args);
//    }
}
View Code

 

 

这样的话,那个按钮会充满着整个屏幕,不好。

那么用一个面板容器类

StackPane pane = new StackPane();

这样把那个Button放进去.就是pane.getChildren().add(btnOK);

然后把它放去场景那里就可以,

设置按钮监听也很简单。

注意的是需要设置成final,因为我这里1.7需要用而已,1.8不报错了。

https://www.zhihu.com/question/39397230

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FxPackage;

/**
 *
 * @author Liu
 */

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MxJavaFx extends Application {
    
    @Override
    public void start(Stage primayStage) {
        StackPane pane = new StackPane();
        pane.getChildren().add(new Button("liuweiming"));
        Scene scene = new Scene(pane, 300, 250);
        final Stage stage = new Stage();
        stage.setTitle("liuweming");
        stage.setScene(scene);
        
        Button btnOK = new Button("OK");
        btnOK.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                stage.show();
            }
        });
        
        pane = new StackPane();
        pane.getChildren().add(btnOK);
        scene = new Scene(pane, 300, 250);
        primayStage.setTitle("hahah");
        primayStage.setScene(scene);
        primayStage.show();
    }
    
    public static void main(String[] arges) {
        launch(arges);
    }
}
View Code

 

 

FlowPane使得每个Node之间更有层次,有规划,不然用pane添加两次Node(Button),会聚在一起,看不到

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FxPackage;

/**
 *
 * @author Liu
 */

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.stage.*;

public class MxJavaFx extends Application {
    
    @Override
    public void start(Stage primayStage) {
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(11, 12, 13, 14)); //它的边框大小以像素作为单位是顶部11、右边12、底部13、左边14
        
        pane.setHgap(5); //指定了面板中两个相邻节点之间的水平和垂直距离
        pane.setVgap(5);
        
        pane.getChildren().addAll(new Label("first name"), new TextField());
        pane.getChildren().addAll(new Label("TEL          "), new TextField());
        
        TextField tfMi = new TextField();
        tfMi.setPrefColumnCount(1); //需要用到这些就需要现适声明
        //设置期望列数是1
        pane.getChildren().add(tfMi);
        
        
        Scene scene = new Scene(pane, 250, 250);
        primayStage.setTitle("ShowFlowPane");
        primayStage.setScene(scene);
        primayStage.show();
    }
    
    public static void main(String[] arges) {
        launch(arges);
    }
}
FlowPane

 

但是上面那个排版有点乱,所以用

GridPane.我靠这些东西都有那么多个面板。一个比一个好,肯定用这个啊

pane.add(节点, col, row);

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package FxPackage;

/**
 *
 * @author Liu
 */

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.stage.*;

public class MxJavaFx extends Application {
    
    @Override
    public void start(Stage primayStage) {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
        pane.setHgap(5.5);
        pane.setVgap(5.5);
        
        pane.add(new Label("帐号"), 0, 0);
        pane.add(new TextField(), 1, 0);
        pane.add(new Label("密码"), 0, 1);
        pane.add(new TextField(), 1, 1);
        Button btn = new Button("click");
        pane.add(btn, 1, 2);
        GridPane.setHalignment(btn, HPos.RIGHT);
        
        Scene scene = new Scene(pane, 300, 250);
        primayStage.setScene(scene);
        primayStage.setTitle("GridPane");
        primayStage.show();
    }
    
    public static void main(String[] arges) {
        launch(arges);
    }
}
GridPane

 

 

 

 

响应事件

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DoNot;

/**
 *
 * @author Liu
 */

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.stage.*;

public class TestClass extends Application {
    
    @Override
    public void start(Stage primayStage) {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
        pane.setHgap(5.5);
        pane.setVgap(5.5);
        
        pane.add(new Label("帐号"), 0, 0);
        pane.add(new TextField(), 1, 0);
        pane.add(new Label("密码"), 0, 1);
        pane.add(new TextField(), 1, 1);
        
        Button btn = new Button("click");
        ClickHandlerClass clickOne = new ClickHandlerClass();
        btn.setOnAction(clickOne);
        pane.add(btn, 1, 2);
        GridPane.setHalignment(btn, HPos.RIGHT);
        
        Scene scene = new Scene(pane, 300, 250);
        primayStage.setScene(scene);
        primayStage.setTitle("GridPane");
        primayStage.show();
    }
    
    public static void main(String[] arges) {
        launch(arges);
    }
}

class ClickHandlerClass implements EventHandler<ActionEvent> {
    @Override
    public void handle(ActionEvent e) {
        GridPane t = new GridPane();
        t.setAlignment(Pos.CENTER);
        t.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
        t.setHgap(5.5);
        t.setVgap(5.5);
        
        t.add(new Label("欢迎点击"), 0, 0);
        
        Scene scene = new Scene(t, 250, 150);
        Stage stage = new Stage();
        stage.setTitle("Click");
        stage.setScene(scene);
        stage.show();
    }
}
View Code

 

用了一个Mybutton类来继承了button类,目的就是重写button里面的tostring方法,因为每个button的id都是不同,我需要点击这个button的时候能知道它是那个id,可以调用super("button name")来放名字

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DoNot;

/**
 *
 * @author Liu
 */
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.stage.*;

public class TestClass extends Application {
    private int id;
    @Override
    public void start(Stage primayStage) {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
        pane.setHgap(5.5);
        pane.setVgap(5.5);
        
//        pane.add(new Label("帐号"), 0, 0);
//        pane.add(new TextField(), 1, 0);
//        pane.add(new Label("密码"), 0, 1);
//        pane.add(new TextField(), 1, 1);
        
//        MyButton btn = new MyButton(1);
//        btn.setText("click");
//        btn.setOnAction(new ClickHandlerClass());
//        btn.setOnAction(e -> {
////            System.out.println(e.toString());
//            GridPane t = new GridPane();
//            t.setAlignment(Pos.CENTER);
//            t.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
//            t.setHgap(5.5);
//            t.setVgap(5.5);
//
//            t.add(new Label(e.toString()), 0, 0);
//
//            Scene scene = new Scene(t, 250, 150);
//            Stage stage = new Stage();
//            stage.setTitle("Click");
//            stage.setScene(scene);
//            stage.show();
//        });
//        pane.add(btn, 1, 2);
//        GridPane.setHalignment(btn, HPos.RIGHT);

        MyButton[] arr = new MyButton[22];
        for (int i = 1; i <= 20; ++i) {
            arr[i] = new MyButton(i, "number" + i);
            arr[i].setOnAction(e-> {
                System.out.println(e.toString());
            });
        }
        int to = 1;
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 5; ++j) {
                pane.add(arr[to++], j, i);
            }
        }
        Scene scene = new Scene(pane, 300, 250);
        primayStage.setScene(scene);
        primayStage.setTitle("GridPane");
        primayStage.show();
    }

    public static void main(String[] arges) {
        launch(arges);
    }

    private class ClickHandlerClass implements EventHandler<ActionEvent> { //内部类
        int tf = id;
        @Override
        public void handle(ActionEvent e) {
            GridPane t = new GridPane();
            t.setAlignment(Pos.CENTER);
            t.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
            t.setHgap(5.5);
            t.setVgap(5.5);
            Label temp = new Label("欢迎点击" + tf);
            t.add(temp, 0, 0);

            Scene scene = new Scene(t, 250, 150);
            Stage stage = new Stage();
            stage.setTitle("Click");
            stage.setScene(scene);
            stage.show();
        }
    }
}

class MyButton extends Button {
    int id;
    MyButton(int _id, String _name) {
        super(_name);
        id = _id;
    }
//    MyButton() {
//        
//    }
    @Override 
    public String toString() {
        return id + "";
    }
}
View Code

 

javafx中launch只能被调用一次,所以自己写的一个MessageBox不能再次调用launch,直接调用start即可,但是start又不能是static的,所以不能写静态方法调用了。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DoNot;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author Liu
 */
public class MessageBox extends Application {
    private static String str;
    @Override
    public void start(Stage first) {
        StackPane stackPane = new StackPane();
        Text text = new Text(str);
        text.setFont(Font.font("Verdana", 20));
        text.setFill(Color.RED);
        stackPane.getChildren().add(text);
        Scene scene = new Scene(stackPane, Math.max(250, str.length() * 20), 100);
        first.setTitle("Show Messge");
        first.setScene(scene);
        first.show();
    }
    public MessageBox() {}
    public void show(String _str) {
        str = _str;
        start(new Stage()); //不能静态方法了
    }
}
View Code

 

 

一个面基计算器

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DoNot;

/**
 *
 * @author Liu
 */
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.stage.*;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

public class TestClass extends Application {
    @Override
    public void start(Stage primayStage) {
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
        pane.setHgap(5.5);
        pane.setVgap(5.5);
        
        Label inputRadiusLabel = new Label("请输入半径:");
        inputRadiusLabel.setOnMouseDragged(e -> {
            show("fff");
        });
        TextField inputRadiusTextField = new TextField("请输入一个实数");
        pane.add(inputRadiusLabel, 0, 0);
        pane.add(inputRadiusTextField, 1, 0);
        Button clickButton = new Button("Click");
        clickButton.setOnMouseDragged(e -> {
            clickButton.setText("aler");
        });
        clickButton.setOnAction(e-> {
            double radius = Double.parseDouble(inputRadiusTextField.getText());
            double res = Math.acos(-1.0) * radius * radius;
            clickButton.setText("aler");
            show("" + res);
        });
        pane.add(clickButton, 1, 1);
        GridPane.setHalignment(clickButton, HPos.RIGHT);
        Scene scene = new Scene(pane, 300, 250);
        primayStage.setTitle("计算圆形面积");
        primayStage.setScene(scene);
        primayStage.show();
    }
    private void show(String str) {
        Pane pane = new Pane();
        Text text = new Text(20, 20, str);
        text.setOnMouseDragged(e -> {
            text.setX(e.getX());
            text.setY(e.getY());
        });
        
        pane.getChildren().add(text);
        Scene scene = new Scene(pane, 250, 50);
        Stage stage = new Stage();
        stage.setTitle("message");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] arges) {
        launch(arges);
    }
}
View Code

 
ImageView类。其中图片要放在生成的.class文件中。(清理并构建项目后,就会清除所有图片,注意)

然后输入包名  + image文件夹名 + 照片

也可以用http://

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DoNot;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Liu
 */
public class PhotoViewer extends Application {
    @Override
    public void start(Stage first) {
        String str = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1792465142,3538385120&fm=23&gp=0.jpg";
        StackPane stackPane = new StackPane();
        ImageView imageView = new ImageView(new Image("DoNot/image/7.png"));
//        ImageView imageView = new ImageView(new Image(str));
        
        stackPane.getChildren().add(imageView);
        Scene scene = new Scene(stackPane, 250, 250);
        first.setTitle("Photo Viewer");
        first.setScene(scene);
        first.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
View Code

 

使用java.util.Date()得到当前日期。

private final java.util.Date whenBuilt;
whenBuilt = new java.util.Date();

 

 ------------------------------------------------------------------------------------------------------------------------------

posted on 2017-02-20 11:05  stupid_one  阅读(460)  评论(0编辑  收藏  举报

导航