登陆窗体无法实现??? ( 积分: 100 )

  • 主题发起人 主题发起人 野汉
  • 开始时间 开始时间

野汉

Unregistered / Unconfirmed
GUEST, unregistred user!
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent2 extends WindowAdapter { //implements ActionListener{(这个加上就编译不了了?不知什么原因啊?)
TextField tf1,tf2;
Panel p1,p2,p3,p4,p5;
Label l1,l2;
Dialog dlg;
Frame f;
String password,password_from;
public void go(){
f = new Frame("登录窗体");
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
tf1=new TextField();
tf2=new TextField();
l1=new Label("姓名");
//Font lf1=new Font("Dialog",Font.PLAIN,20);
//l1.setFont(lf1);
l2=new Label("密码");
Font lf2=new Font("Helvetica",Font.PLAIN,20);
l2.setFont(lf2);
tf1.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf1_focuslost(e);}
});(这一段是注册监听器,那参数里写的是什么意思啊?下边四个都是这样啊?)
tf2.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf2_focuslost(e);}
});
Button b1 = new Button("确定");
b1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b1_actionPerformed(e);}
});
Button b2 = new Button("退出");
b2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b2_actionPerformed(e);}
});
p1.setLayout(new GridLayout(3,2,30,30));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
f.add(p1,"Center");
f.add(p2,"East");
f.add(p3,"South");
f.add(p4,"West");
f.add(p5,"North");
f.setSize(300,200);
f.setBackground(Color.white);
f.setLocation(200,500);
f.setVisible(true);
}
public static void main(String args[]) {
TestActionEvent2 sb=new TestActionEvent2();
sb.go();
}
public void b1_actionPerformed(ActionEvent e){ // b1_actionPerformed这段有错吗?为什么不运行TRY里的内容啊?运行出来点击确定按钮没有用啊!直接输出的是CATCH的语句?
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:dog", "", "");
Statement stmt=con.createStatement();
String sql="select * from yh";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
if((tf1.getText()).equals(rs.getString("name"))){
password_from=rs.getString("password");
if((password).equals(password_from)){
dlg = new Dialog(f,"登录成功",true);
dlg.add("Center",new Label("您已经成功登录!",Label.CENTER));
dlg.setSize(250,100);
//dlg.pack();
dlg.addWindowListener(this);
dlg.setLocation(250,250);
dlg.setVisible(true);
//JOptionPane.showMessageDialog(this,"密码正确!","登录信息",JOptionPane.INFORMATION_MESSAGE);(这句什么意思啊?书上写的为什么运行不了啊?所以我改成上面的了,不知道为什么啊?)
}
else
{
dlg = new Dialog(f,"登录失败",true);
dlg.add("Center",new Label("用户不存在!请确认输入正确",Label.CENTER));
dlg.setSize(250,100);
dlg.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e) {
dlg_windowClosing(e);}});
dlg.setLocation(250,250);
dlg.setVisible(true);
//JOptionPane.showMessageDialog(this,"密码和用户名不符,请重新输入密码!","登录信息",JOptionPane.INFORMATION_MESSAGE););

} }
stmt.close();
con.close();}
catch(Exception ex){
System.out.println("连接数据库有问题");
}
}
public void dlg_windowClosing(WindowEvent e){
dlg.setVisible(false);
}
public void b2_actionPerformed(ActionEvent e){
System.exit(0);
}
public void tf1_focuslost(FocusEvent e){
}
public void tf2_focuslost(FocusEvent e){
password=tf2.getText();
}
}
我是一JAVA的初学者,希望各师哥师姐多多帮忙,这是我自己看书,自己编写的,不足之处多多指教,谢谢!
 
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent2 extends WindowAdapter { //implements ActionListener{(这个加上就编译不了了?不知什么原因啊?)
TextField tf1,tf2;
Panel p1,p2,p3,p4,p5;
Label l1,l2;
Dialog dlg;
Frame f;
String password,password_from;
public void go(){
f = new Frame("登录窗体");
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
tf1=new TextField();
tf2=new TextField();
l1=new Label("姓名");
//Font lf1=new Font("Dialog",Font.PLAIN,20);
//l1.setFont(lf1);
l2=new Label("密码");
Font lf2=new Font("Helvetica",Font.PLAIN,20);
l2.setFont(lf2);
tf1.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf1_focuslost(e);}
});(这一段是注册监听器,那参数里写的是什么意思啊?下边四个都是这样啊?)
tf2.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf2_focuslost(e);}
});
Button b1 = new Button("确定");
b1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b1_actionPerformed(e);}
});
Button b2 = new Button("退出");
b2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b2_actionPerformed(e);}
});
p1.setLayout(new GridLayout(3,2,30,30));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
f.add(p1,"Center");
f.add(p2,"East");
f.add(p3,"South");
f.add(p4,"West");
f.add(p5,"North");
f.setSize(300,200);
f.setBackground(Color.white);
f.setLocation(200,500);
f.setVisible(true);
}
public static void main(String args[]) {
TestActionEvent2 sb=new TestActionEvent2();
sb.go();
}
public void b1_actionPerformed(ActionEvent e){ // b1_actionPerformed这段有错吗?为什么不运行TRY里的内容啊?运行出来点击确定按钮没有用啊!直接输出的是CATCH的语句?
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:dog", "", "");
Statement stmt=con.createStatement();
String sql="select * from yh";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
if((tf1.getText()).equals(rs.getString("name"))){
password_from=rs.getString("password");
if((password).equals(password_from)){
dlg = new Dialog(f,"登录成功",true);
dlg.add("Center",new Label("您已经成功登录!",Label.CENTER));
dlg.setSize(250,100);
//dlg.pack();
dlg.addWindowListener(this);
dlg.setLocation(250,250);
dlg.setVisible(true);
//JOptionPane.showMessageDialog(this,"密码正确!","登录信息",JOptionPane.INFORMATION_MESSAGE);(这句什么意思啊?书上写的为什么运行不了啊?所以我改成上面的了,不知道为什么啊?)
}
else
{
dlg = new Dialog(f,"登录失败",true);
dlg.add("Center",new Label("用户不存在!请确认输入正确",Label.CENTER));
dlg.setSize(250,100);
dlg.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e) {
dlg_windowClosing(e);}});
dlg.setLocation(250,250);
dlg.setVisible(true);
//JOptionPane.showMessageDialog(this,"密码和用户名不符,请重新输入密码!","登录信息",JOptionPane.INFORMATION_MESSAGE););

} }
stmt.close();
con.close();}
catch(Exception ex){
System.out.println("连接数据库有问题");
}
}
public void dlg_windowClosing(WindowEvent e){
dlg.setVisible(false);
}
public void b2_actionPerformed(ActionEvent e){
System.exit(0);
}
public void tf1_focuslost(FocusEvent e){
}
public void tf2_focuslost(FocusEvent e){
password=tf2.getText();
}
}
我是一JAVA的初学者,希望各师哥师姐多多帮忙,这是我自己看书,自己编写的,不足之处多多指教,谢谢!
 
