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);
// }
}
这样的话,那个按钮会充满着整个屏幕,不好。
那么用一个面板容器类
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, , );
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, , );
primayStage.setTitle("hahah");
primayStage.setScene(scene);
primayStage.show();
} public static void main(String[] arges) {
launch(arges);
}
}
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 pane.setHgap(); //指定了面板中两个相邻节点之间的水平和垂直距离
pane.setVgap(); pane.getChildren().addAll(new Label("first name"), new TextField());
pane.getChildren().addAll(new Label("TEL "), new TextField()); TextField tfMi = new TextField();
tfMi.setPrefColumnCount(); //需要用到这些就需要现适声明
//设置期望列数是1
pane.getChildren().add(tfMi); Scene scene = new Scene(pane, , );
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("帐号"), , );
pane.add(new TextField(), , );
pane.add(new Label("密码"), , );
pane.add(new TextField(), , );
Button btn = new Button("click");
pane.add(btn, , );
GridPane.setHalignment(btn, HPos.RIGHT); Scene scene = new Scene(pane, , );
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("帐号"), , );
pane.add(new TextField(), , );
pane.add(new Label("密码"), , );
pane.add(new TextField(), , ); Button btn = new Button("click");
ClickHandlerClass clickOne = new ClickHandlerClass();
btn.setOnAction(clickOne);
pane.add(btn, , );
GridPane.setHalignment(btn, HPos.RIGHT); Scene scene = new Scene(pane, , );
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("欢迎点击"), , ); Scene scene = new Scene(t, , );
Stage stage = new Stage();
stage.setTitle("Click");
stage.setScene(scene);
stage.show();
}
}
用了一个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[];
for (int i = ; i <= ; ++i) {
arr[i] = new MyButton(i, "number" + i);
arr[i].setOnAction(e-> {
System.out.println(e.toString());
});
}
int to = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
pane.add(arr[to++], j, i);
}
}
Scene scene = new Scene(pane, , );
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, , ); Scene scene = new Scene(t, , );
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 + "";
}
}
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", ));
text.setFill(Color.RED);
stackPane.getChildren().add(text);
Scene scene = new Scene(stackPane, Math.max(, str.length() * ), );
first.setTitle("Show Messge");
first.setScene(scene);
first.show();
}
public MessageBox() {}
public void show(String _str) {
str = _str;
start(new Stage()); //不能静态方法了
}
}
一个面基计算器
/*
* 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, , );
pane.add(inputRadiusTextField, , );
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, , );
GridPane.setHalignment(clickButton, HPos.RIGHT);
Scene scene = new Scene(pane, , );
primayStage.setTitle("计算圆形面积");
primayStage.setScene(scene);
primayStage.show();
}
private void show(String str) {
Pane pane = new Pane();
Text text = new Text(, , str);
text.setOnMouseDragged(e -> {
text.setX(e.getX());
text.setY(e.getY());
}); pane.getChildren().add(text);
Scene scene = new Scene(pane, , );
Stage stage = new Stage();
stage.setTitle("message");
stage.setScene(scene);
stage.show();
}
public static void main(String[] arges) {
launch(arges);
}
}
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, , );
first.setTitle("Photo Viewer");
first.setScene(scene);
first.show();
}
public static void main(String[] args) {
launch(args);
}
}
使用java.util.Date()得到当前日期。
private final java.util.Date whenBuilt;
whenBuilt = new java.util.Date();
------------------------------------------------------------------------------------------------------------------------------
javaFx 学习笔记的更多相关文章
- JavaFX学习笔记——ControlsFX控件集学习——ToggleSwitch和BreadCrumbBar例子
ToggleSwitch ToggleSwitch ts = new ToggleSwitch("开"); 效果 BreadCrumbBar BreadCrumbBar<St ...
- JavaFX 学习笔记——jfoenix类库学习——raised风格按钮创建
创建按钮 JFXButton jfxb = new JFXButton("hello"); jfxb.getStyleClass().add("button-raised ...
- 【javaFX学习】(二) 面板手册--1
找了好几个资料,没找到自己想要的,自己写个列表吧,方便以后用的时候挑选,边学边记.以学习笔记为主,所以会写的会偏个人记忆性.非教程,有什么问题一起讨论啊. 各个不同的控件放入不同的面板中有不同的效果, ...
- 学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记
回到顶部 注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...
- 我的Android进阶之旅------>Android中编解码学习笔记
编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...
- 【javaFX学习】(二) 面板手册
移至http://blog.csdn.net/qq_37837828/article/details/78732591 更新 找了好几个资料,没找到自己想要的,自己整理下吧,方便以后用的时候挑选,边学 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
随机推荐
- An O(ND) Difference Algorithm and Its Variations (1986)
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 The problems of finding a longest com ...
- TButton.Repaint的执行过程
测试,在按钮事件里写上 Button1.Repaint;(包括TWinControl.Invalidate;和procedure TWinControl.Update;两个函数,会被TButton所继 ...
- (MySQL里的数据)通过Sqoop Import Hive 里 和 通过Sqoop Export Hive 里的数据到(MySQL)
Sqoop 可以与Hive系统结合,实现数据的导入和导出,用户需要在 sqoop-env.sh 中添加HIVE_HOME的环境变量. 具体,见我的如下博客: hadoop2.6.0(单节点)下Sqoo ...
- js中的关系操作符
1.关于“小于(<)” a.关于数值的判断就是跟正常的比较一样: b.var result = “23” <"3" ; //true 这两个是字符串,会比较他们的编码 ...
- springboot在eclipse实现热部署
eclipse使用spring-tool-suite插件创建springboot项目,项目创建完成后. 选中项目,右键 Spring Tools --> Add Boot Devtools 点 ...
- oracle:对Index重建、分析
对index进行分析,index_stats 表很有用.下面例子就结合index相关操作及 index_stats 的使用,对index进行分析. SQL> select count(*) fr ...
- Go语言的管道Channel用法
本文实例讲述了Go语言的管道Channel用法.分享给大家供大家参考.具体分析如下: channel 是有类型的管道,可以用 channel 操作符 <- 对其发送或者接收值. ch <- ...
- 为datanode配置多个数据存储地
datanode配置多个数据存储地址,涉及到以下两个配置项 dfs.name.dir Determines where on the local filesystem the DFS name nod ...
- ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml
由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用. 那么得在现有Service项目中添加Http请求方式来实现系统间数据交互.只需要告知请求地 ...
- ThreadPoolExecutor之二:jdk实现的线程池介绍
一 简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.uti ...