简体   繁体   English

单击JButton时发生错误-未执行ActionEvent

[英]Error when clicked a JButton - ActionEvent is not performed

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. 这是一个我们必须创建一个UML-to-Java文本文件的项目。 User would enter class information and choose a UML relationship, and this programme have to print out the Java clsas text on a JTextField. 用户将输入类信息并选择UML关系,此程序必须在JTextField上打印出Java clsas文本。 Just like when you create a new class in eclipse. 就像在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时,它将存储false。而且我还希望检查JTextField中键入的内容通过checkName(),如果方法返回false,则该字符串将存储在ArrayList<String>

Below is my code - and the error is that when user clicked confirmButton, the programme just stopped and have no reaction. 下面是我的代码-错误是当用户单击confirmButton时,程序刚刚停止并且没有任何反应。 Also, there's some problems in getName() method and the boolean[] for storing true and false. 另外,getName()方法和用于存储true和false的boolean []中也存在一些问题。 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. 我觉得我正在使事情复杂化,应该有一种更简单的方法来实现。

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

In the ActionListener for the confirm button the Scanner instance input in this statement blocks waiting on input from the user 在确认按钮的ActionListener中,此语句中的Scanner实例input阻止等待用户输入

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作为从用户读取输入的替代方法。

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 (ie command line). 具体来说,您的input变量与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. OTOH,我怀疑使用命令行不是您想要的,就像使用GUI一样。 Instead you can add an input field (eg a JTextBox). 相反,您可以添加一个输入字段(例如JTextBox)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM