Here I have a GUI window and it basically ask the user to select a JRadioButton and type something in a JTextField, then choose confirm/cancel.
这里我有一个GUI窗口,它基本上要求用户选择一个JRadioButton并在JTextField中键入内容,然后选择确认/取消。
It is a project which we have to make a UML-to-Java text file. User would enter class information and choose a UML relationship, and this programme have to print out the Java clsas text on a JTextField. Just like when you create a new class in eclipse.
这是一个我们必须制作UML到Java文本文件的项目。用户将输入类信息并选择UML关系,该程序必须在JTextField上打印出Java clsas文本。就像你在eclipse中创建一个新类一样。
what I want to do is make a boolean[] to store an array of booleans, when user selects JRadioButton_A it'll store true and when user select JRadioButton_B it'll store false.And also I want the things typed in JTextField to be checked by a checkName(), if the method returns false, the string will be stored in an ArrayList<String>
.
我想要做的是使用boolean []来存储一个布尔数组,当用户选择JRadioButton_A它将存储为true时,当用户选择JRadioButton_B时它将存储假。我还要检查在JTextField中键入的内容通过checkName(),如果方法返回false,则字符串将存储在ArrayList
Below is my code - and the error is that when user clicked confirmButton, the programme just stopped and have no reaction. Also, there's some problems in getName() method and the boolean[] for storing true and false. When user needs to input name again, it would save the discarded sting/boolean into the array. Is there any better way to make this programme? I feel like I am complicating things and there should be a simpler way to make it.
下面是我的代码 - 错误是当用户点击confirmButton时,程序刚停止并且没有反应。此外,getName()方法和boolean []存在一些问题,用于存储true和false。当用户需要再次输入名称时,它会将丢弃的sting / boolean保存到数组中。有没有更好的方法来制作这个节目?我觉得我使事情变得复杂,应该有一个更简单的方法来实现它。
Here's my UI class
这是我的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
Here's my class for storing user input
这是我的用于存储用户输入的类
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
2 个解决方案
#1
0
The reason your program "stops" when the user clicks the confirm button is that the program is waiting for input on the command line. Specifically, your input
variable is tied to System.in
(i.e. command line).
当用户单击确认按钮时程序“停止”的原因是程序正在等待命令行上的输入。具体来说,您的输入变量与System.in(即命令行)绑定。
You can just type your answer at the command line, hit Enter nad your program will continue.
您只需在命令行输入答案,按Enter键即可继续执行程序。
OTOH, I suspect that using the command line is not what you want as you are using a GUI. Instead you can add an input field (e.g. a JTextBox).
OTOH,我怀疑在使用GUI时使用命令行并不是你想要的。相反,您可以添加输入字段(例如JTextBox)。
#2
0
In the ActionListener
for the confirm button the Scanner
instance input
in this statement blocks waiting on input from the user
在确认按钮的ActionListener中,此语句中的扫描程序实例输入阻止等待用户的输入
String name = input.nextLine();
The ActionEvent
won't complete until a value has been entered.
在输入值之前,ActionEvent不会完成。
Solution: Add a JTextField
as an alternative means to read input from the user.
解决方案:添加JTextField作为读取用户输入的替代方法。
#1
0
The reason your program "stops" when the user clicks the confirm button is that the program is waiting for input on the command line. Specifically, your input
variable is tied to System.in
(i.e. command line).
当用户单击确认按钮时程序“停止”的原因是程序正在等待命令行上的输入。具体来说,您的输入变量与System.in(即命令行)绑定。
You can just type your answer at the command line, hit Enter nad your program will continue.
您只需在命令行输入答案,按Enter键即可继续执行程序。
OTOH, I suspect that using the command line is not what you want as you are using a GUI. Instead you can add an input field (e.g. a JTextBox).
OTOH,我怀疑在使用GUI时使用命令行并不是你想要的。相反,您可以添加输入字段(例如JTextBox)。
#2
0
In the ActionListener
for the confirm button the Scanner
instance input
in this statement blocks waiting on input from the user
在确认按钮的ActionListener中,此语句中的扫描程序实例输入阻止等待用户的输入
String name = input.nextLine();
The ActionEvent
won't complete until a value has been entered.
在输入值之前,ActionEvent不会完成。
Solution: Add a JTextField
as an alternative means to read input from the user.
解决方案:添加JTextField作为读取用户输入的替代方法。