开发者

Java program exits when I click OK button in JOptionPane.showMessageDialog

开发者 https://www.devze.com 2023-01-06 02:03 出处:网络
whe开发者_运维技巧n I click the OK button in second.java program, the program exit the program. I want it not to exit (since there is a thread running). I tried removing setDefaultCloseOperation(JFram

whe开发者_运维技巧n I click the OK button in second.java program, the program exit the program. I want it not to exit (since there is a thread running). I tried removing setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).

Java program exits when I click OK button in JOptionPane.showMessageDialog


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);
}
}

Many Thanks


You'll need to remove the System.exit(0); line. That's all.

0

精彩评论

暂无评论...
验证码 换一张
取 消