Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
User interface javafx按钮,用于从txt读取到文本字段的行_User Interface_Button_Javafx_Textfield_Settext - Fatal编程技术网

User interface javafx按钮,用于从txt读取到文本字段的行

User interface javafx按钮,用于从txt读取到文本字段的行,user-interface,button,javafx,textfield,settext,User Interface,Button,Javafx,Textfield,Settext,我正在创建一个用于打开和关闭函数的文件对象。 现在,我指向一个特定的位置,使用一个固定的名称。文件中填充了数据行。 按钮在窗格上,我有一个openContact函数,它应该逐行读取文本文件,并将结果发送到文本字段setText方法,当您单击按钮时调用此函数。 编辑器中没有语法错误,但单击按钮不会填充GUI中的字段。 除此之外,我不确定该问什么问题或搜索什么。 我附上我的代码,因为它是。 如有任何关于适当问题的提示或指导或思考过程,将不胜感激 package programmingassignme

我正在创建一个用于打开和关闭函数的文件对象。 现在,我指向一个特定的位置,使用一个固定的名称。文件中填充了数据行。 按钮在窗格上,我有一个openContact函数,它应该逐行读取文本文件,并将结果发送到文本字段setText方法,当您单击按钮时调用此函数。 编辑器中没有语法错误,但单击按钮不会填充GUI中的字段。 除此之外,我不确定该问什么问题或搜索什么。 我附上我的代码,因为它是。 如有任何关于适当问题的提示或指导或思考过程,将不胜感激

package programmingassignment1;

import java.awt.Image;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.*;
//import javafx.scene.layout.StackPane;
//import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.*; //input/output
import java.util.Scanner;
//import java.util.*; //scanner, user input
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;

public class Address extends Application {
    String contactFirst,
           contactLast,
           spouseFirst,
           spouseLast,
           street,
           city,
           state,
           zip;
    TextField tf_contactFirst = new TextField();
    TextField tf_contactLast = new TextField();
    TextField tf_spouseFirst = new TextField();
    TextField tf_spouseLast = new TextField(); 
    TextField tf_street = new TextField();
    TextField tf_city = new TextField();
    TextField tf_state = new TextField();
    TextField tf_zip = new TextField();
    TextArea ta_notes = new TextArea();

