簡體   English   中英

如何在單擊鼠標時將一個不同類的JPanel添加到另一個JPanel

[英]How to add a JPanel from a diffent class to another JPanel on mouse click

我一直在嘗試在單擊JLabel時使用文本字段(這里稱為“ readerSetTxtJTextField”)填充到一個名為“ Home.java”的類中的一個JPanel(這里稱為“ BulletinsJPanel”)。 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++)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM