设置UI内容仅添加最后一行

时间:2013-02-03 17:59:31

标签: java user-interface

基本上我在这里做的是将文本文件的内容导入到用户输入字段中(其中有7个。我运行一个循环来通过它们获取命令[i]。代码在控制台中正常工作,但它不适用于GUI,因为它只输入每个字段的最后一行。你有什么问题是什么吗?这是UI:

http://imageshack.us/f/692/21778853.jpg

正如您所看到的,它可以很好地打印到控制台中,但是当我通过在GUI上单击导入来导入数据时,它只是放置txt文件的最后一个命令

我的代码是:

    final JTextField Command[];//Create one dimensional array 
    Command = new JTextField[8];//Declare Command as a JTextField 
    for (int i = 1; i < 8; i++)//Run the loop through each selection 
    { 
        Command[i] = new JTextField(i);//Read through each text field
    }
    //Add user input fields including their absolute position on the panel
    Command[1].setText("Please enter the commands...");//Starting text
    Command[1].setBounds(121, 13, 236, 25);//Set the position of the text field
    getContentPane().add(Command[1]);//Add this text field to the content
            Command[2].setBounds(121, 47, 236, 25);/fsdfsdf
    getContentPane().add(Command[2]);

    Command[3].setBounds(121, 83, 236, 25);/fasdfasd
    getContentPane().add(Command[3]);

    Command[4].setBounds(121, 119, 236, 25);/fsfasdasdf
    getContentPane().add(Command[4]);

    Command[5].setBounds(121, 155, 236, 25);
    getContentPane().add(Command[5]);

    Command[6].setBounds(121, 191, 236, 25);
    getContentPane().add(Command[6]);

    Command[7].setBounds(121, 227, 236, 25);
    getContentPane().add(Command[7]);  

            //IMPORT FILE   
    JMenuItem Import = new JMenuItem("Import");
    File.add(Import);
    Import.addActionListener(new ActionListener() {//Call up ActionListener function
        public void actionPerformed(ActionEvent arg0) {//Event handler
            try {
                //use buffering, reading one line at a time
                //FileReader always assumes default encoding is OK!
                BufferedReader input = new BufferedReader(new FileReader("W:\\EclipsePortable\\Data\\workspace2\\" +
                                        "RobotController\\src\\RobotController\\Import commands.txt"));
                try {
                    String line = null; //not declared within while loop
                    while (( line = input.readLine()) != null) {
                        System.out.println(line);//Print the lines
                        for(int i = 1; i < 8; i++) {
                        Command[i].setText(line);
                    }
                }}
                finally {
                    input.close();
                }
            }
            catch (IOException ex){
                ex.printStackTrace();
            }
    }});`

1 个答案:

答案 0 :(得分:1)

所以,在您的文件导入代码中,您正在执行此操作...

for(int i = 1; i < 8; i++) {
    Command[i].setText(line);

将当前文本行应用于所有字段(所有字段的同一行文本),这意味着当您完成文件读取时,您只获得文件的最后一行。

相反,我会保持对字段索引的引用和每行读取的增量

添加了示例

                String line = null; //not declared within while loop
                int fieldIndex = 0;
                while (( line = input.readLine()) != null) {
                    System.out.println(line);//Print 
                    Command[fieldIndex].setText(line);
                    fieldIndex++
                    if (fieldIndex > 7) {
                        break;
                    }