import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import javax.swing.BoxLayout;
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 java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main extends JFrame {
private static Main frame;
private final JPanel panel = new JPanel();
private final JLabel label = new JLabel();
private final JLabel label_1 = new JLabel();
private final JLabel label_2 = new JLabel();
private final JLabel label_3 = new JLabel();
private final JPasswordField passwordField = new JPasswordField();
private final JButton button_1 = new JButton();
private final JButton button = new JButton();
private final JTextField textField = new JTextField();
private final JButton button_2 = new JButton(); public static void main(String args[]) 
{ frame = new Main();
frame.setVisible(true);

public Main() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setBackground(Color.GRAY);
setJMenuBar(null);

getContentPane().add(panel);
panel.setLayout(null);
label.setName("label");
label.setBounds(125, 0, 260, 85);
label.setBackground(Color.BLACK);
panel.add(label);
label.setActionMap(null);
label.setText("学生信息管理系统");
label.setFont(new Font("宋体", Font.BOLD, 28));

panel.add(label_1);
label_1.setText("登录界面:");
label_1.setBounds(210, 110, 66, 18);

panel.add(label_2);
label_2.setText("用户名:");
label_2.setBounds(90, 170, 66, 18);

panel.add(label_3);
label_3.setText("密码:");
label_3.setBounds(90, 224, 66, 18);
label_3.addKeyListener(new ISBNkeyListener());

panel.add(passwordField);
passwordField.setBounds(165, 223, 193, 20);

panel.add(button_1);
button_1.setText("登录");
button_1.setBounds(376,215, 106, 28);

panel.add(button);
button.setText("找回密码");
button.setBounds(112, 304, 106, 28);

panel.add(textField);
textField.setBounds(162, 168, 193, 20);

panel.add(button_2);
button_2.setText("退出系统");
button_2.setBounds(252, 304, 106, 28);
button.addActionListener(new getbackpassword_actionAdapter(this));
button_1.addActionListener(new login_actionAdapter(this));
button_2.addActionListener(new canel_actionAdapter(this));
}
// 登录
class login_actionAdapter implements java.awt.event.ActionListener {
Main adaptee; login_actionAdapter(Main adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.login_actionPerformed(e);
}
}

public void login_actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("登录"))
{
try
{
JOptionPane.showMessageDialog(null, "登陆成功!");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:Student","sa","123");
Statement c=conn.createStatement();
ResultSet rs=c.executeQuery("select * from PasswordTable where UserID='"+textField.getText()+"' and LoginPassword='"+passwordField.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null, "登陆成功!");
String userName = rs.getString("UserID");  
String password = rs.getString("LoginPassword");
                int Right = rs.getInt("Right");
// Main_frames ma=new Main_frames(frame,userName,password,Right);
// ma.setVisible(true);
rs.close();
dispose();
}
else
JOptionPane.showMessageDialog(null,"用户名或密码错误!");

catch (Exception err) {
String error = err.getMessage();
JOptionPane.showMessageDialog(null, error);
}}


// button_1.addKeyListener(new KeyAdapter()
// {
// public void keyPressed(KeyEvent e)
// {
// int   keyCode=e.getKeyCode();
//           if(keyCode==KeyEvent.VK_ENTER )
// { button_1.doClick();
//
// }
//
// }
// });
}

//退出系统
public void canel_actionPerformed(ActionEvent e) {
System.exit(0);
}
class canel_actionAdapter implements ActionListener {
Main adaptee; canel_actionAdapter(Main adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.canel_actionPerformed(e);
}
}//找回密码
public void getbackpassword_actionPerformed(ActionEvent e)
  {
// getPassword ma=new getPassword();
// ma.setVisible(true);
  }
class getbackpassword_actionAdapter implements java.awt.event.ActionListener 
{
Main adaptee;

getbackpassword_actionAdapter(Main adaptee) 
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) 
{
adaptee.getbackpassword_actionPerformed(e);
}
}
class ISBNkeyListener extends KeyAdapter{
public void keyPressed(final KeyEvent e){
if(e.getKeyCode()==13){
button_1.doClick();
}
}
} }我做了一个登录,想输完密码,安回车。会点击登录,可是不知道为什么没有反应。
                  panel.add(label_3);
label_3.setText("密码:");
label_3.setBounds(90, 224, 66, 18);
label_3.addKeyListener(new ISBNkeyListener());

回车事件:
class ISBNkeyListener extends KeyAdapter{
public void keyPressed(final KeyEvent e){
if(e.getKeyCode()==13){
button_1.doClick();
}
}
}

请大家帮忙看看,到底是哪里错误。

解决方案 »

  1.   

    按回车不是这么做的JFrame 中有个 getRootPanel().setDefaultButton(xxx); 把面板上默认按钮的变量放到里面。
      

  2.   

    其实更主要的是,LZ监听器添加错地方了...
    你怎么也该给passwordField添加监听,却添加到label_3了
    且回车ascii码是10(换行,win下如此,不知其他平台,所以你最好10,13-回车一起判断)
      

  3.   

    楼主应该给passwordField添加监听,可以用addActionListener()来实现监听,它会监听摁下enter键的,以下摘自API
    文本字段如何使用 VK_ENTER 事件取决于文本字段是否具有任何操作侦听器。如果具有操作侦听器,则 VK_ENTER 导致侦听器获取一个 ActionEvent,并使用 VK_ENTER 事件。这与 AWT 文本字段处理 VK_ENTER 事件的方式是兼容的。如果文本字段没有操作侦听器,则从 1.3 版本开始不使用 VK_ENTER 事件。而是处理祖先组件的绑定,这将启用 JFC/Swing 的默认按钮特性。