gpt4 book ai didi

java - 如何使按钮事件将输出写入文本字段

转载 作者:行者123 更新时间:2023-12-02 13:07:20 24 4
gpt4 key购买 nike

如何使按钮事件将输出写入文本字段,因为按钮是在 hbox 中,另一个在文本字段中,因为我使用的是 borderpane。 在操作事件结束时,按钮应在文本字段中写入所选的文件签名这个问题的解决办法是什么?

public class Filrsystemencryption extends Application 
{

private HBox getHBox()
{
HBox hbButtons = new HBox(15);
Button btnimport = new Button("import");
TextField textfieldd = new TextField ();
btnimport.setOnAction((event) ->
{



btnimport.setOnAction(new EventHandler<ActionEvent>()
{

@Override
public void handle(ActionEvent event) {

JButton open = new JButton();
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File("C:/Users/hannah/Desktop"));
fc.setDialogTitle("choose a file");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){
textfieldd.setText("file chosen");
}
}

});

Button btnDelete = new Button("Remove");
TextField textfield = new TextField ();

final ComboBox ComboBox = new ComboBox();
ComboBox.getItems().addAll(
"Encrypt",
"Decrypt"
);

Label label = new Label("password");
hbButtons.setSpacing(30);
hbButtons.setPadding(new Insets(10, 20, 30, 20));
hbButtons.getChildren().addAll(btnimport, btnDelete,
label,textfield,ComboBox);
return hbButtons ;
}


@Override
public void start(Stage primaryStage) {


BorderPane pane = new BorderPane();
pane.setTop(getHBox());
pane.setCenter(getHBoxx());


primaryStage.setTitle("File system encryption");
Scene scene = new Scene(pane, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private TextField getHBoxx() {

TextField textfieldd = new TextField ();
textfieldd.setPrefWidth(400);
textfieldd.setPrefHeight(200);
return textfieldd;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

最佳答案

当并非真正必要时在 JavaFX 中使用 Swing 类只会导致两个平台的主线程出现问题。更好用FileChooser对于文件和目录(如本例)DirectoryChooser :

btnimport.setOnAction(evt -> {
DirectoryChooser dirChooser = new DirectoryChooser();
dirChooser.setInitialDirectory(new java.io.File("C:/Users/hannah/Desktop"));
dirChooser.setTitle("choose a file");

File choice = dirChooser.showDialog(btnimport.getScene().getWindow());

if (choice != null) {
// dialog not aborted
textfieldd.setText("file chosen");
}
});

关于java - 如何使按钮事件将输出写入文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094272/

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