package cm_system;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.xml.ws.handler.Handler;
;public class LoginFrame extends JFrame {
JFrame loginframe;
//添加登录界面的pannel
JPanel a=new JPanel();
JPanel b=new JPanel();
JPanel c=new JPanel();
JPanel d=new JPanel();
JPanel e=new JPanel();
JPanel f=new JPanel();
//定义登录按钮
JButton login=new JButton("登录");
JButton reset=new JButton("重置");
//定义用户名和密码标签
JLabel userName= new JLabel("用户名");
JLabel passWord=new JLabel("密码");
//文本框
JTextField userText=new JTextField(15);
JPasswordField passwordF=new JPasswordField(15);
public LoginFrame(){
JFrame loginframe=new JFrame();
//设置外形
loginframe.setBounds(300, 300, 200, 100);
loginframe.setTitle("登陆界面");
loginframe.setSize(350, 200);
loginframe.setLayout(new GridLayout(3, 1));
a.add(userName);
a.add(userText);
loginframe.add(a);
b.add(passWord);
b.add(passwordF);
loginframe.add(b);
c.add(login);
c.add(reset);
loginframe.add(c);
loginframe.setVisible(true);
//做登录的监听
login.addActionListener(new loginAction());
reset.addActionListener(new resetAction());
}
//登陆的监听
public class loginAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == login) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/cm_system", "root", "root");// 数据库名为Cm_system,密码为root
System.out.println("com : "+ con);
java.sql.Statement cmd = con.createStatement();
String sql = "select * from cm_user where User_name='"
+ userText.getText() + "' and User_password='"
+ passwordF.getText() + "'" ;
ResultSet rs =cmd.executeQuery(sql);// 表名为user,user_name和User_passwoed是存放用户名和密码的字段名
if (rs.next()) {
mainFrame.setVisible(true);
loginframe.setVisible(false);
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
}
}
}
}
//重置的监听
public class resetAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
userText.setText("");
passwordF.setText("");
}}
public static void main(String[] args) {
new LoginFrame();
}
}
问题:
mainFrame.setVisible(true);
loginframe.setVisible(false);
为什么点击登陆之后这个loginframe没有关闭,也试了system.exit(0),还有defaultnode 的方法,都不可以,这是为什么呀?
菜鸟求大神助攻TAT
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.xml.ws.handler.Handler;
;public class LoginFrame extends JFrame {
JFrame loginframe;
//添加登录界面的pannel
JPanel a=new JPanel();
JPanel b=new JPanel();
JPanel c=new JPanel();
JPanel d=new JPanel();
JPanel e=new JPanel();
JPanel f=new JPanel();
//定义登录按钮
JButton login=new JButton("登录");
JButton reset=new JButton("重置");
//定义用户名和密码标签
JLabel userName= new JLabel("用户名");
JLabel passWord=new JLabel("密码");
//文本框
JTextField userText=new JTextField(15);
JPasswordField passwordF=new JPasswordField(15);
public LoginFrame(){
JFrame loginframe=new JFrame();
//设置外形
loginframe.setBounds(300, 300, 200, 100);
loginframe.setTitle("登陆界面");
loginframe.setSize(350, 200);
loginframe.setLayout(new GridLayout(3, 1));
a.add(userName);
a.add(userText);
loginframe.add(a);
b.add(passWord);
b.add(passwordF);
loginframe.add(b);
c.add(login);
c.add(reset);
loginframe.add(c);
loginframe.setVisible(true);
//做登录的监听
login.addActionListener(new loginAction());
reset.addActionListener(new resetAction());
}
//登陆的监听
public class loginAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == login) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/cm_system", "root", "root");// 数据库名为Cm_system,密码为root
System.out.println("com : "+ con);
java.sql.Statement cmd = con.createStatement();
String sql = "select * from cm_user where User_name='"
+ userText.getText() + "' and User_password='"
+ passwordF.getText() + "'" ;
ResultSet rs =cmd.executeQuery(sql);// 表名为user,user_name和User_passwoed是存放用户名和密码的字段名
if (rs.next()) {
mainFrame.setVisible(true);
loginframe.setVisible(false);
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
}
}
}
}
//重置的监听
public class resetAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
userText.setText("");
passwordF.setText("");
}}
public static void main(String[] args) {
new LoginFrame();
}
}
问题:
mainFrame.setVisible(true);
loginframe.setVisible(false);
为什么点击登陆之后这个loginframe没有关闭,也试了system.exit(0),还有defaultnode 的方法,都不可以,这是为什么呀?
菜鸟求大神助攻TAT