如何在鼠标单击时将不同类的JPanel添加到另一个JPanel



当单击JLabel时,我一直试图在一个被引用为"Home.java"的类中填充一个JPanel(此处被引用为'BulletinsJPanel'),其中包含一个文本字段(此处被参考为'readerSetTxtJTextField')。ReaderPanel在另一个名为"Reader.java"的类中,该类应该读取文本文件的内容并用一行文本填充JTextField对象。

我使用的是netbeans,它不会向我显示任何代码错误突出显示。

如果您能帮助我显示文本字段"readerSetTxtJTextField",我将不胜感激。提前非常感谢。

这是我的代码:

// The class Home
package Panels;
public class Home extends javax.swing.JPanel {
    // Here's a method in the class 'Home.java' which should populate 'BulletinsJPanel' with the contents
    private void MouseClickedList(java.awt.event.MouseEvent evt) {
        BulletinsJPanel.add(Reader.readerMouseClickedList());
        BulletinsJPanel.revalidate();
        BulletinsJPanel.repaint();
    }
}
// Now the class Reader below
package Database;
public class Reader {
    public static JTextField readerSetTxtJTextField;
    public static JTextField readerMouseClickedList() {                                  
        try {
            // Our code
            String fileURL = "/D:/TestFile.txt/";
            List<String[]> matches = new ArrayList<String[]>();
            String finalText;
            FileInputStream fileInputStream = new FileInputStream(fileURL);
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
            String stringLine;
            while((stringLine = bufferedReader.readLine()) != null) {
                String splittable = "[\s]", splitLenth = "{955}";
                if(stringLine.startsWith("2013001")) {
                    String[] splits = stringLine.split(splittable + splitLenth );
                    matches.add(splits);
                }
            }
            dataInputStream.close();
            for(String[] items : matches) {
                int itemsLength = items.length;
                int i;
                for(i = 0; i <= itemsLength; i++) {
                    finalText = (items[i]);
                    readerSetTxtJTextField = new JTextField();
                    readerSetTxtJTextField.setText(finalText);
                }
            }
        } catch (Exception e) {
            // Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return readerSetTxtJTextField;
    }                                 
}

作为对错误的快速修复,更改:

for(i = 0; i <= itemsLength; i++)

for(i = 0; i < itemsLength; i++)

最新更新