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 ...
随机推荐
- iOS优化策略
之前写的一系列文章或者小经验一直没有时间去整理放在博客上,今天整理出来,之前是写在作业部落,语法是markdown,点击链接浏览,仅供参考,希望对你有帮助. https://www.zybuluo.c ...
- 逼近法(例 poj3208、poj1037)
逼近法是一种很奇妙的算法,以为"逼近"这一种思想在很多的算法中都有体现.诸如:像我们的二分答案,不断地排除决策集合的一半以接近我们的最终答案:我们的树上倍增求 \(LCA\) ...
- js对table操作(添加删除交换上下TR)
<table width="100%" border="0" cellpadding="2" cellspacing="1& ...
- Introduce Null Object
今天继续总结<重构>这本书中的一个重构手法,Introduce Null Object.写这个手法是因为它确实很巧妙,在实际编程中经常会遇到这种情况,前人总结出来了这么一个经典的手法,当然 ...
- 处理TCP连包的一小段代码
学习网络编程也有一段时间了,一直听说TCP数据会连包,但一直不知道怎么测试好.最近测试了下:发送方使用对列,将发送的数据存入队列,然后开线程,专门发送.发送多包数据之间不延时.在接收方,他们确实连在一 ...
- 一个简单的servlet
1.创建一个自己的servlet文件,继承HttpServlet MyServlet.java package com.jmu.ccjoin.controller; import java.io.IO ...
- MYSQL初级学习笔记三:数据的操作DML!(视频序号:初级_24,25,36)
知识点五:数据的操作DML(24,25,36) 插入数据: --测试插入记录INSERT CREATE TABLE IF NOT EXISTS user13( id TINYINT UNSIGNED ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- iOS沙盒(sandbox)机制及获取沙盒路径
一. 每个iOS应用SDK都被限制在“沙盒”中,“沙盒”相当于一个加了仅主人可见权限的文件夹,苹果对沙盒有以下几条限制. (1)应用程序可以在自己的沙盒里运作,但是不能访问任何其他应用程序的沙盒. ( ...
- Spring配置错误 No adapter for IAdvice of type
参考:http://www.2cto.com/kf/201305/211728.html 错误十三 在配置拦截器后,运行的时候报错=> Error creating context 'sprin ...