Java 设置UI内容只会添加最后一行

Java 设置UI内容只会添加最后一行,java,user-interface,Java,User Interface,基本上,我在这里做的是将文本文件的内容导入到用户输入字段中(共有7个字段)。我对命令[I]运行了一个循环来遍历这些字段。代码在控制台中运行良好,但对GUI不起作用,因为它只为每个字段输入最后一行。您知道问题出在哪里吗?以下是UI: final JTextField Command[];//Create one dimensional array Command = new JTextField[8];//Declare Command as a JTextField

基本上,我在这里做的是将文本文件的内容导入到用户输入字段中(共有7个字段)。我对命令[I]运行了一个循环来遍历这些字段。代码在控制台中运行良好,但对GUI不起作用,因为它只为每个字段输入最后一行。您知道问题出在哪里吗?以下是UI:

    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();
            }
    }});`

    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();
            }
    }});`
如您所见,它可以很好地打印到控制台中,但是当我通过在GUI上单击import导入数据时,它只会放入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();
            }
    }});`
我的代码是:

    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();
            }
    }});`
final JTextField命令[];//创建一维数组
Command=newjtextfield[8];//将命令声明为JTextField
for(int i=1;i<8;i++)//在每个选择中运行循环
{ 
Command[i]=newjtextfield(i);//通读每个文本字段
}
//添加用户输入字段,包括其在面板上的绝对位置
命令[1].setText(“请输入命令…”);//开始文本
命令[1]。setBounds(121、13、236、25);//设置文本字段的位置
getContentPane().add(命令[1]);//将此文本字段添加到内容
命令[2].setBounds(121,47,236,25);/fsdfsdf
getContentPane().add(命令[2]);
命令[3].setBounds(121,83,236,25);/fasdfasd
getContentPane().add(命令[3]);
命令[4].setBounds(121、119、236、25);/fsfasdasdf
getContentPane().add(命令[4]);
命令[5]。设置边界(121、155、236、25);
getContentPane().add(命令[5]);
命令[6].setBounds(12119123625);
getContentPane().add(命令[6]);
命令[7]。设置边界(121、227、236、25);
getContentPane().add(命令[7]);
//导入文件
JMenuItem导入=新的JMenuItem(“导入”);
添加(导入);
Import.addActionListener(新ActionListener(){//调用ActionListener函数
public void actionPerformed(ActionEvent arg0){//事件处理程序
试一试{
//使用缓冲,一次读取一行
//FileReader始终假定默认编码是正确的!
BufferedReader输入=新的BufferedReader(新文件读取器(“W:\\EclipsePortable\\Data\\workspace2\\”+
“RobotController\\src\\RobotController\\Import commands.txt”);
试一试{
String line=null;//未在while循环中声明
而((line=input.readLine())!=null){
System.out.println(行);//打印行
对于(int i=1;i<8;i++){
命令[i].setText(行);
}
}}
最后{
input.close();
}
}
捕获(IOEX异常){
例如printStackTrace();
}
}});`

因此,在文件导入代码中,您正在执行以下操作

    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();
            }
    }});`
for(int i = 1; i < 8; i++) {
    Command[i].setText(line);

你想要什么行为?你想要第一个输入包含第一行,第二个输入-第二行ant等?也请以小写字母开始变量名。在Java中用大写字母很难阅读。非常感谢!这对我来说非常有效,尽管我必须输入fieldIndex=fieldIndex+1,而不是fieldIndex++,因为它不起作用出于某种原因;再次感谢:)这很奇怪,fieldIndex++是fieldIndex=fieldIndex+1或fieldIndex+=1的简写。。我认为这与我使用的软件有关