进来一看,晕菜~JAVA的。大哥,这里是delphi论坛哦~以为是delphi的,准备来抢分呢
 
代碼有問題,修改后的代碼如下:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestActionEvent2 extends WindowAdapter implements ActionListener{ //(這個加上就編譯不了了?不知什?原因啊?)
TextField tf1, tf2;
Panel p1, p2, p3, p4, p5;
Label l1, l2;
Dialog dlg;
Frame f;
String password, password_from;
/*
public void actionPerformed(ActionEvent e){
// b1_actionPerformed(e);

}
*/
public void go(){
f = new Frame("登錄表單");
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
tf1=new TextField();
tf2=new TextField();
l1=new Label("姓名");
Font lf1=new Font("Dialog",Font.PLAIN,20);
l1.setFont(lf1);
l2=new Label("密碼");
Font lf2=new Font("Helvetica",Font.PLAIN,20);
l2.setFont(lf2);

tf1.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf1_focuslost(e);}
});
//(這一段是註冊監聽器,那參數裏寫的是什?意思啊?下邊四個都是這樣啊?)
tf2.addFocusListener(new java.awt.event.FocusListener(){
public void focusLost(FocusEvent e) {
tf2_focusLost(e);}

public void focusGained(FocusEvent e) {
tf2_focusGained(e);}
}
);

