我正在构建自己的 GUI,它将以列表形式显示朋友对象的列表。我遇到的第一个问题是,当我在没有构造函数的情况下运行代码时,一切正常。但是当我为我的 GUI 类创建一个构造函数时,会显示错误消息:
load: GUIapp.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class GUIapp with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:680)
我的代码:
public class GUIapp extends JApplet{
/*
* Attributes
*/
//** Friends Objects**//
private FriendsGroup a;
private ArrayList<friends> friendList;
//** PANEL **//
private JPanel outerPanel;
//** Button **//
private JButton button1;
/*
* Constructor for Getting all the friends set up
*/
private GUIapp(){
a = null; //initialize variable
try {
a = new FriendsGroup("friends.txt"); //import friend list
} catch (IOException e) {
System.out.println("Fail Import.");
}
friendList = a.getFriendsGroup(); //return an arrayList of Friends Object
}
/*
* Create Stuff
*/
public void createStuff() {
outerPanel = new JPanel(); //create outer panel
button1 = new JButton("Click Me");
outerPanel.add(button1,BorderLayout.SOUTH);
}
/*
* Initialize Stuff
*
*/
public void init(){
createStuff(); //initialize create stuff
this.add (outerPanel);
}
}
在上面的代码中,如果你取出构造函数,它似乎可以完美地工作。我的问题是,代码有什么问题?为什么我似乎不能创建一个构造函数来首先加载数据?
我的第二个问题是我将如何创建一个面板来显示朋友姓名列表?这些名称被导入并存储在名为friendList 的朋友对象的arraylist 中,该对象存储在构造函数中。
谢谢,