这基本上是一个 Java 代码转换器。它涉及一个 GUI,让用户输入类类型、名称和方法。为了存储这些值,我创建了一个类VirtualClass
来ArrayList<VirtualClass> classes
存储变量boolean isPrivate
,String className
和String methodName
。但是,我发现没有任何内容存储在ArrayList
...请帮助我看看有什么问题
下面是类VirtualClass
import java.util.*;
public class VirtualClass {
private static ArrayList<VirtualClass> classes = new ArrayList<VirtualClass>();
private boolean isPrivate;
private String className;
private String methodName;
public void setVirtualClass(String name, String method, boolean isP){
this.className = name;
this.isPrivate = isP;
this.methodName = method;
}
public void createClass(String name, String method, boolean isP){
this.className = name;
this.isPrivate = isP;
this.methodName = method;
classes.add(this);
}
作为参考,这里有一些来自 GUI 的相关代码,可让用户创建类
public class GuiAddClass extends JFrame{
private VirtualClass stObject;
...
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
String cName = inputClassName.getText();
String mName = inputMethodName.getText();
boolean isP = true;
if (classObject.checkName(cName) == false){
JOptionPane.showMessageDialog(null, "Class name invalid. " +
"\nEntered name should not contain java keywords or equal to other existing names. " +
"\nPlease try again.");
} else if (classObject.checkName(cName) == true) {
JOptionPane.showMessageDialog(null, "Class saved.");
// this message pane has popped up
cName = inputClassName.getText();
mName = inputMethodName.getText();
if (event.getSource() == publicButton) {
isP = false;
} else if (event.getSource() == privateButton) {
isP = true;
}
stObject = new VirtualClass();
stObject.createClass(cName, mName, isP);
}
}// end actionPerformed()
}// end Handler class
这是来自另一个类的几个方法,用于显示最终的 javaCode
public String getClassName(){
String cName = "classname";
String c = "c";
for (int i=0; i<classes.size(); i++){
c = classes.get(i).className;
}
cName = c;
return cName;
}
public String getMethodName(){
String mName = "methodname";
String m = "m";
for (int i=0; i<classes.size(); i++){
m = classes.get(i).methodName;
}
mName = m;
return mName;
}
public boolean getIsPrivate(){
boolean isP = false;
for (int i=0; i<classes.size(); i++){
isP = classes.get(i).isPrivate;
}
return isP;
}
这是生成Java代码的方法
public String getJavaCode(){
String javaCode = (classObject.getPublic() + " class " +
stObject.getClassName() + stObject.getListSize() +
"{\n"+"\t"+"public void "+stObject.getMethodName()+"{\n"+"\t}"+"\n}");
return javaCode;
在我的程序中显示的内容是这样的,其中 c 应该是类名,m 应该是方法名,并且0 = classes.size()
public class c0{
public void m{
}
}
任何人都可以帮我找出问题吗?我只是不知道,我收到的答案似乎不起作用。请帮忙!