JavaFX 编程语言开发互联网应用程序(RIA)
执行入口:
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{
// 加载xml文件进行 Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("配置中心"); primaryStage.setScene(new Scene(root, 860, 605)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
View层 FXML文件:
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.String?> <?import javafx.collections.FXCollections?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.ComboBox?> <?import javafx.scene.control.DatePicker?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Tab?> <?import javafx.scene.control.TabPane?> <?import javafx.scene.control.TextArea?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.shape.Arc?> <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="465.0" prefWidth="737.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"
//绑定Controller,完整的包路径
fx:controller="sample.SampleController"> <tabs> <Tab text="服务配置"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <Label layoutX="29.0" layoutY="69.0" prefHeight="14.0" prefWidth="90.0" text="服务IP" /> <Label layoutX="38.0" layoutY="107.0" text="端口" /> // fx:绑定Controller的属性
<TextField fx:id="hostName" layoutX="80.0" layoutY="65.0" prefHeight="22.0" prefWidth="290.0" /> <TextField fx:id="hostPort" layoutX="80.0" layoutY="103.0" prefHeight="22.0" prefWidth="290.0" /> <Label layoutX="27.0" layoutY="31.0" text="http协议" /> <ComboBox fx:id="prototypeId" layoutX="80.0" layoutY="27.0" prefHeight="22.0" prefWidth="290.0"> <items> <FXCollections fx:factory="observableArrayList"> <String fx:value="https" /> <String fx:value="http" /> </FXCollections> </items> <value> <String fx:value="https" /> </value> </ComboBox> <Arc fill="#e8ff1e" layoutX="680.0" layoutY="369.0" length="270.0" radiusX="70.0" radiusY="53.0" startAngle="45.0" stroke="BLACK" strokeType="INSIDE" type="ROUND" /> </children> </AnchorPane> </content> </Tab> <Tab text="配置"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="261.0" prefWidth="453.0"> <children>
// 按钮绑定对应Controller对应的事件,
<Button id="btn_1" layoutX="30.0" layoutY="3.0" mnemonicParsing="false" onAction="#genRsaEvent" text="生成" textAlignment="CENTER" /> <Button layoutX="99.0" layoutY="3.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="22.0" prefWidth="64.0" text="保存" /> <TextField fx:id="securityKey" layoutX="98.0" layoutY="38.0" prefHeight="22.0" prefWidth="591.0" /> <Label layoutX="27.0" layoutY="42.0" prefHeight="14.0" prefWidth="78.0" text="关键字" /> <Label layoutX="37.0" layoutY="78.0" text="有效日期" /> <DatePicker fx:id="startTime" layoutX="99.0" layoutY="74.0" prefHeight="22.0" prefWidth="277.0" /> <DatePicker fx:id="endTime" layoutX="409.0" layoutY="74.0" prefHeight="22.0" prefWidth="280.0" /> <Label layoutX="388.0" layoutY="78.0" text="至" /> <Label layoutX="37.0" layoutY="121.0" text="内容01" /> <TextArea fx:id="privateKeyId" layoutX="98.0" layoutY="121.0" prefHeight="172.0" prefWidth="590.0" wrapText="true" /> <Label layoutX="37.0" layoutY="304.0" text="内容02" /> <TextArea fx:id="publicKeyId" layoutX="97.0" layoutY="304.0" prefHeight="104.0" prefWidth="590.0" wrapText="true" /> </children> </AnchorPane> </content> </Tab> <Tab text="黑名单"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> </content> </Tab> </tabs> </TabPane>
控制层:
import java.net.URL; import java.time.LocalDate; import java.util.Map; import java.util.ResourceBundle; public class SampleController implements Initializable { private String securityUrl="/third/app/security/syn"; // 空间的元素ID @FXML private ComboBox prototypeId; @FXML private TextField hostName; @FXML private TextField hostPort; @FXML private TextArea privateKeyId; @FXML private TextArea publicKeyId; @FXML private TextField securityKey; @FXML private DatePicker startTime; @FXML private DatePicker endTime; //事件方法 @FXML private void genRsaEvent(ActionEvent event){ Map<String,String> securityMap = StorgeUtils.getKeyInfo(); String privateKeyContent = securityMap.get("private"); String publicKeyContent = securityMap.get("public"); privateKeyId.setText(privateKeyContent); publicKeyId.setText(publicKeyContent); } @FXML private void handleButtonAction(javafx.event.ActionEvent event) { StringBuilder httpReqUrl = new StringBuilder(); httpReqUrl.append(prototypeId.getValue()).append("://").append(hostName.getText()).append(":").append(hostPort.getText()); httpReqUrl.append(securityUrl); System.out.println(httpReqUrl.toString()); System.out.println(securityKey.getText()); System.out.println(privateKeyId.getText()); System.out.println(publicKeyId.getText()); System.out.println(startTime.getValue()); System.out.println(endTime.getValue()); new AlertBox().display("信息提醒", "保存成功!"); } @Override public void initialize(URL location, ResourceBundle resources) { }