我编写了个小型学生信息输入系统,功能能实现但是不能关闭怎么回事啊下边是在主界面选择插入学生信息后跳转出来的添加信息对话框!功能能实现了,但是点击右上角的×号不能关闭,不知道为什么!请大家帮忙看看哪有差错……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);
}
}
}
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);
}
}
}
解决方案 »
- 新手请教JAVA数据库问题
- public void launchFrame() 是干什么用的 在API中找不到它?
- 一个关于引用类的问题~望高手给菜鸟的我解答
- interface的成员为什么只能是static and final的????而抽象类可以是任何类型
- 问题
- 关于java与硬件交互的问题,请高手回答!
- 一个非常急的问题,谢谢各位兄弟分以西一下原因
- JAVA 实现对 RTF文件 的读操作,提供ITEXT包!!!
- java如何调用com接口并传入参数???急啊!再线等待!
- 哪儿有javacc下
- 请教:如果不理会异常,用catch(Throwable e){}是否可行?
- Thinking In Java 的问题
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);
}
JFrame的:static int EXIT_ON_CLOSE
退出应用程序后的默认窗口关闭操作。
设置用户在此窗体上发起 "close" 时默认执行的操作。
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();
这样不就行了嘛!
你采用楼上的那个监听关闭事件,在那个函数里调用就ok了
即: this.add(new windowAdaptor(WindowEvent e)
{
void windowClosing(windowEvent e)
{
this.dispose();//在此次调用销毁当前窗口的方法
}
});
而是this.addwindowListener(new .....)
Studialog.this.dispose()
还有this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
三种方式都是可以关闭Frame的,楼主可以试试