Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何关闭Java应用程序_Java_Swing - Fatal编程技术网

如何关闭Java应用程序

如何关闭Java应用程序,java,swing,Java,Swing,我写了这段代码,不知道你们能不能帮我弄清楚,每当我在text1中输入名称“Bob/Bob/Bob”时,如何关闭我的应用程序 public class Event extends JFrame { //create items private JTextField text1; private JTextField text2; private JTextField text3; private JPasswordField pass1; pu

我写了这段代码,不知道你们能不能帮我弄清楚,每当我在text1中输入名称“Bob/Bob/Bob”时,如何关闭我的应用程序

public class Event extends JFrame {
    //create items

    private JTextField text1;
    private JTextField text2;
    private JTextField text3;
    private JPasswordField pass1;


    public Event(){
        super("The title");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        text1 = new JTextField(10);
        add(text1); 
        text2 = new JTextField("enter text here");
        add(text2);
        text3 = new JTextField("uneditbale", 15);
        text3.setEditable(false);
        add(text3); 
        pass1 = new JPasswordField("enter your password", 10);
        add(pass1);

        //create object
        thehandler handler = new thehandler();
        text1.addActionListener(handler);
        text2.addActionListener(handler);
        text3.addActionListener(handler);
        pass1.addActionListener(handler);

        //constructor Event ends
    }

    private class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==text1)
                string=String.format("field 1: %s",event.getActionCommand());
            else if(event.getSource()==text2)
                string=String.format("field 2: %s",event.getActionCommand());
            else if(event.getSource()==text3)
                string=String.format("field 3: %s",event.getActionCommand());
            else if(event.getSource()==pass1)
                string=String.format("Password is : %s",event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);
        }

    }
    public static void main(String[] args) {
        Event ev = new Event();
        ev.setSize(350, 100);
        ev.setVisible(true);

    }
}

如果要终止整个过程,可以使用
System.exit(0)
dispose()
方法只能用于关闭窗口

(老实说,我被称为
事件的
JFrame
弄糊涂了,不幸的是这是一个糟糕的名字。)

有关如何执行JTextField和字符串相等性检查的过程的说明,请参阅


要终止应用程序,System.exit(0)应适用于您的目的。

使用此代码。我创建了一个文本字段,当它得到文本“Bob/Bob/Bob”时,它退出Jframe

public class Example extends JFrame {

Example() {

    JTextField textfield1 = new JTextField(10); 
    textfield1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            if(textfield1.getText().equals("Bob/BOB/bob"))
                    System.exit(0);

        }
    });

    add(textfield1);
 }
}

我理解你的问题不同

(...) enter the name "Bob/BOB/bob"
如果要使代码不区分大小写,请尝试使用
string.toLowerCase()
string.toUpperCase()

例如:

if("bob".equals(inputString.toLowerCase()) {
    // app close logic
    System.exit(0);
}

系统退出(0)会有魔力。“如果你觉得它有用,请考虑接受一个答案。”code>dispose()方法只能用于关闭窗口。
如果框架是应用程序中唯一打开的框架,JVM将关闭。