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();
------------------------------------------------------------------------------------------------------------------------------
最新文章
- 走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 1
- DataStructure——红黑树学习笔记
- 多个网站使用不同的SSH密钥登陆(zz)
- 传递给数据库 &#39;master&#39; 中的日志扫描操作的日志扫描号无效
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
- 第16章 使用Squid部署代理缓存服务
- Splash Screen开场屏在Android中的实现
- java利用反射绕过私有检查机制实行对private、protected成员变量或方法的访问
- Java:Date、Calendar、Timestamp的使用
- 解决PyGObject在pydev下报错的问题
- mysql limit性能问题
- keepalived+httpd 做web服务的高可用
- Oracle 中Return 和exit的区别
- 关于mybatis中typeHandler的两个案例
- 【多视图几何】TUM 课程 第5章 双视图重建:线性方法
- 谷歌技术";三宝";之GFS
- PyCharm 安装 pip
- An &#39;&#39;all&#39;&#39; model group must appear in a particle with...问题解决记录
- c++刷题(39/100)笔试题3
- jQuery.data() 的实现方式,jQuery16018518865841457738的由来,jQuery后边一串数字的由来
热门文章
- BootStrap-DualListBox怎样改造成为双树
- 对云资源服务商资源读写的架构思考:前端代码走token
- gitlab merge过程
- delphi 八字排盘源码(post数据以后,又分析数据)
- SDUT OJ 2054 双向链表的实现 (结构体node指针+遍历 *【模板】)
- LightOJ - 1422 Halloween Costumes —— 区间DP
- 在canvas标签和style中定义width和height
- MongoDb复制集实现故障转移,读写分离
- nodejs 打造 多人对战游戏服务器(初级入门)
- Hibernate 4.3.7 可编程方式+注解