javafx窗口设计
javafx
package chapter01;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.time.LocalDateTime;
public class javafx extends Application {
// private Button
private Button btnExit = new Button("退出");
private Button btnSend = new Button("发送");
private Button btnOpen = new Button("加载");
private Button btnSave = new Button("保存");
//待发送信息的文本框
private TextField tfSend = new TextField();
//显示信息的文本区域
private TextArea taDisplay = new TextArea();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TextFileIO textFileIO = new TextFileIO();
// BorderPane
BorderPane mainPane = new BorderPane();
//内容显示区域
VBox vBox = new VBox();
vBox.setSpacing(10);//各控件之间的间隔
//VBox 面板中的内容距离四周的留空区域
vBox.setPadding(new Insets(10,20,10,20));
vBox.getChildren().addAll(new Label("信息显示区:"),taDisplay,new Label("信息输入区:"), tfSend);
//设置显示信息区的文本区域可以纵向自动扩充范围
VBox.setVgrow(taDisplay, Priority.ALWAYS);
mainPane.setCenter(vBox);
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(10,20,10,20));
hBox.setAlignment(Pos.CENTER_RIGHT);
hBox.getChildren().addAll(btnSend,btnSave,btnOpen,btnExit);
mainPane.setBottom(hBox);
Scene scene = new Scene(mainPane,700,400);
primaryStage.setScene(scene);
primaryStage.show();
//--------事件处理代码部分--
//taDisplay 控件,设置为只读和自动换行
taDisplay.setWrapText(true);
taDisplay.setEditable(false);
//退出按钮
btnExit.setOnAction(event -> {System.exit(0);});
//发送按钮
btnSend.setOnAction(event -> {
String msg = tfSend.getText();
taDisplay.appendText(msg + "\n");
tfSend.clear();
});
//添加文本框 tfSend 的回车响应功能
//输完内容,回车即可将信
//息添加到信息显示区;如果是 Shift+回车,则在信息前加上 echo: 的信息头再
//发送
tfSend.setOnKeyPressed(event ->{
if(event.getCode()== KeyCode.ENTER){
if (event.isShiftDown()) {
String msg=tfSend.getText();
taDisplay.appendText("echo:"+msg+"\n");
tfSend.clear();
}
else{
String msg=tfSend.getText();
taDisplay.appendText(msg+"\n");
tfSend.clear();
}
}
});
//保存
btnSave.setOnAction(event -> {
//添加当前时间信息进行保存
textFileIO.append(
LocalDateTime.now().withNano(0) +" "+ taDisplay.getText());
});
//加载
btnOpen.setOnAction(event -> {
String msg = textFileIO.load();
if(msg != null) {
taDisplay.clear();
taDisplay.setText(msg);
}
});
}
}
TextFileIO.java类
package chapter01;
import javafx.stage.FileChooser;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class TextFileIO {
private PrintWriter pw;
private Scanner sc;
public TextFileIO(){}
//内容添加到文件中,文件通过对话框来确定
public void append(String msg){
FileChooser fileChooser=new FileChooser();
File file=fileChooser.showSaveDialog(null);
if(file==null) return;
try{
//以追加模式 utf-8 的编码模式写到文件中
pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), "utf-8"));
}catch(Exception e){
e.printStackTrace();
}finally {
pw.close();
}
}
public String load() {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(null);
if(file == null) //用户放弃操作则返回
return null;
StringBuilder sb = new StringBuilder();
try {
//读和写的编码要注意保持一致
sc = new Scanner(file,"utf-8");
while (sc.hasNext()) {
sb.append(sc.nextLine() + "\n"); //补上行读取的行末尾回车
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
return sb.toString();
}
}