[英]How to add a JPanel from a diffent class to another JPanel on mouse click
I've been trying to populate a JPanel (here reffered to as 'BulletinsJPanel') in a class reffered to as 'Home.java' with a textfield (here reffered to as 'readerSetTxtJTextField') when a JLabel is clicked. 我一直在尝试在单击JLabel时使用文本字段(这里称为“ readerSetTxtJTextField”)填充到一个名为“ Home.java”的类中的一个JPanel(这里称为“ BulletinsJPanel”)。 ReaderPanel is in another class called 'Reader.java' which is supposed to read the contents of a text file and populate a JTextField object with a line of text. ReaderPanel在另一个名为“ Reader.java”的类中,该类应读取文本文件的内容并用一行文本填充JTextField对象。
I'm using netbeans which doesn't show me any code error highlights. 我正在使用的netbeans不会显示任何代码错误突出显示。
I would really appreciate some help in getting the textfield 'readerSetTxtJTextField' to show. 我非常感谢帮助显示文本字段“ readerSetTxtJTextField”的帮助。 Thanks a lot in advance. 非常感谢。
Heres my code: 这是我的代码:
// 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;
}
}
As a quick fix to your error , Change : 为了快速解决您的错误,请更改:
for(i = 0; i <= itemsLength; i++)
to 至
for(i = 0; i < itemsLength; i++)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.