String 如何将按钮的值传递给字符串,以便以后在java中使用它?
我正在编写一个程序,需要一个按钮来获取文件的位置(filepath)。现在,在选择文件之后,我想将该值传递给一个字符串,稍后我可以在下面的代码中引用该字符串。你能告诉我怎么做吗 这是按钮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<
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。那么,我们是否可以引用它以供以后使用呢?为什么不在按钮的设置动作
之外声明StringBuilderwmark
,并在其内部附加值
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());