简体   繁体   English

Java GUI 只接受某些名称

[英]Java GUI to accept only certain names

I'm currently working on a GUI where you enter your name and it tells you whether or not it has been accepted.我目前正在开发一个 GUI,您可以在其中输入您的姓名,它会告诉您它是否已被接受。 Say that if the names "John" or "Jane" are entered then you get a "Verified" message or "Unverified" message if you type any other name.假设如果输入了姓名“John”或“Jane”,那么如果您输入任何其他姓名,则会收到“已验证”消息或“未验证”消息。 Here's what I have so far, just really unsure how to add the IF statement for detecting the certain names.这是我到目前为止所拥有的,只是真的不确定如何添加 IF 语句来检测某些名称。 Thanks.谢谢。

NamePrompt.java名称提示.java

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NamePrompt extends JFrame{

    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField(21);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitButton(textBoxToEnterName));
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);

   }

    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true); 
    }

}

SubmitButton.java提交按钮.java

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class SubmitButton implements ActionListener {

    JTextField nameInput;

    public SubmitButton(JTextField textfield){
        nameInput = textfield;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput.getText() + " which is allowed.");
    }


}

As mentioned in another answer, this should be handled in the actionPerformed method.正如另一个答案中提到的,这应该在 actionPerformed 方法中处理。 However, going based on what you've presented to the community, here's something that should work;然而,根据你向社区展示的内容,这里有一些应该起作用的东西;

if the names are case sensitive, your modification would be as such:如果名称区分大小写,则您的修改如下:

public SubmitButton(JTextField textfield){
    nameInput = textfield;
    if (nameInput.equals("InsertCaseSensitiveName")) {
        //TODO: Verified Name
    } else {
        //TODO: Unverified Name
    }
}

if case insensitive:如果不区分大小写:

public SubmitButton(JTextField textfield){
    nameInput = textfield;
    if (nameInput.equalsIgnoreCase("InsertCaseSensitiveName")) {
        //TODO: Verified Name
    } else {
        //TODO: Unverified Name
    }
}

To use a list:要使用列表:

//initialize your list (formed so backwards compatible)
List<String> valid = new java.util.concurrent.CopyOnWriteArrayList<String>();
//Within a function, add all names to the list in lowercase (java.lang.String.toLowerCase())

    public SubmitButton(JTextField textfield){
        nameInput = textfield;
        if (valid.contains(nameInput.toLowerCase()) {
            //TODO: Verified Name
        } else {
            //TODO: Unverified Name
        }
    }

References:参考:

conditional if statements 条件 if 语句

java.lang.String.equals java.lang.String.equals

java.lang.String.equalsIgnoreCase java.lang.String.equalsIgnoreCase

java.lang.String.toLowerCase java.lang.String.toLowerCase

java.util.List java.util.List

java.util.concurrent.CopyOnWriteArrayList java.util.concurrent.CopyOnWriteArrayList

The actionPerformed method is called after clicking the submit button.单击提交按钮后将调用actionPerformed方法。

public void actionPerformed(ActionEvent submitClicked) {
    Component frame = new JFrame();
    JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput.getText() + " which is allowed.");

    // You can store the value of whatever the user enters.
    String inputName = nameInput.getText();

    // And add the if statements:
    if(inputName.equals("John") {
        JOptionPane.showMessageDialog(frame, "Verified");
    }
}

Alternatively you can create a List of Strings that contains all the names that is accepted.或者,您可以创建一个包含所有接受名称的字符串列表。 Example:例子:

List<String> acceptedNames = Arrays.asList(new String[]{"John", "Jane"});
// and check
if acceptedNames.contains(inputName) {
    // verified.
}

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

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