-1

我想要这样的结果。如果我单击完成按钮,一个类将被添加到类中的umlClassList(via setName) 和publicPrivateArrayLists 中StoreData。然后我想将它们转换为字符串并将它们显示在GenCode类的文本框区域中,例如public ClassName{}

我想在类中调用后将字符串获取s3=s1+s2+s3到类 中,但是为什么它只返回?我想得到 textareaGenCodegenCode()StoreData{}public ClassName{}

public class CreateUmlClass extends JFrame {

JRadioButton radio1Public = new JRadioButton("Public"); // Radio Button
JRadioButton radio2Private = new JRadioButton("Private");
JButton finish = new JButton("Finish");
JButton cancel = new JButton("Cancel");
JButton clearAll = new JButton("Clear All");
JLabel classNameLabel = new JLabel("Class Name:");// Class name label
JLabel methodNameLabel = new JLabel("Method Name:");// Class name label
ButtonGroup group = new ButtonGroup();
JTextField methodNameTextField = new JTextField(20);// Class Name Text Box
JTextField classNameTextField = new JTextField(20);// Class Name Text Box
private JFrame frame;

public CreateUmlClass() {
    super("Create class");

    this.setLocation(100, 100);
    // this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());


    add(classNameLabel);
    add(classNameTextField);


    group.add(radio1Public);
    group.add(radio2Private);
    add(radio1Public);
    add(radio2Private);
    add(methodNameLabel);
    add(methodNameTextField);
    add(finish);
    add(cancel);
    add(clearAll);


    Handler handle = new Handler();
    finish.addActionListener(handle);
    cancel.addActionListener(handle);
    clearAll.addActionListener(handle);

}

private class Handler implements ActionListener {
    String ClassName;
    StoreData obj = new StoreData();

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == finish) {
            ClassName=classNameTextField.getText();
            if (classNameTextField != null && (radio1Public.isSelected() || radio2Private.isSelected())) {
                obj.setName(ClassName);
                if (radio1Public.isSelected()) {
                    obj.CheckisPrivate(false);

                } else if (radio1Public.isSelected()) {
                    obj.CheckisPrivate(true);
                }
                JOptionPane.showMessageDialog(frame, "Your class is stored");
            } else {
                JOptionPane.showMessageDialog(frame,
                        "Class Name or/and access modifier cannot be empty.",
                        "Inane warning",
                        JOptionPane.WARNING_MESSAGE);
            }
        }

        else if (e.getSource() == cancel) {
            setVisible(false);
        }
        else if (e.getSource() == clearAll) {
            classNameTextField.setText(null);
            group.clearSelection();
            methodNameTextField.setText(null);              
        }
    }

}
}

StoreData 类

public class StoreData {

    String name;
    static ArrayList<String> umlClassList = new ArrayList<String>();
    static ArrayList<String> publicPrivate = new ArrayList<String>();

    public void setName(String className) {

        umlClassList.add(className);

    }

    public void CheckisPrivate(boolean isPrivate) {
        if (isPrivate = true) {
            publicPrivate.add("Private");
        } else if (isPrivate = false) {
            publicPrivate.add("Public");
        }
    }

    public String genCode() {
        String s3="";
        for (int index = 0; index < umlClassList.size(); index++) {
            String s1,s2;
            s1=publicPrivate.get(index);
            s2=umlClassList.get(index);
            s3=s3+s1+s2;        
        }
        return s3+"{"+"}";

    }

}

GenCode 类

public class GenCode extends JFrame{

    JLabel genedCode = new JLabel("The generated code:");
    StoreData data = new StoreData();
    String finalCode =data.genCode();
    JTextArea codeBox = new JTextArea(finalCode,5,30);
    JButton okbtn = new JButton("OK");
    JButton cancel = new JButton("Cancel");
    Handler handle = new Handler();

    private JFrame frame;
    public GenCode(){
        super("The Generated Code");
        setSize(200, 900);
        setLocation(200, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());


        //add(codeBox); 
        add(codeBox);
        codeBox.setVisible(true);
        codeBox.setEditable(false);
        add(okbtn);
        add(cancel);


        //add(codeBox,BorderLayout.CENTER);
        cancel.addActionListener(handle);

    }
    private class Handler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==cancel){
                setVisible(false);
            }
        }
        }

}
4

1 回答 1

1

你的代码

public String genCode() {
    String s3="";
    for (int index = 0; index < umlClassList.size(); index++) {
        String s1,s2;
        s1=publicPrivate.get(index);
        s2=umlClassList.get(index);
        s3=s3+s1+s2;        
    }
    return s3+"{"+"}";

}

可以简化为

public String genCode() {
    String s3="";
        String s1,s2;
        if(umlClassList.size()>0)
        {
            s1=publicPrivate.get(umlClassList.size()-1);
            s2=umlClassList.get(umlClassList.size()-1);
        }
        s3=s3+s1+s2;        
    return s3+"{"+"}";

}

所以它返回“{}”的原因是因为“umlClassList”是空的,你没有更新s1和s2。您可以通过以下方式检查

if(umlClassList.size()==0) System.out.println("umlClassList is empty" );
于 2013-10-27T11:11:00.010 回答