Java 为什么赢了';这不就是我想要的字符串吗?

Java 为什么赢了';这不就是我想要的字符串吗?,java,swing,Java,Swing,我想要这样的结果。如果我单击finish按钮,一个类将被添加到umlClassList(通过setName)和StoreData类中的publicPrivateArrayList。然后我想将它们转换为字符串,并在GenCode类的文本框区域中显示它们,如public ClassName{} public class CreateUmlClass extends JFrame { JRadioButton radio1Public = new JRadioButton("Public"); //

我想要这样的结果。如果我单击finish按钮,一个类将被添加到
umlClassList
(通过
setName
)和
StoreData
类中的
publicPrivate
ArrayList。然后我想将它们转换为字符串,并在
GenCode
类的文本框区域中显示它们,如
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
类中的
GenCode()
之后,我想将字符串
s3=s1+s2+s3
放到
GenCode
类中的
textarea
,但为什么它只返回
{}
?我想获得
公共类名{}

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);              
        }
    }

}
}
存储数据类

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+"{"+"}";

    }

}
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);
            }
        }
        }

}
你的代码

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+"{"+"}";

}
所以它返回“{}”的原因是“umlClassList”为空,并且不更新s1和s2。你可以通过电话查一下

if(umlClassList.size()==0) System.out.println("umlClassList is empty" );

它会返回“{}”吗?我不明白您为什么要这样做,但是
如果(isPrivate=true)
肯定是错误的。您的意思可能是
if(isPrivate==true)
(或者更好的
if(isPrivate)
)。您想要串联还是算术和?对于后者,请参阅此可能的重复:循环内的字符串串联应始终使用
StringBuilder
@lauretg Oh!是的,谢谢,`s3=s3+s1+s2行之后的
}
上有语法错误`我已经更改了代码,但代码再次出错;(上面说字符串s1,s2没有初始化。在这种情况下,我可以初始化s1,s2为空吗?是的字符串s1=“”,s2=“”;这是因为您的umlCLassList为空。