gpt4 book ai didi

java - 如何使用 fxml 元素上的放置事件? (javafx)

转载 作者:行者123 更新时间:2023-12-01 12:55:17 24 4
gpt4 key购买 nike

我启动了一个 fxml 应用程序,但我发现很难找到相关文档。我想将文件拖到我的 fxml 应用程序上,特别是选项卡上,并获取文件的位置。我是 fxml 场景的新手,所以请尽量简单。

fxml Controller

public class FXMLDocumentController implements Initializable {

@FXML protected void onDropEndEvent(ActionEvent event) {
System.out.println(event.toString());
}
@FXML protected void onDragEvent(ActionEvent event) {
System.out.println("stuff happened");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

fxml文档

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="485.0" prefWidth="577.0" style="-fx-background-color: #c4c4c4; -fx-border-radius: 0px;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="craigencdec.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button layoutX="458.0" layoutY="13.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="110.0" style="-fx-border-radius: 1 1 1 1; -fx-background-color: #4A4DFF; -fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 0); -fx-background-radius: 0 0 0 0;" text="Do Stuff" textFill="WHITE">
<font>
<Font name="Ubuntu" size="19.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<TextField layoutX="11.0" layoutY="13.0" prefHeight="45.0" prefWidth="441.0" promptText="Enter your key here" style="-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 0); -fx-border-radius: 0 0 0 0; -fx-background-radius: 0 0 0 0;">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<font>
<Font name="Ubuntu" size="19.0" />
</font>
</TextField>
<ProgressBar layoutX="332.0" layoutY="453.0" prefHeight="24.0" prefWidth="236.0" progress="0.06" style="-fx-background-radius: 0 0 0 0; -fx-border-radius: 0 0 0 0;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</ProgressBar>
<TabPane layoutY="69.0" onDragDetected="#onDragEvent" onDragDropped="#onDropEndEvent" prefHeight="416.0" prefWidth="325.0" style="-fx-background-color: #FFFFFF;" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab closable="false" text="Encode" />
<Tab closable="false" text="Decode" />
</tabs>
</TabPane>
<Label alignment="CENTER" layoutX="332.0" layoutY="70.0" prefHeight="33.0" prefWidth="236.0" style="-fx-border-radius: 3 3 3 3; -fx-background-color: #4A4DFF; -fx-background-radius: 0 0 0 0;" text="Status" textFill="WHITE">
<font>
<Font size="27.0" />
</font>
</Label>
<Circle fill="WHITE" layoutX="344.0" layoutY="120.0" radius="12.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" />
<Label alignment="TOP_LEFT" layoutX="369.0" layoutY="110.0" prefHeight="75.0" prefWidth="200.0" text="drag a file into a tab to encode or decode it" textFill="WHITE" textOverrun="CLIP" wrapText="true" />
<LineChart layoutX="333.0" layoutY="199.0" prefHeight="243.0" prefWidth="236.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis prefHeight="258.0" prefWidth="24.0" side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>

craigencdec

package craigencdec;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
*
* @author craig
*/

public class CraigEncDec extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

Scene scene = new Scene(root);
stage.setTitle("Encoder/Decoder");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);

}
private void getBits(String fileLoc){
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(""))) {
for (int b; (b = is.read()) != -1;) {
String s = "0000000" + Integer.toBinaryString(b);
s = s.substring(s.length() - 8);
sb.append(s).append(' ');
}
}catch(IOException e){
System.out.println("This is not a valid file");
}
System.out.println(sb);
}

}

最佳答案

如果您使用场景构建器,请转到代码部分,然后在“拖放时”文本字段中输入 Controller 中要在该节点上放置某些东西时处理拖动事件的方法的名称 enter image description here然后在您的 Controller 中创建此函数的主体,以便在放置发生时调用

    @FXML private void somethingDropped(Event event) {
System.out.println("something dropped!");
//TODO More useful code
}

关于java - 如何使用 fxml 元素上的放置事件? (javafx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23965900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com