我写了一个程序,但是发现当关闭了程序运行窗口以后命令行依然没有停止运行,必须要 ctrl+c 才能结束。请问怎么可以在关闭窗口的同时也结束命令行?需要添加怎样一段程序,添加在哪呢?

解决方案 »

  1.   

    JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);如果你用的awt的Frame,那可能要麻烦一些,要加WindowListener才能搞定。
      

  2.   

    囧,LZ还没问是什么呢,1L就知道是 SWING
      

  3.   

    这是我写的程序,大家看下应该怎么加入退出程序//程序文件名为CeShiFile.java
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.io.*;
    public class CeShiFile extends JFrame implements ActionListener
    {
    JLabel jlbName = new JLabel("文件名");
    JTextField tfName = new JTextField(20);
    JButton btnOpen = new JButton("打开文件");
    JButton btnSave = new JButton("存储文件");
    JTextArea taText = new JTextArea(5,20);
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel(new BorderLayout());
    JPanel p5 = new JPanel(new BorderLayout());
    public CeShiFile()
    {
      p1.add(jlbName);
      p2.add(tfName);
      p3.add(btnOpen);
      p3.add(btnSave);
      JScrollPane s = new JScrollPane(taText);
      p4.add(p1,BorderLayout.NORTH);
      p4.add(p2,BorderLayout.CENTER);
      p4.add(p3,BorderLayout.SOUTH);
      p5.add(p4,BorderLayout.NORTH);
      p5.add(s,BorderLayout.CENTER);
      getContentPane().add(p5,BorderLayout.CENTER);
      btnOpen.addActionListener(this);
      btnSave.addActionListener(this);
      }
      public void actionPerformed(ActionEvent a)
      {
       try
       {
         String arg = a.getActionCommand();
          String str = tfName.getText();
          byte buf[] = new byte[2056];
          int bytes;
         if(arg == "打开文件")
         {
         FileInputStream fileIn = new FileInputStream(str);
         bytes = fileIn.read(buf,0,2056);
         String strin = new String(buf,0,bytes);
         taText.setText(strin);
         }
         else if(arg == "存储文件")
         {
         String str1 = taText.getText();
         buf = str1.getBytes();
         FileOutputStream fileOut = new FileOutputStream(str);
         fileOut.write(buf,0,buf.length);
         fileOut.flush();
         fileOut.close();
         }
        }
        catch(IOException e)
        {
         System.out.println(e.getMessage());
        }
      }
      public static void main(String args[])
      {
       CeShiFile CS = new CeShiFile();
       CS.setSize(300,300);
       CS.show();
      }

      

  4.   

    public static void main(String args[]) {
    CeShiFile CS = new CeShiFile();
    CS.setSize(300,300);
    CS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 加上这句就行
    CS.show();
    }
      

  5.   

    JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      

  6.   

    再补充问一下,怎么能让程序直接双击就能运行呢?
    jar的格式是不是不能直接在电脑上运行?
      

  7.   

    用myeclipse直接打包成jar,能够在带jre环境的电脑里双击运行。