本文介绍了无法在类对象的 ArrayList 中存储值.(代码编辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是一个Java代码转换器.它涉及一个 GUI 让用户输入类的类型、名称和方法.为了存储这些值,我创建了一个 VirtualClass 和一个 ArrayList 类.classes 用于存储变量 boolean isPrivateString classNameString methodName.但是,我发现 ArrayList 中没有存储任何内容...请帮我看看是什么问题

This is basically a Java code converter. It involves a GUI let user input class type, name and method. To store the values, I've created a class VirtualClass with an ArrayList<VirtualClass> classes to store variables boolean isPrivate, String className and String methodName. However, I found that nothing was stored into the ArrayList...please help me to see what's the problem

下面是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 的相关代码,让用户创建类

For reference, here's some relevant code from the GUI which let users create class

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

And here's a couple of methods from another class for display the final 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代码的方法

Here's the method to generate the Java code

    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()

And what would display in my programme is like this, where c should be class name, m should be method name, and 0 = classes.size()

public class c0{
    public void m{
    }
}

谁能帮我找出问题所在?我只是不知道,我收到的答案似乎不起作用.请帮忙!

Can anyone help me to spot out the problem please?I just have no idea and the answers I received doesn't seem to work. Please help!

推荐答案

从您发布的信息来看,您将 VirtualClass stObject 启动到 actionPerformed 方法似乎很奇怪.这意味着每次重新创建对象时.

From information you posted, seems strange that you initiate VirtualClass stObject into actionPerformed method. Its mean that each time you recreate your object.

使您的 VirtualClass stObject 成为全局,例如:

Make your VirtualClass stObject to be global for example, like:

private VirtualClass stObject;

...

stObject = new VirtualClass();

private class Handler implements ActionListener{

    public void actionPerformed(ActionEvent event){

    ...

   stObject.createClass(cName, mName, isP);

这篇关于无法在类对象的 ArrayList 中存储值.(代码编辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:05