监听器的用途和使用方法
接口的使用
实现了程序中途的一次暂停,即可以与用户有一定的互动
点击前运行程序需包括
方法名 参数名 =new 方法名();
对象名.addActionListener(参数名);
点击后运行程序格式为public class 类名 implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
监听器的使用
###主函数为
import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Login { //主函数 程序入口 public static void main(String []args) { Login lgu = new Login(); lgu.initUI(); } //初始化界面 public void initUI() { //实例化\创建一个窗体对象 JFrame lgjf = new JFrame(); //设置窗体 lgjf.setTitle("QQLogin"); lgjf.setSize(400,400); lgjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 进程关闭 lgjf.setLocationRelativeTo(null);//居中显示 //创建流式布局器 FlowLayout fl = new FlowLayout(); //设置布局 lgjf.setLayout(fl); /** * 创建组件 */ //图片 ImageIcon image = new ImageIcon("E:/abc.jpg"); //标签 JLabel imjl = new JLabel(image); JLabel nameJl = new JLabel("账号:"); JLabel pwdJl = new JLabel("密码:"); //尺寸 Dimension jtddim= new Dimension(330,30); // 输入框 JTextField namejtx = new JTextField(); namejtx.setPreferredSize(jtddim); JPasswordField pwdjtx = new JPasswordField(); pwdjtx.setPreferredSize(jtddim); //按钮 JButton lgbtn = new JButton("登录"); /** * 添加组件 */ lgjf.add(imjl); lgjf.add(nameJl); lgjf.add(namejtx); lgjf.add(pwdJl); lgjf.add(pwdjtx); lgjf.add(lgbtn); lgjf.setVisible(true); LoginListener jr=new LoginListener(); jr.namef=namejtx; jr.passwordf =pwdjtx; lgbtn.addActionListener(jr); jr.h=lgjf; }}###监听器代码为
import javax.swing.JFrame; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginListener implements ActionListener { JTextField namef; JPasswordField passwordf; JFrame h=new JFrame(); public void actionPerformed(ActionEvent e){ String names=namef.getText(); String passwords=passwordf.getText(); if(names.equals("123")&&passwords.equals("123")){ System.out.println("登陆成功"); Login2 a=new Login2(); a.initUI(); h.setVisible(false); } System.out.println("按钮被电击了"); } }另一个页面
import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Login2 { //主函数 程序入口 public static void main(String []args) { Login2 lgu = new Login2(); lgu.initUI(); } //初始化界面 public void initUI() { //实例化\创建一个窗体对象 JFrame lgjf = new JFrame(); //设置窗体 lgjf.setTitle("QQLogin"); lgjf.setSize(400,400); lgjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 进程关闭 lgjf.setLocationRelativeTo(null);//居中显示 //创建流式布局器 FlowLayout fl = new FlowLayout(); //设置布局 lgjf.setLayout(fl); /** * 创建组件 */ //图片 ImageIcon image = new ImageIcon("E:/abc.jpg"); //标签 JLabel imjl = new JLabel(image); JLabel nameJl = new JLabel("账号:"); JLabel pwdJl = new JLabel("密码:"); //尺寸 Dimension jtddim= new Dimension(330,30); // 输入框 JTextField namejtx = new JTextField(); namejtx.setPreferredSize(jtddim); JPasswordField pwdjtx = new JPasswordField(); pwdjtx.setPreferredSize(jtddim); //按钮 JButton lgbtn = new JButton("登录"); /** * 添加组件 */ lgjf.add(imjl); lgjf.add(nameJl); lgjf.add(namejtx); lgjf.add(pwdJl); lgjf.add(pwdjtx); lgjf.add(lgbtn); lgjf.setVisible(true); }}这样实现了页面的跳转和监视器功能
