我环顾了其他线程,找不到可行的解决方案。
我的程序读取一个文件,将每个单词分割成一行,然后存储在数组中。如果要搜索的是array [0],那么我想将数组输出到其对应的文本字段中。
我试图通过仅为1个文本字段(ID文本字段)设置文本来进行测试,但该文本字段未填充文本。到目前为止,这是我的代码:
GUI-StudentUI
public class StudentUI extends javax.swing.JFrame {
/**
* Creates new form StudentUI
*/
public StudentUI() {
initComponents();
saveBtn.setVisible(false);
}
final String FILENAME = "Students.txt";
private void initComponents() {
searchTxt = new javax.swing.JTextField();
idLbl = new javax.swing.JLabel();
titleLbl = new javax.swing.JLabel();
forenameLbl = new javax.swing.JLabel();
surnameLbl = new javax.swing.JLabel();
address1Lbl = new javax.swing.JLabel();
address2Lbl = new javax.swing.JLabel();
postcodeLbl = new javax.swing.JLabel();
numberLbl = new javax.swing.JLabel();
birthLbl = new javax.swing.JLabel();
idTxt = new javax.swing.JTextField();
forenameTxt = new javax.swing.JTextField();
surnameTxt = new javax.swing.JTextField();
address1Txt = new javax.swing.JTextField();
address2Txt = new javax.swing.JTextField();
postcodeTxt = new javax.swing.JTextField();
numberTxt = new javax.swing.JTextField();
birthTxt = new javax.swing.JTextField();
searchBtn = new javax.swing.JButton();
searchLbl = new javax.swing.JLabel();
titleCombo = new javax.swing.JComboBox<>();
addBtn = new javax.swing.JButton();
editBtn = new javax.swing.JButton();
deleteBtn = new javax.swing.JButton();
saveBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
idLbl.setText("ID");
titleLbl.setText("Title");
forenameLbl.setText("Forename");
surnameLbl.setText("Surname");
address1Lbl.setText("Address 1");
address2Lbl.setText("Address 2");
postcodeLbl.setText(" Postcode");
numberLbl.setText("Phone Number");
birthLbl.setText("Date of Birth (DD/MM/YYYY)");
searchBtn.setText("Search");
searchBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchBtnActionPerformed(evt);
}
});
public void setTextField(JTextField jTF, String value) {
jTF.setText(value);
}
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
int count = Search.totalLines(FILENAME);
Search.linear(FILENAME, count, searchTxt.getText());
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new StudentUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel address1Lbl;
private javax.swing.JTextField address1Txt;
private javax.swing.JLabel address2Lbl;
private javax.swing.JTextField address2Txt;
private javax.swing.JLabel birthLbl;
private javax.swing.JTextField birthTxt;
private javax.swing.JButton deleteBtn;
private javax.swing.JButton editBtn;
private javax.swing.JLabel forenameLbl;
private javax.swing.JTextField forenameTxt;
private javax.swing.JLabel idLbl;
private javax.swing.JTextField idTxt;
private javax.swing.JLabel numberLbl;
private javax.swing.JTextField numberTxt;
private javax.swing.JLabel postcodeLbl;
private javax.swing.JTextField postcodeTxt;
private javax.swing.JButton saveBtn;
private javax.swing.JButton searchBtn;
private javax.swing.JLabel searchLbl;
private javax.swing.JTextField searchTxt;
private javax.swing.JLabel surnameLbl;
private javax.swing.JTextField surnameTxt;
private javax.swing.JComboBox<String> titleCombo;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
搜索类别
public class Search {
public static int totalLines(String filename) {
int n = 0;
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
n = n + 1;
}
} catch (Exception e) {
}
return n;
}
public static void linear(String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
StudentUI student = new StudentUI();
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
}
}
}
答案 0 :(得分:2)
您在(A)处遇到了大问题:
public static void linear(String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
StudentUI student = new StudentUI(); // ****** (A) ******
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
// ****** (B) ******
}
}
您正在创建一个new StudentUI()
对象-一个新对象,该对象与已经显示的StudentUI完全不同,因此设置其JTextField的状态将无效。在当前显示的StudentUI对象上。
错误的解决方案是将JTextField变量设置为静态-请勿这样做,因为这样做会把OOP婴儿和洗澡水一起扔掉。
更好的解决方案是将对当前显示的 StudentUI对象的引用传递给他的方法,以便您可以更改感兴趣对象的状态。
(B)处的其他问题-不要以这种方式忽略异常,因为这样做,如果您的代码崩溃,您将不知道为什么。
解决这个问题的一种方法是为线性方法提供一个StudentUI参数:
public static void linear(StudentUI student, String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
// StudentUI student = new StudentUI(); // ****** (A) ******
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
e.printStacktrace(); // ****** (B) *******
}
}
然后调用它,将this
作为第一个参数传递到方法中。
另外,请注意,我现在正在打印异常的堆栈跟踪,以便我可以查看是否/何时引发异常,什么原因引起的异常以及在何处发生。