我希望我的结果如下。如果我单击“完成”按钮,则会在umlClassList
类的setName
(通过publicPrivate
)和StoreData
数组列表中添加一个类。然后我想将它们转换为字符串并在GenCode
类的文本框区域中显示它们,例如public ClassName{}
我希望在s3=s1+s2+s3
课程中调用textarea
后,将GenCode
字符串添加到genCode()
课程中的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);
}
}
}
}
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+"{"+"}";
}
}
StoreData类
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);
}
}
}
}
GenCode类
{{1}}
答案 0 :(得分: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" );