2017-05-21 53 views
0

如何使按钮事件写在一个文本框的输出,因为按钮处于横向盒 和其他在一个文本框,因为我使用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); 
    } 

    } 
+0

为什么像巴顿和JFileChooser所用JavaFX类在此应用程序混合Swing类?这看起来错了。 – jewelsea

+0

下面是如何[使用格式代码的想法(https://www.jetbrains.com/help/idea/2017.1/reformatting-source-code.html),尝试这样做,对你的代码张贴使用这种技术或一个适用于你的源代码编辑器。 – jewelsea

回答

1

在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"); 
    } 
}); 
+0

我Wher应该写这个功能? –

+1

只需更换自己的破'setOnAction'部分(在您尝试使用naomymus类和lambda表达式),我的代码。 – fabian

相关问题