});
/* 添加组件 */ c.add(an1);
/* 设置窗体属性 */
this.setTitle(\动作事件监听器\); this.setSize(180,140); Dimension
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width-this.getWidth())/2, (screenSize.height-this.getHeight())/2); this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
运行结果如下:
注:一般情况下,为事件源做监听事件时使用匿名内部类形式,也可以使用继承ActionListener接口,也可以使用外部类来实现或使用成员内部类继承该接口来实现事件监听器。
? 焦点事件监听器FocusListener
焦点事件(FocusEvent)监听器在实际项目开发中应用也比较广泛,如将光标焦点离开一个文本框时需要弹出一个对话框,或者将焦点返回给该文本框等。
表13.4 焦点事件监听器 事 件 名 称 FocusEvent 例子 事 件 源 Component以及派生类 监 听 接 口 FocusListener 添加或删除相应类型监听器的方法 addFocusListener()、removeFocusListener() import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class Test extends JFrame { /*定义组件 */
private JPanel mb1,mb2;
private JTextField tf1,tf2;
public static void main(String[] args) throws Exception { Test T = new Test(); // 创建本类对象
T.setVisible(true); // 设置显示窗体 }
public Test() throws Exception {
Container c = this.getContentPane(); /* 设置窗体布局管理器 */
c.setLayout(new GridLayout(2, 1)); /* 创建组件 */
tf1=new JTextField(\文本框1\,10); tf2=new JTextField(\文本框2\,10); mb1=new JPanel(); mb2=new JPanel(); /*设置流*/
/* 设置组件属性 */ /* 为组件添加事件 */
tf1.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) {
JOptionPane.showMessageDialog(null,\文本框1失去焦点!\);
}
public void focusGained(FocusEvent e) { } });
/* 添加组件 */ mb1.add(tf1); mb2.add(tf2); c.add(mb1); c.add(mb2);
/* 设置窗体属性 */
this.setTitle(\动作事件监听器\); this.setSize(180,140); Dimension
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width-this.getWidth())/2, (screenSize.height-this.getHeight())/2); this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
运行结果如下:
实践与练习
尝试开发一个登录窗体,包括用户名、密码以及提交按钮和重置按钮,当用户输入用户名mr、密码mrsoft时,弹出登录成功提示对话框。
import java.awt.*; import java.io.*;
import javax.swing.*; import java.awt.event.*; import java.net.*;
public class UserLogin extends JFrame{ /*定义用户名和密码*/
private String userName,password; private File file1,file2;
/*声明组件*/
private JPanel mb1,mb2,mb3,mb4,mb5; private JLabel jl1,jl2,jl3,jl4; private JTextField jt1;
private JPasswordField password1; private JButton an1,an2;
public static void main(String[] args) {
UserLogin User=new UserLogin(); //创建本类对象 User.setVisible(true); //设置窗体显示 }
public UserLogin() { /*转换容器*/
Container c=this.getContentPane(); mb1=(JPanel)c; /*创建组件*/
URL url=UserLogin.class.getResource(\); Icon icon=new ImageIcon(url); jl1=new JLabel(icon); URL jfurl=UserLogin.class.getResource(\);
Icon icon1=new ImageIcon(jfurl);
mb2=new JPanel(); mb3=new JPanel(); mb4=new JPanel(); mb5=new JPanel();
jl2=new JLabel(\用 户 登 录\);
jl3=new JLabel(\用户名:\); jt1=new JTextField(10); jl4=new JLabel(\密 码:\); password1=new JPasswordField(10);
an1=new JButton(\登录\); an2=new JButton(\重置\); /*设置组件属性*/
mb1.setOpaque(false); mb2.setOpaque(false); mb3.setOpaque(false); mb4.setOpaque(false); mb5.setOpaque(false);
jl2.setFont(new Font(null,Font.BOLD,20)); jl3.setFont(new Font(null,Font.PLAIN,16)); jl4.setFont(new Font(null,Font.PLAIN,16)); jt1.setToolTipText(\请输入您的姓名!\);
password1.setToolTipText(\输入的内容必须是6~17字母、数字和下划线!\);
password1.setEchoChar('*'); jl1.setBounds(0,
0,icon.getIconWidth(),icon.getIconHeight()); mb1.setLayout(new GridLayout(4, 1)); /*为组件添加事件*/
password1.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) {
if((password1.getPassword().length==0)&&(jt1.getText().equals(\))){
JOptionPane.showMessageDialog(null,\用户名和密码不能为空!\);
}else if(password1.getPassword().length==0){ JOptionPane.showMessageDialog(null,\密码不能为空!\);
}else if(jt1.getText().equals(\)){
JOptionPane.showMessageDialog(null,\用户名不能为空!\);
} }
public void focusGained(FocusEvent e) {
} });
an1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { file1=new File(\);
file2=new File(\);
if(file1.exists()&&file2.exists()){ this.proof(); }else{ try {
file1.createNewFile(); file2.createNewFile(); this.proof();
} catch (IOException e1) { e1.printStackTrace(); } } }
private void proof(){ try {
FileReader in1=new FileReader(file1);
BufferedReader buin1=new BufferedReader(in1); FileReader in2=new FileReader(file2);
BufferedReader buin2=new BufferedReader(in2); String str1=null; String str2=null;
while((str1=buin1.readLine())!=null&&(str2=buin2.readLine())!=null){
userName=jt1.getText(); password=new
String(password1.getPassword(),0,password1.getPassword().length);
if(str1.equals(userName)&&str2.equals(password)){
JOptionPane.showMessageDialog(null,\登录成功!\);
return; } }
if((password1.getPassword().length!=0)&&!(jt1.getText().equ