专栏目录
1.JavaFx实现闹钟小程序2.银行账户管理员
3.大数字
4.购物车
5.文本编辑器
6.乌龟图
JavaFx菜鸟教程
JavaFx哔哩哔哩教程
JavaFx是java实现图形界面的一种方式,其他还有java的awt、swing,但是逐渐被淘汰。
awt --> swing --> JavaFx
javafx可以实现逻辑和样式的分离,可以使用xml和css来编写样式。
在学习之前请确保你已经熟练掌握面向对象、包装类、枚举、注解、匿名对象等内容的概念和使用
自从java11以后,jdk已经不内置javafx库,已交给开源社区管理,所以我们需要自己导入
可以到网站去下载 jar 包。注意下载的类型是sdk
若无法访问建议使用科学上网,或自行选取其他方式下载
或者使用maven引入依赖
<dependency>
<groupId>org.openjfxgroupId>
<artifactId>javafx-controlsartifactId>
<version>19-ea+8version>
dependency>
Make a simple notepad style editor that the user can use to create, open and edit simple text based files. Provide functionality found in your typical editor including saving, copy/paste and printing capability.
–src
------image
------BackGround.java
------RightPointB.java
------SaveFile.java
------TextEditor.java
/** @projectName: TextEditor
* @package: javaFx
* @className: TextEditor
* @author: Jiabao Yu
* @date: 2023/1/12 16:29
*/
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class TextEditor extends Application {
protected String name = "C:/Users/Jiaba/Desktop/xx.txt";
protected static String path = "";
protected static TextArea textArea = new TextArea();
VBox group = new VBox();
Scene scene = new Scene(group, 192 * 4, 103.5 * 4);
protected static Button b = new Button("保存"),
g = new Button("格式"), p = new Button("打印");
@Override
public void start(Stage textMain) {
setMenu();
textArea.prefWidthProperty().bind(scene.widthProperty());
textArea.prefHeightProperty().bind(scene.heightProperty());
group.getChildren().add(textArea);
//
ButtonAction();
//
ScrollPane sc = new ScrollPane();
sc.setContent(textArea);
textMain.setTitle(name);
textMain.setScene(scene);
textMain.show();
}
public void setMenu() {
HBox menu = new HBox();
menu.prefWidthProperty().bind(scene.widthProperty());
//
g.setPrefWidth(65);
b.setPrefWidth(65);
p.setPrefWidth(65);
menu.getChildren().addAll(b, g, p);
group.getChildren().add(menu);
}
public static void ButtonAction(){
b.setOnAction(e->{
new SaveFile().textSave(textArea,path);
});
g.setOnAction(e->{
new SaveFile.FONT(textArea);
});
p.setOnAction(e->{
new SaveFile().print(textArea,path);
});
}
}
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
public class BackGround extends Application {
private static double H = 0, V = 0;
private static Group group = new Group();
private static Scene scene = new Scene(group, 1920, 1035);
ImageView view;
RightPointB rpb = new RightPointB();
@Override
public void start(Stage Background) {
getImage();
scene.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.SECONDARY) {
Node node = e.getPickResult().getIntersectedNode();
rpb.getInstance().show(node, e.getScreenX(), e.getScreenY());
} else {
rpb.getInstance().hide();
}
});
Background.setTitle("Windows");
Background.setScene(scene);
Background.show();
}
//背景图片
public void getImage() {
Image image = new Image("file:src/image/bg.jpg");
view = new ImageView(image);
view.fitWidthProperty().bind(scene.widthProperty());
view.fitHeightProperty().bind(scene.heightProperty());
group.getChildren().add(view);
}
//背景图片
public static void creat() {
ImageView c = new ImageView(new Image("file:src/image/text.png"));
c.fitWidthProperty().bind(scene.widthProperty().multiply(0.045));
c.fitHeightProperty().bind(scene.heightProperty().multiply(0.09));
VBox creat = new VBox(0);
TextField nameC = new TextField(" ");
nameC.prefWidthProperty().bind(scene.widthProperty().multiply(0.045));
nameC.prefHeightProperty().bind(scene.heightProperty().multiply(0.01));
creat.getChildren().addAll(c, nameC);
creat.layoutXProperty().bind(scene.widthProperty().multiply(H / 960));
creat.layoutYProperty().bind(scene.heightProperty().multiply(V / 517.5));
if (V + scene.getHeight() * 0.13 > 450) {
V = 0;
H = H + scene.getWidth() * 0.05;
} else {
V = V + scene.getHeight() * 0.13;
}
group.getChildren().add(creat);
String path = localPath();
nameC.setOnKeyPressed(ov -> {
if (ov.getCode() == KeyCode.ENTER) {
String name = nameC.getText();
new SaveFile().textCreate(path, name);
nameC.setEditable(false);
}
});
nameC.setOnMouseClicked(e -> {
int times = e.getClickCount();
if (times == 2) {
String old;
nameC.setEditable(true);
old = nameC.getText();
nameC.setOnKeyPressed(ov -> {
if (ov.getCode() == KeyCode.ENTER) {
String name = nameC.getText();
new SaveFile().textRename(path, old, name);
nameC.setEditable(false);
}
});
}
});
c.setOnMouseClicked(ov -> {
int times = ov.getClickCount();
if (times == 2) {
TextEditor p = new TextEditor();
p.name = nameC.getText();
String name = path + p.name + ".txt";
new SaveFile().textOpen(p, name);
p.start(new Stage());
}
});
}
public static void open() {
GridPane gp = new GridPane();
Stage stageO = new Stage();
stageO.setScene(new Scene(gp, 260, 50));
stageO.show();
//
Label urL = new Label("PS:C:/Users/Jiaba/Desktop/xx.txt");
TextField url = new TextField();
Button btOK = new Button("OK");
gp.add(urL, 0, 0);
gp.add(url, 0, 1);
gp.add(btOK, 1, 1);
//
ImageView o = new ImageView(new Image("file:src/image/text.png"));
o.fitWidthProperty().bind(scene.widthProperty().multiply(0.045));
o.fitHeightProperty().bind(scene.heightProperty().multiply(0.09));
//
btOK.setOnAction(e -> {
String path =url.getText();
File fil = new File(path);
stageO.close();
VBox open = new VBox(0);
TextField nameO = new TextField();
nameO.prefWidthProperty().bind(scene.widthProperty().multiply(0.045));
nameO.prefHeightProperty().bind(scene.heightProperty().multiply(0.01));
String na = fil.getName().substring(0,fil.getName().lastIndexOf("."));
nameO.setText(na);
nameO.setEditable(false);
//
open.getChildren().addAll(o, nameO);
open.layoutXProperty().bind(scene.widthProperty().multiply(H / 960));
open.layoutYProperty().bind(scene.heightProperty().multiply(V / 517.5));
if (V + scene.getHeight() * 0.13 > 450) {
V = 0;
H = H + scene.getWidth() * 0.05;
} else {
V = V + scene.getHeight() * 0.13;
}
group.getChildren().add(open);
///
o.setOnMouseClicked(ov -> {
int times = ov.getClickCount();
if (times == 2) {
TextEditor op = new TextEditor();
new SaveFile().textOpen(op, url.getText());
op.name = na;
op.start(new Stage());
}
});
});
}
public static String localPath(){
String p="";
String t = FileSystemView.getFileSystemView().getHomeDirectory().getPath();
for(int i=0;i<t.length();i++){
if(t.charAt(i)=='\\'){
p+='/';
}else {
p+=t.charAt(i);
}
}
return p+'/';
}
}
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
@SuppressWarnings("restriction")
public class RightPointB extends ContextMenu {
/**
* 单例
*/
private static RightPointB INSTANCE = null;
private MenuItem openFile = new MenuItem("打开");
private MenuItem createFile = new MenuItem("新建");
/**
* 私有构造函数
*/
RightPointB() {
Event();
getItems().add(openFile);
getItems().add(createFile);
}
public void Event() {
openFile.setOnAction(e -> {
BackGround.open();
});
createFile.setOnAction(e->{
BackGround.creat();
});
}
/**
* 获取实例 * @return GlobalMenu
*/
public static RightPointB getInstance() {
if (INSTANCE == null) {
INSTANCE = new RightPointB();
}
return INSTANCE;
}
}
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import java.io.*;
public class SaveFile {
public static void textOpen(TextEditor te, String path) {
File fOpen = new File(path);
te.path = path;
FileReader fr = null;
try {
fr = new FileReader(fOpen);
char a[] = new char[(int) fOpen.length()];
fr.read(a);
te.textArea.appendText(String.valueOf(a));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void textCreate(String path, String name) {
String Name = path + name + ".txt";
File created = new File(Name);
if (!created.exists()) {
try {
created.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Error();
}
}
public static void textRename(String path, String name, String rename) {
String old = path + name + ".txt";
File created = new File(old);
String Name = path + rename + ".txt";
System.out.println(Name);
created.renameTo(new File(Name));
}
public static void textSave(TextArea ta, String path) {
File file = new File(path);
try {
FileWriter fw = new FileWriter(file);
fw.write(ta.getText());
fw.flush();
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void print(TextArea ta, String path) {
String p = path.substring(0, path.lastIndexOf("."));
String print = p + "打印.txt";
File file = new File(print);
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(ta.getText());
fw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void Error() {
Pane pane = new Pane();
Label label = new Label("!!!文件已存在!!!");
label.setFont(Font.font(48));
pane.getChildren().add(label);
Stage sta = new Stage();
sta.setScene(new Scene(pane));
sta.show();
}
public static class FONT extends Group {
Scene scene = new Scene(this, 192 * 1.5, 103.5 * 3);
Stage sta = new Stage();
Button apply = new Button("应用");
ComboBox font = new ComboBox();
ComboBox size = new ComboBox();
ComboBox color = new ComboBox();
public FONT(TextArea ta) {
put(ta);
sta.setTitle("格式");
apply.setOnAction(e -> {
Apply(ta);
});
sta.setScene(scene);
sta.show();
}
public void put(TextArea ta) {
Label fon = new Label("字体:"), siz = new Label("大小:"),
col = new Label("颜色:");
font.setLayoutX(69);
font.setLayoutY(10);
font.setPrefWidth(190);
fon.setLayoutX(19);
fon.setLayoutY(10);
size.setLayoutX(69);
size.setLayoutY(90);
size.setPrefWidth(190);
siz.setLayoutX(19);
siz.setLayoutY(90);
color.setLayoutX(69);
color.setLayoutY(170);
color.setPrefWidth(190);
col.setLayoutX(19);
col.setLayoutY(170);
getChildren().addAll(font, size, color, fon, siz, col);
apply.setFont(Font.font(24));
apply.setLayoutX(150);
apply.setLayoutY(210);
apply.setPrefWidth(95);
apply.setPrefHeight(95);
getChildren().add(apply);
//
font.setValue(ta.getFont().getFamily());
size.setValue(ta.getFont().getSize());
color.setValue("黑色");
setFont(font);
setColor(color);
setSize(size);
}
public void setFont(ComboBox c) {
c.getItems().addAll("黑体", "楷体", "宋体", "隶书", "System");
}
public void setColor(ComboBox c) {
c.getItems().addAll("黑色", "红色", "黄色", "绿色", "金色", "紫色", "白色", "蓝色");
}
public void setSize(ComboBox c) {
c.getItems().addAll("8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72");
}
public void Apply(TextArea ta) {
if (font.getValue() == "黑体") {
ta.setFont(Font.font("SimHei"));
} else if (font.getValue() == "楷体") {
ta.setFont(Font.font("KaiTi"));
} else if (font.getValue() == "宋体") {
ta.setFont(Font.font("NSimSun"));
} else if (font.getValue() == "隶书") {
ta.setFont(Font.font("LiSu"));
} else if (font.getValue() == "System") {
ta.setFont(Font.font("System"));
}
if (size.getValue() == "8") {
ta.setFont(Font.font(8));
} else if (size.getValue() == "9") {
ta.setFont(Font.font(9));
} else if (size.getValue() == "10") {
ta.setFont(Font.font(10));
} else if (size.getValue() == "11") {
ta.setFont(Font.font(11));
} else if (size.getValue() == "12") {
ta.setFont(Font.font(12));
} else if (size.getValue() == "14") {
ta.setFont(Font.font(14));
} else if (size.getValue() == "16") {
ta.setFont(Font.font(16));
} else if (size.getValue() == "18") {
ta.setFont(Font.font(18));
} else if (size.getValue() == "20") {
ta.setFont(Font.font(20));
} else if (size.getValue() == "22") {
ta.setFont(Font.font(22));
} else if (size.getValue() == "24") {
ta.setFont(Font.font(24));
} else if (size.getValue() == "26") {
ta.setFont(Font.font(26));
} else if (size.getValue() == "28") {
ta.setFont(Font.font(28));
} else if (size.getValue() == "36") {
ta.setFont(Font.font(36));
} else if (size.getValue() == "48") {
ta.setFont(Font.font(48));
} else if (size.getValue() == "72") {
ta.setFont(Font.font(72));
}
if (color.getValue() == "黑色") {
ta.setStyle("-fx-text-fill:black;");
} else if (color.getValue() == "红色") {
ta.setStyle("-fx-text-fill:red;");
} else if (color.getValue() == "黄色") {
ta.setStyle("-fx-text-fill:yellow;");
} else if (color.getValue() == "绿色") {
ta.setStyle("-fx-text-fill:green;");
} else if (color.getValue() == "金色") {
ta.setStyle("-fx-text-fill:gold;");
} else if (color.getValue() == "紫色") {
ta.setStyle("-fx-text-fill:purple;");
} else if (color.getValue() == "白色") {
ta.setStyle("-fx-text-fill:white;");
} else if (color.getValue() == "蓝色") {
ta.setStyle("-fx-text-fill:blue;");
}
}
}
}