互联网设计class1
第一节课的要求:使用javafx设计一个界面,能够使用enter以及组合键shift+enter发送消息,能够保存发送的消息,能够导入txt文件的内容;
代码如下:
1.SimpleFx.java
1 package chapter01; 2 3 import javafx.application.Application; 4 import javafx.event.EventHandler; 5 import javafx.geometry.Insets; 6 import javafx.geometry.Pos; 7 import javafx.scene.Scene; 8 import javafx.scene.control.Button; 9 import javafx.scene.control.Label; 10 import javafx.scene.control.TextArea; 11 import javafx.scene.control.TextField; 12 import javafx.scene.input.KeyCode; 13 import javafx.scene.input.KeyEvent; 14 import javafx.scene.layout.BorderPane; 15 import javafx.scene.layout.HBox; 16 import javafx.scene.layout.Priority; 17 import javafx.scene.layout.VBox; 18 import javafx.stage.Stage; 19 import chapter01.TextFileIO; 20 21 import java.time.LocalTime; 22 23 public class SimpleFx extends Application { 24 private Button b1 = new Button("退出"); 25 private Button b2 = new Button("发送"); 26 private Button b3 = new Button("加载"); 27 private Button b4 = new Button("保存"); 28 29 private TextField tf1 = new TextField();//信息输入 30 private TextArea ta1 = new TextArea();//显示输入的信息 31 32 public static void main(String[] args) { 33 launch(args); 34 } 35 36 @Override 37 public void start(Stage primaryStage) { 38 BorderPane bdp1 = new BorderPane(); 39 VBox vb1 = new VBox(); 40 vb1.setSpacing(5); 41 vb1.setPadding(new Insets(10,20,10,20)); 42 vb1.getChildren().addAll(new Label("信息显示区:"), 43 ta1, new Label("信息输入区:"),tf1); 44 VBox.setVgrow(ta1, Priority.ALWAYS); 45 bdp1.setCenter(vb1); 46 ta1.setEditable(false); 47 48 HBox hb1 = new HBox(); 49 hb1.setSpacing(10); 50 hb1.setPadding(new Insets(10,20,10,20)); 51 hb1.setAlignment(Pos.CENTER); 52 hb1.getChildren().addAll(b1,b2,b3,b4); 53 54 bdp1.setBottom(hb1); 55 Scene sc1 = new Scene(bdp1,700,400); 56 primaryStage.setScene(sc1); 57 primaryStage.show(); 58 59 //setonaction 60 b1.setOnAction(e->System.exit(0));//退出 61 b2.setOnAction(e->{//保存 62 String msg = tf1.getText(); 63 ta1.appendText(msg + "\n"); 64 tf1.clear(); 65 }); 66 //回车响应 67 tf1.setOnKeyPressed(new EventHandler<KeyEvent>() { 68 @Override 69 public void handle(KeyEvent event) { 70 //shift+enter 71 if (event.getCode() == KeyCode.ENTER && event.isShiftDown() && !tf1.getText().isEmpty()){ 72 String msg = tf1.getText(); 73 ta1.appendText("echo: "+msg + "\n"); 74 tf1.clear(); 75 } 76 //回车且非空 77 else if (event.getCode() == KeyCode.ENTER && !tf1.getText().isEmpty()){ 78 String msg = tf1.getText(); 79 ta1.appendText(msg + "\n"); 80 tf1.clear(); 81 } 82 } 83 }); 84 85 //文件读写 86 TextFileIO textFileIO = new TextFileIO(); 87 88 b3.setOnAction(e->{//加载 89 String msg = textFileIO.load(); 90 // System.out.println("msg: "+msg); 91 if (msg != null){ 92 ta1.clear(); 93 ta1.setText(msg); 94 } 95 }); 96 97 b4.setOnAction(e->{//写 98 textFileIO.append( 99 LocalTime.now().withNano(0)+" "+ta1.getText() 100 );101 } 102 }
2.TextFileIO.java
1 package chapter01; 2 3 import javafx.stage.FileChooser; 4 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.OutputStreamWriter; 8 import java.io.PrintWriter; 9 import java.util.Scanner; 10 11 public class TextFileIO { 12 private PrintWriter pw; 13 private Scanner sc; 14 15 //构造函数 16 public TextFileIO(){ 17 }; 18 19 //写 20 public void append(String msg){ 21 FileChooser fc1 = new FileChooser(); 22 File f1 = fc1.showSaveDialog(null); 23 if (f1 == null) 24 return; 25 try{ 26 pw = new PrintWriter( 27 new OutputStreamWriter( 28 new FileOutputStream(f1,true),"utf-8" 29 )); 30 pw.println(msg); 31 } catch (Exception e){ 32 e.printStackTrace(); 33 } finally { 34 pw.close(); 35 } 36 } 37 38 //读 39 public String load(){ 40 FileChooser fc2 = new FileChooser(); 41 File f2 = fc2.showOpenDialog(null); 42 if (f2 == null) 43 return null; 44 StringBuilder sb = new StringBuilder(); 45 try { 46 sc = new Scanner(f2,"utf-8"); 47 while (sc.hasNext()){ 48 sb.append(sc.nextLine()+"\n"); 49 } 50 } catch (Exception e){ 51 e.printStackTrace(); 52 } finally { 53 sc.close(); 54 } 55 return sb.toString(); 56 }