是否可以从文本文件中读取多行并将其导出到各自的文本字段?
Is it possible to read multiple lines from textfile and export it to their respective jtextfields?
private void loadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
FileReader reader = new FileReader("reload.txt");
BufferedReader br = new BufferedReader(reader);
koontf.read(br,null);
baamtf.read(br,null);
sachitf.read(br,null);
fakertf.read(br,null);
phonsekaltf.read(br,null);
lauretf.read(br,null);
yeontf.read(br,null);
aguerotf.read(br,null);
agnistf.read(br,null);
lokitf.read(br,null);
lawliettf.read(br,null);
ryuzakitf.read(br,null);
br.close();
koontf.requestFocus();
baamtf.requestFocus();
sachitf.requestFocus();
fakertf.requestFocus();
phonsekaltf.requestFocus();
lauretf.requestFocus();
yeontf.requestFocus();
aguerotf.requestFocus();
agnistf.requestFocus();
lokitf.requestFocus();
lawliettf.requestFocus();
ryuzakitf.requestFocus();
}catch(IOException e) {
}
}
Is it even possible to put each of them to a certain textfield?Like 12 to jtextfield1,10 to jtextfield2 and so on...I've tried some tutorials and can't really figure it out.
您可以将所有文本字段放在一个数组中,然后在读取文本文件时遍历该数组。像这样:
JTextField[] textFields = new JTextField[10];
// ... init your textFields here
int line =0; // first line will be first textfield and so on
Scanner scanner = new Scanner(new File("reload.txt")); // use Scanner instead of FileReader, it's easier :)
while(scanner.hasNextLine()){ // as long as you did not reach the end of the file
textFields[line++].setText(scanner.nextLine()); // get the next line and put it in the respective textfield
}
但是,在这种情况下,您必须确保每一行都有一个文本字段,或者您阅读的行数不超过文本字段的数量。
例如:
while(.....){
....
if(line==textFields.length){
break;
}
}
另一件需要注意的事情是,行的顺序必须与文本字段的顺序相对应。
编辑
我必须补充一点,所有这些都可以毫无问题地工作。但这不是一个非常优雅的解决方案。当您更改 UI 并且文本字段的顺序不同时会发生什么?或者在您的文本文件中有一个重要的新行,但在您的 UI 中没有 TextField?
编辑 2
您评论中的代码没有显示您如何将 JTextFields 放入数组中。我的猜测是您正在使用一些 IDE 来创建 GUI,因此您应该在构造函数中调用 initComomponents();
或其他内容。在这种情况下,从 loadActionPerformed
方法中删除行 JTextField[] textFields = new JTextField[10];
并将其放入构造函数中,如下所示:
public class MyClass{
private JTextField[] textFields;
public MyClass(){
initComponents();
this.textFields = new JTextField[10] // where 10 is the number of lines in your textfile AND the number of JTextFields you have in your GUI
// then fill the array (by hand if you like)
this.textField[0] = koontf;
this.textField[1] = baamtf;
// and so on..
}
编辑 3
只是说清楚,这就是你制作程序所需要的运行。假设您的 class 被称为 MyClass
那么它可能看起来像这样:
private JTextField[] textFields; // this creates your array
public MyClass(){ // this is the constructor of your class (I don't know how it is called)
initComponents(); // auto generated code from NetBeans to initalize your GUI elements
// init your array
textFields = new JTextField[12]; // 12 if I counted correctly
// fill it
textFields[0] = koontf;
textFields[1] = baamtf;
textFields[2] = sachitf;
textFields[3] = fakertf;
textFields[4] = phonsekaltf;
textFields[5] = lauretf;
textFields[6] = yeontf;
textFields[7] = aguerotf;
textFields[8] = agnistf;
textFields[9] = lokitf;
textFields[10] = lawliettf;
textFields[11] = ryuzakitf;
}
private void loadActionPerformed(java.awt.event.ActionEvent evt){
int line = 0;
try(Scanner scanner = new Scanner(new File("reload.txt"))){
while(scanner.hasNextLine()){
textFields[line++].setText(scanner.nextLine());
if(line == textFields.length){
break;
}
}
}catch(FileNotFoundException ex){
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
koontf.requestFocus(); // you can only call request focus on one element at a time (it does not make sense to call it on all textfields
}
private void loadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
FileReader reader = new FileReader("reload.txt");
BufferedReader br = new BufferedReader(reader);
koontf.read(br,null);
baamtf.read(br,null);
sachitf.read(br,null);
fakertf.read(br,null);
phonsekaltf.read(br,null);
lauretf.read(br,null);
yeontf.read(br,null);
aguerotf.read(br,null);
agnistf.read(br,null);
lokitf.read(br,null);
lawliettf.read(br,null);
ryuzakitf.read(br,null);
br.close();
koontf.requestFocus();
baamtf.requestFocus();
sachitf.requestFocus();
fakertf.requestFocus();
phonsekaltf.requestFocus();
lauretf.requestFocus();
yeontf.requestFocus();
aguerotf.requestFocus();
agnistf.requestFocus();
lokitf.requestFocus();
lawliettf.requestFocus();
ryuzakitf.requestFocus();
}catch(IOException e) {
}
}
Is it even possible to put each of them to a certain textfield?Like 12 to jtextfield1,10 to jtextfield2 and so on...I've tried some tutorials and can't really figure it out.
您可以将所有文本字段放在一个数组中,然后在读取文本文件时遍历该数组。像这样:
JTextField[] textFields = new JTextField[10];
// ... init your textFields here
int line =0; // first line will be first textfield and so on
Scanner scanner = new Scanner(new File("reload.txt")); // use Scanner instead of FileReader, it's easier :)
while(scanner.hasNextLine()){ // as long as you did not reach the end of the file
textFields[line++].setText(scanner.nextLine()); // get the next line and put it in the respective textfield
}
但是,在这种情况下,您必须确保每一行都有一个文本字段,或者您阅读的行数不超过文本字段的数量。
例如:
while(.....){
....
if(line==textFields.length){
break;
}
}
另一件需要注意的事情是,行的顺序必须与文本字段的顺序相对应。
编辑
我必须补充一点,所有这些都可以毫无问题地工作。但这不是一个非常优雅的解决方案。当您更改 UI 并且文本字段的顺序不同时会发生什么?或者在您的文本文件中有一个重要的新行,但在您的 UI 中没有 TextField?
编辑 2
您评论中的代码没有显示您如何将 JTextFields 放入数组中。我的猜测是您正在使用一些 IDE 来创建 GUI,因此您应该在构造函数中调用 initComomponents();
或其他内容。在这种情况下,从 loadActionPerformed
方法中删除行 JTextField[] textFields = new JTextField[10];
并将其放入构造函数中,如下所示:
public class MyClass{
private JTextField[] textFields;
public MyClass(){
initComponents();
this.textFields = new JTextField[10] // where 10 is the number of lines in your textfile AND the number of JTextFields you have in your GUI
// then fill the array (by hand if you like)
this.textField[0] = koontf;
this.textField[1] = baamtf;
// and so on..
}
编辑 3
只是说清楚,这就是你制作程序所需要的运行。假设您的 class 被称为 MyClass
那么它可能看起来像这样:
private JTextField[] textFields; // this creates your array
public MyClass(){ // this is the constructor of your class (I don't know how it is called)
initComponents(); // auto generated code from NetBeans to initalize your GUI elements
// init your array
textFields = new JTextField[12]; // 12 if I counted correctly
// fill it
textFields[0] = koontf;
textFields[1] = baamtf;
textFields[2] = sachitf;
textFields[3] = fakertf;
textFields[4] = phonsekaltf;
textFields[5] = lauretf;
textFields[6] = yeontf;
textFields[7] = aguerotf;
textFields[8] = agnistf;
textFields[9] = lokitf;
textFields[10] = lawliettf;
textFields[11] = ryuzakitf;
}
private void loadActionPerformed(java.awt.event.ActionEvent evt){
int line = 0;
try(Scanner scanner = new Scanner(new File("reload.txt"))){
while(scanner.hasNextLine()){
textFields[line++].setText(scanner.nextLine());
if(line == textFields.length){
break;
}
}
}catch(FileNotFoundException ex){
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
koontf.requestFocus(); // you can only call request focus on one element at a time (it does not make sense to call it on all textfields
}