这里我有一个 GUI 窗口,它基本上要求用户选择一个 JRadioButton 并在 JTextField 中键入一些内容,然后选择确认/取消。
这是一个我们必须制作 UML-to-Java 文本文件的项目。用户将输入类信息并选择 UML 关系,该程序必须在 JTextField 上打印出 Java clsas 文本。就像在 Eclipse 中创建一个新类一样。
我想要做的是创建一个 boolean[] 来存储布尔数组,当用户选择 JRadioButton_A 时它将存储 true,当用户选择 JRadioButton_B 时它将存储 false。我还希望检查在 JTextField 中键入的内容通过 checkName(),如果方法返回 false,则字符串将存储在ArrayList<String>
.
下面是我的代码 - 错误是当用户单击确认按钮时,程序刚刚停止并且没有反应。此外,getName() 方法和 boolean[] 用于存储真假也存在一些问题。当用户需要再次输入名称时,它会将丢弃的 sting/boolean 保存到数组中。有没有更好的方法来制作这个程序?我觉得我把事情复杂化了,应该有一种更简单的方法来做到这一点。
这是我的 UI 类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class AddClass extends JFrame{
Scanner input = new Scanner(System.in);
ClassName classObject = new ClassName();
private JRadioButton publicButton, privateButton;
private JLabel className;
private JTextField inputClassName;
private JButton confirmButton;
private JButton cancelButton;
public AddClass(){
super("Add class");
setLayout(new FlowLayout());
publicButton = new JRadioButton("public", true);
privateButton = new JRadioButton("private", false);
className = new JLabel("Class Name: ");
inputClassName = new JTextField(10);
confirmButton = new JButton("Confirm");
cancelButton = new JButton("Cancel");
add(publicButton);
add(privateButton);
add(className);
add(inputClassName);
add(confirmButton);
add(cancelButton);
ButtonGroup group = new ButtonGroup();
group.add(publicButton);
group.add(privateButton);
Handler handler = new Handler();
// Handler2 handler2 = new Handler2();
// Handler3 handler3 = new Handler3();
confirmButton.addActionListener(handler);
// publicButton.addActionListener(handler2);
// privateButton.addActionListener(handler3);
}// end constructor AddClass()
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
String name = inputClassName.getText();
classObject.addName(name);
while (classObject.checkName(name) == true){
JOptionPane.showMessageDialog(null, "Class name invalid. " +
"\nEntered name should not contain java keywords or equal to other existing names. " +
"\nPlease try again."); // doesn't work
name = input.nextLine();
classObject.addName(name);
}// end if
JOptionPane.showMessageDialog(null, "Class saved."); // doesn't work
name = input.nextLine();
classObject.addName(name);
}// end actionPerformed()
}// end Handler class
private class Handler2 implements ActionListener{
public void actionPerformed(ActionEvent event){
boolean b = true;
b = classObject.setPP();
}
}
private class Handler3 implements ActionListener{
public void actionPerformed(ActionEvent event){
boolean b = false;
b = classObject.setPP();
}
}
}// end AddClass
这是我用于存储用户输入的类
import java.util.Scanner;
import java.util.ArrayList;
public class ClassName {
Scanner input = new Scanner(System.in);
JavaKeywords keyObject = new JavaKeywords();
private ArrayList<String> className = new ArrayList<String>();
private String name = new String();
private int size = className.size();
private Boolean[] bArray = new Boolean[size];
public void addName(String name){
this.name = name;
className.add(name);
}// end addName()
public boolean checkName(String name){
boolean check = true;
for (int i=0; i<=size; i++){
if (keyObject.containsKeyword(className.get(i)) || name.equals(className.get(i))){
boolean o = false;
check = o;
}// end if
}// end for
return check;
}// end checkName
public boolean setPP(){
boolean b = true;
return b;
}
public void addPP(Boolean[] bArray){
this.bArray = bArray;
for (int i=0; i>=size; i++){
bArray[i] = setPP();
}
}// add a Array of boolean. for className[i], its class type = item[i] in bArray.
// public = true, private = false
public String getPublicPrivate(){
String p = "";
for (int i =0; i<=size; i++){
if(bArray[i]=true)
p = "public";
else
p = "private";
}
return p;
}
public void setName(String name){
this.name = name;
}//end setName()
public String getName(){
return name;
}// end getName()
public ArrayList<String> getAllNames(){
return className;
}// end getAllNames()
public void setAllNames(ArrayList<String> className){
this.className = className;
}// end setAllNames()
}//end class