谁帮忙看一下为什么没用啊mouseClicked
程序代码:
package com.library.mainFrame; import java.awt.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JPanel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import com.library.bean.*; import com.library.Dao.*; public class login extends JFrame implements MouseListener { private JFrame frame; private JTextField userName; private JTextField passWord; private static final long serialVersionUID = 1L; private static final String TIP = "用户名/手机/邮箱"; private JPanel panel; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { login window = new login(); window.frame.setVisible(true); window.frame.setLocationRelativeTo(null); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public login() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setIconImage(Toolkit.getDefaultToolkit().getImage(login.class.getResource("/com/library/images/IconImage.jpg"))); frame.setTitle("图书管理系统"); frame.setResizable(false); frame.setBounds(100, 100, 793, 598); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JButton login_button = new JButton(""); login_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { User user; UserDao userDao; userDao=new UserDao(); user=userDao.getUser(userName.getText(),passWord.getText()); if(user.getUserName()!=null) { try{ //mainFrame mainframe=new mainFrame(); //mainframe.setVisible(true); frame.setVisible(false); }catch(Exception ex){ ex.printStackTrace(); } }else{ JOptionPane.showMessageDialog(null, "账号或密码错误,请您重新再试!"); userName.setText(""); passWord.setText(""); } } }); login_button.setIcon(new ImageIcon(login.class.getResource("/com/library/images/loginButton.jpg"))); login_button.setBounds(504, 286, 50, 51); frame.getContentPane().add(login_button); panel = new JPanel(); panel.setName("panel"); panel.setBounds(310, 220, 244, 51); frame.getContentPane().add(panel); panel.setLayout(null); panel.addMouseListener (this); userName = new JTextField(TIP); userName.setBounds(0, 0, 244, 51); panel.add(userName); userName.setForeground(Color.LIGHT_GRAY); userName.setFont(new Font("宋体", Font.PLAIN, 20)); userName.setName(""); userName.setToolTipText(""); userName.setColumns(10); userName.addMouseListener(this); passWord = new JTextField(); passWord.setText("密码"); passWord.setForeground(Color.LIGHT_GRAY); passWord.setFont(new Font("宋体", Font.PLAIN, 20)); passWord.setBounds(310, 286, 193, 51); frame.getContentPane().add(passWord); passWord.setColumns(10); JLabel label = new JLabel(""); label.setIcon(new ImageIcon(login.class.getResource("/com/library/images/login.jpg"))); label.setBounds(0, 0, 793, 571); frame.getContentPane().add(label); } @Override public void mouseClicked ( MouseEvent e ) { Component component = e.getComponent (); String name = component.getName (); if (e.getButton () == MouseEvent.BUTTON1) { if ("userName".equals (name)) { JTextField userName = (JTextField) component; if (TIP.equals (userName.getText ())) { userName.setText (""); } } else if ("panel".equals (name)) { JTextField userName = (JTextField) ( (JPanel) component ).getComponents ()[0]; if ("".equals (userName.getText ())) { userName.setText (TIP); } } } } @Override public void mousePressed ( MouseEvent e ) {} @Override public void mouseReleased ( MouseEvent e ) {} @Override public void mouseEntered ( MouseEvent e ) {} @Override public void mouseExited ( MouseEvent e ) {} }用户名不消失