    ExtensionFilter jpgExtension = new ExtensionFilter("JPG", "*.jpg");
    ExtensionFilter pngExtension = new ExtensionFilter("PNG", "*.png");
    ExtensionFilter allExtension = new ExtensionFilter("ALL", "*.*");
    Rectangle imageBox = new Rectangle(10, 0, 10, 20);
    FileChooser fc = new FileChooser();
    @Override
    public void start(Stage primaryStage){
        //modify text area and register actions
        ta_notes.setWrapText(true);
        ta_notes.setEditable(true);
        ta_notes.setPrefColumnCount(12);
        ta_notes.setPrefRowCount(3);

//Setting an action for the Clear button       
        Button bt_cancel = new Button("Cancel");
        bt_cancel.setOnAction(e -> {
            tf_contactFirst.clear();
            tf_contactLast.clear();
            tf_spouseFirst.clear();
            tf_spouseLast.clear();
            tf_street.clear();
            tf_city.clear();
            tf_state.clear();
            tf_zip.clear();

            ta_notes.setText(null);
    });

//Setting an action for the Open Contact  button       
        Button bt_openContact = new Button("Open Contact");
        File file = new File("AddressBook.txt");
        bt_openContact.setOnAction(e -> {
            new EventHandler<ActionEvent>(){
                @Override
                public void handle(ActionEvent e){
                    try{openContact(file);}
                    catch(Exception f){f.getMessage();}
                }
            };
        });
//Setting an action for the Save button        
        Button bt_save = new Button("Save");

        bt_save.setOnAction(
            new EventHandler<ActionEvent>(){
                @Override
                public void handle(ActionEvent e){
                    try{saveContact(file);}
                    catch(Exception f){f.getMessage();}
        }});

        RadioButton rb_male = new RadioButton("Male");
        RadioButton rb_female = new RadioButton("Female");

        ToggleGroup tgrp = new ToggleGroup();
        rb_male.setToggleGroup(tgrp);
        rb_female.setToggleGroup(tgrp);

        rb_male.setOnAction(e -> {
            if(rb_male.isSelected()){int maleContact = 1;}
        });
        rb_female.setOnAction(e -> {
            if(rb_female.isSelected()){int maleContact = 0;}
        });

        //create combo box and add items as an observable list
        String[] x = {"Home Address", "Work Address"};
        ComboBox cbo = new ComboBox(FXCollections.observableArrayList(x));
        //cbo.setEditable(false);
        cbo.setValue("Home Address");

//        cbo.setOnAction(e -> {/**____________***/;});
        //set imageBox rectangle action
        //click in it, choose image, file, its displayed?
        //fc is an import or not?
        //setOnMouseClicked should work for any node or scene, why not this rect
        /*imageBox.setOnMouseClicked((MouseEvent e) -> {
            fc.setTitle("Open Image File");
            fc.setInitialDirectory(new File("."));
            fc.getExtensionFilters().addAll(jpgExtension, pngExtension, allExtension);
            fc.setSelectedExtensionFilter(jpgExtension);

            File picture = fc.showOpenDialog(primaryStage);
            if (picture != null){
                rootPane.getChildren().remove(imageBox);
                contact.setImageFile(picture.getName());

                Image userImage = new Image(picture.getName());
                ImageView userView = new ImageView(userImage);

                rootPane.getChildren().add(userView);
            }
        });*/
        GridPane rootPane = new GridPane();        

        rootPane.add(new Label("First Name"), 1, 1);
        rootPane.add(tf_contactFirst, 1, 2);
        rootPane.add(new Label("Last Name"), 2, 1);
        rootPane.add(tf_contactLast, 2, 2);
        rootPane.add(new Label("Sex"), 3, 1);
        rootPane.add(rb_female, 3, 2);
        rootPane.add(rb_male, 3, 3);       
        rootPane.add(new Label("Spouse's First Name"), 1, 4);
        rootPane.add(tf_spouseFirst, 1, 5);
        rootPane.add(new Label("Spouse's Last Name"), 2, 4);
        rootPane.add(tf_spouseLast, 2, 5);
        rootPane.add(cbo, 1, 6);
        rootPane.add(new Label("Address Street"), 1, 7);
        rootPane.add(tf_street, 1, 8);
        rootPane.add(new Label("City"), 1, 9);
        rootPane.add(tf_city, 1, 10);
        rootPane.add(new Label("State"), 2, 9);
        rootPane.add(tf_state, 2, 10);
        rootPane.add(new Label("Zip Code"), 3, 9);
        rootPane.add(tf_zip, 3, 10);
        rootPane.add(imageBox, 4, 1 );

        //Label label = new Label();
        rootPane.add(new Label("Notes"), 1, 11);
        rootPane.add(ta_notes, 1, 12);

        rootPane.add(bt_cancel, 2, 13);
        rootPane.add(bt_save, 3, 13);
        rootPane.add(bt_openContact, 1, 13);

        //scene = window (isn't it just easier if someon mentions that?)
        Scene scene = new Scene(rootPane, 1000, 500);

        primaryStage.setTitle("Address Book");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
            launch(args);
    }
    public void saveContact(File file) throws FileNotFoundException, Exception{ //declaration
                //this code might cause a FileNotFoundException
                //if it does it creates an exception object of the above type
        try{
                    //PrintWriter output = new PrintWriter (file);
                    PrintStream output = new PrintStream(file);
                    output.println(tf_contactFirst.getText());
                    output.println(tf_contactLast.getText());
                    output.println(tf_spouseFirst.getText());
                    output.println(tf_spouseLast.getText());
                    output.println(tf_street.getText());
                    output.println(tf_city.getText());
                    output.println(tf_state.getText());
                    output.println(tf_zip.getText());
                    output.close();
                }
                //what do do with exception
                //here the catch clause with create another exception
                //that is passed the result of the getMessage() method from the original exception
        catch(FileNotFoundException e){
                    throw new Exception(e.getMessage());
                }
    }

//read same text file you save too
    public void openContact (File file) throws FileNotFoundException, Exception{
        try{
            Scanner read = new Scanner(file);     
            while(read.hasNextLine()){
                //how is a blank field recognized, how are two or three
                //consecutive tokens handled
                //how do I save the imageFileName
                 tf_contactFirst.setText(read.nextLine());
                 tf_contactLast.setText(read.nextLine());
                 //tf_contactGender.setText(read.nextLine());
                 tf_spouseFirst.setText(read.nextLine());
                 tf_spouseLast.setText(read.nextLine());
                 //tf_spouse_gender.setText(read.nextLine());
                 tf_street.setText(read.nextLine());
                 tf_city.setText(read.nextLine());
                 tf_state.setText(read.nextLine());
                 tf_zip.setText(read.nextLine());
                 //ta_notes.setText(read.nextLine());
            }
        }
        catch(FileNotFoundException e){
                    throw new Exception(e.getMessage());
        }
    }
}
package programmingassignment1;
导入java.awt.Image;
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.RadioButton;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.ToggleGroup;
导入javafx.scene.layout.*;
//导入javafx.scene.layout.StackPane;
//导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
导入java.io.*//输入/输出
导入java.util.Scanner;
//导入java.util.*//扫描仪,用户输入
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.image.ImageView;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.FileChooser;
导入javafx.stage.FileChooser.ExtensionFilter;
公共类地址扩展应用程序{
首先,
最后一次接触,
首先,
喷鼻糖,
大街
城市
状态
拉链
TextField tf_contactFirst=新建TextField();
TextField tf_contactLast=新建TextField();
TextField tf_speosefirst=新TextField();
TextField tf_spooselast=新TextField();
TextField tf_street=新TextField();
TextField tf_city=新TextField();
textf_state=新TextField();
textf_zip=new TextField();
TextArea ta_notes=新TextArea();
ExtensionFilter jpgExtension=新的ExtensionFilter(“JPG”,“*.JPG”);
ExtensionFilter pngExtension=新的ExtensionFilter(“PNG”,“*.PNG”);
ExtensionFilter allExtension=新的ExtensionFilter(“全部”,“**”);
矩形imageBox=新矩形(10,0,10,20);
FileChooser fc=新建FileChooser();
@凌驾
公共无效开始(阶段primaryStage){
//修改文本区域并注册操作
tau notes.setWrapText(真);
ta_notes.setEditable(真);
ta_notes.setPrefColumnCount(12);
ta_notes.设置预行计数(3);
//设置清除按钮的操作
按钮bt_cancel=新按钮(“取消”);
bt_取消设置操作(e->{
tf_contactFirst.clear();
tf_contactLast.clear();
tf_mopesefirst.clear();
tf_喷咀last.clear();
特福街,无障碍;
tf_city.clear();
tf_state.clear();
tf_zip.clear();
tau notes.setText(空);
});
//设置“打开触点”按钮的操作
按钮bt_openContact=新按钮(“打开触点”);
File File=新文件(“AddressBook.txt”);
bt_openContact.setOnAction(e->{
新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
请尝试{openContact(文件);}
catch(异常f){f.getMessage();}
}
};
});
//设置“保存”按钮的操作
按钮bt_保存=新按钮(“保存”);
bt_save.setOnAction(
新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
请尝试{saveContact(file);}
catch(异常f){f.getMessage();}
}});
RadioButton rb_male=新的RadioButton(“male”);
RadioButton rb_内螺纹=新的RadioButton(“内螺纹”);
ToggleGroup tgrp=新的ToggleGroup();
rb_雄性设置开关组(tgrp);
rb_雌性组(tgrp);
rb_男性坐姿动作(e->{
如果(rb_male.isSelected()){int malecontch=1;}
});
rb_女性坐姿动作(e->{
如果(rb_female.isSelected()){int malestact=0;}
});
//创建组合框并将项目添加为可观察列表
字符串[]x={“家庭地址”,“工作地址”};
ComboBox cbo=新的ComboBox(FXCollections.observableArrayList(x));
//cbo.setEditable(false);
cbo.设置值(“家庭地址”);
//cbo.setOnAction(e->{/**{/**};
//设置imageBox矩形操作
//点击它,选择图像,文件,其显示?
//fc是否为导入?
//setOnMouseClicked应该适用于任何节点或场景,为什么不使用此矩形
/*imageBox.setOnMouseClicked((MouseeEvent e)->{
fc.setTitle(“打开图像文件”);
fc.setInitialDirectory(新文件(“.”);
fc.getExtensionFilters().addAll(jpgExtension、pngExtension、allExtension);
fc.设置SelectedExtensionFilter(jpgExtension);
File picture=fc.showOpenDialog(primaryStage);
如果(图片!=null){
rootPane.getChildren().remove(imageBox);
contact.setImageFile(picture.getName());
Image userImage=新图像(picture.getName());
ImageView用户视图=新的ImageView(用户图像);
rootPane.getChildren().add(userView);
}
});*/
GridPane rootPane=新建GridPane();
添加(新标签(“名字”),1,1);
添加(tf_contactFirst,1,2);
添加(新标签(“姓氏”),2
    bt_openContact.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            try {
                openContact(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    bt_openContact.setOnAction(event -> {
        try {
            openContact(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });