Java 如何使按钮事件在文本字段中写入输出
如何使按钮事件在文本字段中写入输出,因为按钮是 在hbox中,另一个在文本字段中,因为我使用的是borderpane。 在操作事件结束时,按钮应在文本字段中写入choosen文件签名 这个问题的解决办法是什么Java 如何使按钮事件在文本字段中写入输出,java,javafx,event-handling,addeventlistener,hbox,Java,Javafx,Event Handling,Addeventlistener,Hbox,如何使按钮事件在文本字段中写入输出,因为按钮是 在hbox中,另一个在文本字段中,因为我使用的是borderpane。 在操作事件结束时,按钮应在文本字段中写入choosen文件签名 这个问题的解决办法是什么 public class Filrsystemencryption extends Application { private HBox getHBox() { HBox hbButtons = new HBox(15); Button btnimport = new But
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);
}
}
公共类Filrsystemencryption扩展了应用程序
{
私有HBox getHBox()
{
HBox按钮=新的HBox(15);
按钮btnimport=新按钮(“导入”);
TextField TextField=新的TextField();
btnimport.setOnAction((事件)->
{
setOnAction(新的EventHandler()
{
@凌驾
公共无效句柄(ActionEvent事件){
JButton open=新JButton();
JFileChooser fc=新的JFileChooser();
setCurrentDirectory(新的java.io.File(“C:/Users/hannah/Desktop”);
fc.setDialogTitle(“选择文件”);
fc.setFileSelectionMode(仅限JFileChooser.DIRECTORIES_);
如果(fc.showOpenDialog(打开)=JFileChooser.APPROVE\u选项){
textfield.setText(“选择的文件”);
}
}
});
按钮btnDelete=新按钮(“删除”);
TextField TextField=新的TextField();
最终组合框ComboBox=新组合框();
ComboBox.getItems().addAll(
“加密”,
“解密”
);
标签=新标签(“密码”);
HB按钮。设置间隔(30);
hbButtons.设置填充(新插图(10,20,30,20));
hbButtons.getChildren().addAll(btnimport、btnDelete、,
标签、文本字段、组合框);
返回按钮;
}
@凌驾
公共无效开始(阶段primaryStage){
BorderPane=新的BorderPane();
setTop(getHBox());
setCenter(getHBoxx());
setTitle(“文件系统加密”);
场景=新场景(窗格,600600);
初级阶段。场景(场景);
primaryStage.show();
}
私有文本字段getHBoxx(){
TextField TextField=新的TextField();
textfield.setPrefWidth(400);
textfield.setPrefHeight(200);
返回textfield;
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
}
在JavaFX中使用Swing类(实际上不需要)只会导致两种平台的主线程出现问题。更好地将其用于文件和目录(如本例所示):
为什么要将Button和JFileChooser等Swing类与此应用程序中的JavaFX类混合使用?这看起来是错误的。下面是如何对您使用该技术发布的代码或适用于源代码编辑器的代码执行此操作。我应该在何时编写此函数?只需替换您自己损坏的
setOnAction
部分即可(尝试使用naomymus类和lambda表达式)和我的代码。
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");
}
});