Button b1 = new Button("確定");
b1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b1_actionPerformed(e);}
});

Button b2 = new Button("退出");
b2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
b2_actionPerformed(e);}
});

p1.setLayout(new GridLayout(3,2,30,30));
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
f.add(p1,"Center");
f.add(p2,"East");
f.add(p3,"South");
f.add(p4,"West");
f.add(p5,"North");
f.setSize(300,200);
f.setBackground(Color.white);
f.setLocation(200,500);
f.setVisible(true);

}

public static void main(String args[]) {
TestActionEvent2 sb = new TestActionEvent2();
sb.go();
}
public void b1_actionPerformed(ActionEvent e) { // b1_actionPerformed這段有錯嗎??什?不運行TRY裏的內容啊?運行出來點擊確定按鈕沒有用啊!直接輸出的是CATCH的語句?

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException et) {
System.err.print("ClassNotFoundException: ");
System.err.println(et.getMessage());
System.out.println("odbc err");
}
try {
Connection con = DriverManager.getConnection("jdbc:odbc:dog", "","");
Statement stmt = con.createStatement();
String sql = "select * from yh";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())

if ((tf1.getText()).toString().equals(rs.getString("name"))) {
System.out.println("tf1="+tf1.getText().toString());
password_from = rs.getString("password");
if ((password).equals(password_from)) {
System.out.println("password_from="+password_from);
JOptionPane.showMessageDialog(f,"密碼正確!","登錄資訊",JOptionPane.INFORMATION_MESSAGE);
//(這句什?意思啊?書上寫的?什?運行不了啊?所以我改成上面的了,不知道?什?啊?)
} else
{
JOptionPane.showMessageDialog(f,"密碼和用戶名不符,請重新輸入密碼!","登錄資訊",JOptionPane.INFORMATION_MESSAGE);
};
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
System.out.println("connection err");
}
/*
} catch (Exception ex) {
System.out.println("連接資料庫有問題");
}
*/
}
public void dlg_windowClosing(WindowEvent e) {
dlg.setVisible(false);
}
public void b2_actionPerformed(ActionEvent e) {
System.exit(0);
}
public void tf1_focuslost(FocusEvent e) {
}
public void tf2_focusLost(FocusEvent e) {
password = tf2.getText();
}
public void tf2_focusGained(FocusEvent e) {

}
}
 
1
public class TestActionEvent2 extends WindowAdapter { //implements ActionListener{(这个加上就编译不了了?不知什么原因啊?)
你没有实现actionPerformed(ActionEvent e)
2
大小写的问题
程序中的addFocusListener
tf1.addFocusListener(new java.awt.event.FocusAdapter(){
//注意,public void focusLost(FocusEvent e)这里L是大写
你这边变成了小写,事件就没有响应,所以后面出问题
public void focuslost(FocusEvent e) {
tf1_focuslost(e);}
});
tf2.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf2_focuslost(e);}
});
3 先编译看出错信息,然后查询那边出错
 
tf1.addFocusListener(new java.awt.event.FocusAdapter(){
public void focuslost(FocusEvent e) {
tf1_focuslost(e);}
});
这段的意思是不是给tf1注册监听器,用FocusAdapter适配器里的focuslost这个方法啊?
 
是的,注册监听器
错误是大小写弄错了,造成程序认为是另一个方法
focuslost()和focusLost()是不一样的
你这段程序是书上的?
 
是书上的,谢谢哥们了
 
后退
顶部