我编写了个小型学生信息输入系统,功能能实现但是不能关闭怎么回事啊下边是在主界面选择插入学生信息后跳转出来的添加信息对话框!功能能实现了,但是点击右上角的×号不能关闭,不知道为什么!请大家帮忙看看哪有差错……package com;import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
import java.awt.event.*;
public class Studialog extends Dialog implements ActionListener{
   JLabel jl1,jl2,jl3,jl4,jl5;
   JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
   JButton jb1,jb2;
   JPanel jp1,jp2,jp3;
    Studialog(Frame owner,String title,boolean model) {
         super(owner,title,model);
         jl1=new JLabel("学号");
         jl2=new JLabel("姓名");
         jl3=new JLabel("性别");
         jl4=new JLabel("年龄");
         jl5=new JLabel("专业");
         jb1=new JButton("确定");
         jb1.addActionListener(this);
         jb2=new JButton("取消");
         jb2.addActionListener(this);
         jtf1=new JTextField(20);
         jtf2=new JTextField(20);
         jtf3=new JTextField(20);
         jtf4=new JTextField(20);
         jtf5=new JTextField(20);
         jp1=new JPanel();
         jp2=new JPanel();
         jp3=new JPanel();
         jp1.setLayout(new GridLayout(5,1));
         jp2.setLayout(new GridLayout(5,1));
         jp1.add(jl1);
         jp1.add(jl2);
         jp1.add(jl3);
         jp1.add(jl4);
         jp1.add(jl5);
         jp2.add(jtf1);
         jp2.add(jtf2);
         jp2.add(jtf3);
         jp2.add(jtf4);
         jp2.add(jtf5);
         jp3.add(jb1);
         jp3.add(jb2);
         this.add(jp1,BorderLayout.WEST);
         this.add(jp2,BorderLayout.CENTER);
         this.add(jp3,BorderLayout.SOUTH);
         this.setSize(400,300);
        
         this.setVisible(true);
     }public void actionPerformed(ActionEvent e) {
     if(e.getSource()==jb1)
     {
         //System.out.println("ok");
         Connection conn=null;
         PreparedStatement ps=null;
         ResultSet rs=null;
         Vector r=new Vector();
         r.add(jtf1.getText().trim());
         r.add(jtf2.getText().trim());
         r.add(jtf3.getText().trim());
         r.add(jtf4.getText().trim());
         r.add(jtf5.getText().trim());
//         String m=(String)r.get(2);
//         System.out.println(m);
    
        
         try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        
           conn=DriverManager.getConnection("jdbc:odbc:mytest","sa","123456");
         ps=conn.prepareStatement("insert into student values(?,?,?,?,?)");
         ps.setString(1, (String)r.get(0));
         ps.setString(2, (String)r.get(1));
         ps.setString(3, (String)r.get(2));
         ps.setString(4, (String)r.get(3));
         ps.setString(5, (String)r.get(4));
         int i= ps.executeUpdate();
        
            
         } catch(Exception arg){
             arg.printStackTrace();
         }finally{
             try {
                 if(ps!=null) ps.close();
                 if(conn!=null) ps.close();
             } catch (SQLException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
             }
         }
     }
     else if(e.getSource()==jb2)
     {
         //System.exit(0);
     }
}
}
 

解决方案 »

  1.   


    Studialog(Frame owner, String title, boolean model) {
    super(owner, title, model);
    jl1 = new JLabel("学号");
    jl2 = new JLabel("姓名");
    jl3 = new JLabel("性别");
    jl4 = new JLabel("年龄");
    jl5 = new JLabel("专业");
    jb1 = new JButton("确定");
    jb1.addActionListener(this);
    jb2 = new JButton("取消");
    jb2.addActionListener(this);
    jtf1 = new JTextField(20);
    jtf2 = new JTextField(20);
    jtf3 = new JTextField(20);
    jtf4 = new JTextField(20);
    jtf5 = new JTextField(20);
    jp1 = new JPanel();
    jp2 = new JPanel();
    jp3 = new JPanel();
    jp1.setLayout(new GridLayout(5, 1));
    jp2.setLayout(new GridLayout(5, 1));
    jp1.add(jl1);
    jp1.add(jl2);
    jp1.add(jl3);
    jp1.add(jl4);
    jp1.add(jl5);
    jp2.add(jtf1);
    jp2.add(jtf2);
    jp2.add(jtf3);
    jp2.add(jtf4);
    jp2.add(jtf5);
    jp3.add(jb1);
    jp3.add(jb2);
    this.add(jp1, BorderLayout.WEST);
    this.add(jp2, BorderLayout.CENTER);
    this.add(jp3, BorderLayout.SOUTH);
    this.setSize(400, 300);
                    //add close event 监听器
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    this.setVisible(true);
    }
      

  2.   

    只关闭这个对话框页!然后回到主界面去!不是整个都关……所以那个System.exit(0)不可以
      

  3.   

    用this.dispose()方法可以实现只关闭当前窗口,试试
      

  4.   

    this.dispose()添加到button 相应函数中 点击可以关闭!但是点击窗口右上角的×号时还是不能关闭!
      

  5.   


    JFrame的:static int EXIT_ON_CLOSE 
              退出应用程序后的默认窗口关闭操作。 
      

  6.   

     void setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
              设置用户在此窗体上发起 "close" 时默认执行的操作。 
      

  7.   

    楼主你真的是很蛋疼,在Studialog(Frame owner, String title, boolean model) {
            super(owner, title, model);
            jl1 = new JLabel("学号");
            jl2 = new JLabel("姓名");
            jl3 = new JLabel("性别");
            jl4 = new JLabel("年龄");
            jl5 = new JLabel("专业");
            jb1 = new JButton("确定");
            jb1.addActionListener(this);
            jb2 = new JButton("取消");
            jb2.addActionListener(this);
            jtf1 = new JTextField(20);
            jtf2 = new JTextField(20);
            jtf3 = new JTextField(20);
            jtf4 = new JTextField(20);
            jtf5 = new JTextField(20);
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            jp1.setLayout(new GridLayout(5, 1));
            jp2.setLayout(new GridLayout(5, 1));
            jp1.add(jl1);
            jp1.add(jl2);
            jp1.add(jl3);
            jp1.add(jl4);
            jp1.add(jl5);
            jp2.add(jtf1);
            jp2.add(jtf2);
            jp2.add(jtf3);
            jp2.add(jtf4);
            jp2.add(jtf5);
            jp3.add(jb1);
            jp3.add(jb2);
            this.add(jp1, BorderLayout.WEST);
            this.add(jp2, BorderLayout.CENTER);
            this.add(jp3, BorderLayout.SOUTH);
            this.setSize(400, 300);
                    //add close event 监听器
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            this.setVisible(true);
        }将 System.exit(0); 改成 Studialog.this.dispose();
    这样不就行了嘛!
      

  8.   

    哎,你就不能变通一下啊
    你采用楼上的那个监听关闭事件,在那个函数里调用就ok了
    即:  this.add(new windowAdaptor(WindowEvent e)
    {
       void windowClosing(windowEvent e)
      {
         this.dispose();//在此次调用销毁当前窗口的方法
      }
    });
      

  9.   

    打错了几个字,不是  this.add(new windowAdaptor(WindowEvent e),
    而是this.addwindowListener(new .....)
      

  10.   

    System.exit(0) 
    Studialog.this.dispose()
    还有this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    三种方式都是可以关闭Frame的,楼主可以试试