Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何将按钮的值传递给字符串,以便以后在java中使用它?_String_Button_Javafx - Fatal编程技术网

String 如何将按钮的值传递给字符串,以便以后在java中使用它?

String 如何将按钮的值传递给字符串,以便以后在java中使用它?,string,button,javafx,String,Button,Javafx,我正在编写一个程序,需要一个按钮来获取文件的位置(filepath)。现在,在选择文件之后,我想将该值传递给一个字符串,稍后我可以在下面的代码中引用该字符串。你能告诉我怎么做吗 这是按钮 Label labelWatermark = new Label(); final String wmark = new String(); Button btnWmark = new Button("Watermark?"); btnWmark.setOnAction(new EventHandler<

我正在编写一个程序,需要一个按钮来获取文件的位置(filepath)。现在,在选择文件之后,我想将该值传递给一个字符串,稍后我可以在下面的代码中引用该字符串。你能告诉我怎么做吗

这是按钮

Label labelWatermark = new Label();
final String wmark = new String();
Button btnWmark = new Button("Watermark?");
btnWmark.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent e) {
        FileChooser wmarkFile = new FileChooser();
        fileWmark =  wmarkFile.showOpenDialog(primaryStage);
        String filePathWmark = fileWmark.getAbsolutePath();
        labelWatermark.setText(filePathWmark);
        final String wmark = new String(filePathWmark);
    }
});

HBox hbBtnBrowse3 = new HBox(10);
hbBtnBrowse3.setAlignment(Pos.CENTER);
hbBtnBrowse3.getChildren().addAll(btnWmark, labelWatermark);
grid.add(hbBtnBrowse3, 1, 5);

我认为,因为字符串
wmark
包含在按钮
btnWmark
中,所以我无法传递字符串wmark。那么,我们是否可以引用它以供以后使用呢?

为什么不在按钮的
设置动作
之外声明StringBuilder
wmark
,并在其内部附加值

Label labelWatermark = new Label();
Button btnWmark = new Button("Watermark?");
final StringBuilder wmark = new StringBuilder();
btnWmark.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent e) {
        FileChooser wmarkFile = new FileChooser();
        fileWmark =  wmarkFile.showOpenDialog(primaryStage);
        String filePathWmark = fileWmark.getAbsolutePath();
        labelWatermark.setText(filePathWmark);
        wmark.append(filePathWmark);
    }
});

但是,我认为我们必须注释掉行
final String wmark=new String()Label labelWatermark = new Label();
Button btnWmark = new Button("Watermark?");
final StringBuilder wmark = new StringBuilder();
btnWmark.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent e) {
        FileChooser wmarkFile = new FileChooser();
        fileWmark =  wmarkFile.showOpenDialog(primaryStage);
        String filePathWmark = fileWmark.getAbsolutePath();
        labelWatermark.setText(filePathWmark);
        wmark.append(filePathWmark);
    }
});
Image watermark_image = Image.getInstance(wmark.toString());