package cn.user;
import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.TitledBorder;
import java.awt.event.*; 
import java.sql.*; 
class UserLogin extends JFrame implements ActionListener{
private JLabel username;
private JLabel password;
private JTextField usertext;
private JPasswordField passwordfield;
private JButton land;
private JButton exit;

private PreparedStatement pre; 
private ResultSet res; 
public static void main(String[] args){
UserLogin login=new UserLogin();
login.setVisible(true);
//System.out.println("yes");
}

public UserLogin(){
super();
setTitle("登录界面");
setBounds(100,100,260,210);
getContentPane().setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel username=new JLabel("用户名:");
JTextField usertext=new JTextField();
username.setBounds(38, 83, 60, 15);
this.add(username);
usertext.setBounds(89,80,120,21);
this.add(usertext);

JLabel password=new JLabel("密码:");
JPasswordField passwordfield=new JPasswordField();
password.setBounds(39, 107, 60, 15);
this.add(password);
passwordfield.setBounds(89, 104, 120, 21);
passwordfield.setEchoChar('*');
this.add(passwordfield);

JLabel lb=new JLabel();
lb.setBorder(new TitledBorder(null,"",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION,null,null));
lb.setForeground(new Color(255,0,0));
lb.setFont(new Font("",Font.BOLD,18));
lb.setText("     图书管理系统");
lb.setBounds(50,28,170,36);
getContentPane().add(lb);

JButton land=new JButton("登录");
land.setBounds(67,131,60,23);
this.add(land);

JButton exit=new JButton("退出");
exit.setBounds(141,131,60,23);
this.add(exit);

land.addActionListener(this);
exit.addActionListener(this);
this.validate(); 

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//System.out.println("yes");
if(e.getSource()==land){ 
System.out.println("yes");
JFrame jFrame = new JFrame();jFrame.setBounds(10, 10, 100, 100);jFrame.setTitle("弹出框"); jFrame.setVisible(true);
}
else if(e.getSource()==exit){
System.out.println("no");
JFrame jFrame = new JFrame();jFrame.setBounds(10, 10, 100, 100);jFrame.setTitle("弹出框"); jFrame.setVisible(true);
}

}

}

解决方案 »

  1.   

    e.getSource()返回值是Object类型的,将它强制转换成JComponent类型的
      

  2.   

    String cmd=e.getActionCommand();
    if(cmd.equals("确定")){
    f.hide();
    }
    if(cmd.equals("返回"))
    f.hide();
      

  3.   

    this.add(land);
    this.add(exit);

    land.addActionListener(this);
    exit.addActionListener(this);
    都是this,分别指的是什么,楼主研究一下~~~
    ------------------------------------------------添加监听器的代码改成下面这样就可以了。land.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent actionevent) {
    System.out.println("actionlistener...");

    }
    });
      

  4.   

    不好意思楼主,理解错了。。------------------------------e.getSource() == land    这个判断不成立的原因是:你的land是静态变量,初始化的时候就已经存在了。
    而发生点击事件的JButton是你重新new的一个对象,所以==判断肯定是false。在创建登陆和退出按钮的时候,用静态变量land和exit指向你这两个按钮,就可以了land = new JButton("登录");
    land.setBounds(67, 131, 60, 23);
    this.add(land); exit = new JButton("退出");
    exit.setBounds(141, 131, 60, 23);
    this.add(exit);
      

  5.   

    不好意思楼主,理解错了。。------------------------------e.getSource() == land    这个判断不成立的原因是:你的land是静态变量,初始化的时候就已经存在了。
    而发生点击事件的JButton是你重新new的一个对象,所以==判断肯定是false。在创建登陆和退出按钮的时候,用静态变量land和exit指向你这两个按钮,就可以了land = new JButton("登录");
    land.setBounds(67, 131, 60, 23);
    this.add(land); exit = new JButton("退出");
    exit.setBounds(141, 131, 60, 23);
    this.add(exit);
    厉害厉害  终于搞懂了 谢谢大神 !!!感激不尽!!!
      

  6.   

    不好意思楼主,理解错了。。------------------------------e.getSource() == land    这个判断不成立的原因是:你的land是静态变量,初始化的时候就已经存在了。
    而发生点击事件的JButton是你重新new的一个对象,所以==判断肯定是false。在创建登陆和退出按钮的时候,用静态变量land和exit指向你这两个按钮,就可以了land = new JButton("登录");
    land.setBounds(67, 131, 60, 23);
    this.add(land); exit = new JButton("退出");
    exit.setBounds(141, 131, 60, 23);
    this.add(exit);
    厉害厉害  终于搞懂了 谢谢大神 !!!感激不尽!!!距离大神还有些差距~~~
    楼主有空结一下贴哈,结帖率高了,以后提问的时候回复的人会多的。也能给回复者积分奖励,提高回复者的激情~~~