当我在JOptionPane.showMessageDialog中单击OK按钮时,Java程序退出

时间:2010-07-03 03:13:35

标签: java jframe joptionpane

当我单击second.java程序中的确定按钮时,程序退出该程序。我希望它不要退出(因为有一个线程在运行)。我尝试删除 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

alt text http://i47.tinypic.com/du335.jpg


CODE


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class second extends JFrame implements ActionListener {

JLabel enterName;
JTextField name;
JButton click;
String storeName;

public second(){

    setLayout(null);
    setSize(300,250);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    enterName = new JLabel("Enter Your Name: ");
    click = new JButton("Click");
    name = new JTextField();
    enterName.setBounds(60,30,120,30);
    name.setBounds(80,60,130,30);
    click.setBounds(100,190,60,30);
    click.addActionListener(this);
    add(click);
    add(name);
    add(enterName);

}

public void actionPerformed(ActionEvent e) {

    if(e.getSource() == click) {

        storeName = name.getText();
        JOptionPane.showMessageDialog(null, "Hello" + storeName);
        System.exit(0);
    }
}

public static void main(String args[]){

    second s = new second();
    s.setVisible(true);
}
}

非常感谢

1 个答案:

答案 0 :(得分:14)

您需要删除System.exit(0);行。就是这样。