您的位置:首页 > 编程语言 > Java开发

【java学习】接口

2016-04-20 18:58 211 查看
1、接口

     a、 定义

          public interface 接口名 extends 接口,……{

                             //定义常量

                             public static final 数据类型 常量名 = 值;

                             //定义抽象方法的格式

                             public abstract 返回值类型 方法名(数据类型 参数名1,参数名2……);

          }

       【注意:

         ·接口可以继承一个或多个父类接口

         ·接口只有一种访问修饰符public

        · 接口默认提供static,final,abstract关键字

         ·接口不能实例化对象                                              】

      b、实现

            public class 类名 extends 类名 implements 接口,……{

                                    //类实现接口中所有的抽象方法

            }

           

     c、接口与类的区别

          · 接口只能继承多个接口,类可以继承单个类和多个接口

          · 接口不可以实例化对象,类可以实例化对象  (所以要定义一个类来实现对应的事件接口,并实现接口中的抽象方法)

          · 接口得到修饰符只能有public,static,final,abstract,而类可以使用所有的修饰符

2、事件

           事件源:

          · 只有AWT或SWING提供的容器组件和元素组件类对象才能成为事件源对象

          ·  外界执行的动作发生在哪一个组件上,哪个组件便为事件源对象

        

           事件监听方法:

           addActionListener(ActionListener l);       //监听按钮的点击和输入框的键盘回车

          
addMouseListener(MouseListener l);         // 监听在事件源上是否有鼠标进入、离开、按下、释放、单击

          
addMouseMotionListener(MouseMotionListener l);       //监听在事件源上是否有鼠标移动、拖动

           addKeyListener(KeyListener l);            //监听事件源上是否有键盘按键按下,释放、敲击

           事件接口(事件处理类)

          
ActionListener                      动作事件接口

           MouseListener                    鼠标事件接口

           MouseMotionListener        鼠标移动事件接口

           KeyListener                          键盘事件接口

           举例:

          在登录界面点击一个按钮

          分析:

                    事件源对象:登录按钮

                    事件监听方法:addActionListener(ActionListener l);

                    事件接口(事件处理类):ActionListener

3、练习

        实现登录功能步骤:

        ·定义一个LoginListener类,该类实现ActionListener动作事件接口,实现接口中的抽象方法。

        ·实例化窗体对象,设置窗体的属性值。

        ·在Login类中,实例化LoginListener事件处理类的对象。

        ·给事件源buttonLogin按钮添加动作监听方法,指定事件处理类的对象名

Login类:

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 extends JFrame{

public static void main(String[] args)
{
Login l=new Login();
l.initUI();
}

public void initUI(){
setTitle("Login");
setSize(300,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setResizable(false);

setBackground();
FlowLayout f=new FlowLayout();
setLayout(f);

ImageIcon imag = new ImageIcon(this.getClass().getResource("test.png"));
JLabel a=new JLabel(im
9570
ag);
add(a);

JLabel b=new JLabel("账号:");
add(b);
JTextField c=new JTextField("admin");
c.setPreferredSize(new Dimension(220,30));
add(c);
JLabel d=new JLabel("密码:");
add(d);
JPasswordField e=new JPasswordField("password");
e.setPreferredSize(new Dimension(220,30));
add(e);

JCheckBox jc1=new JCheckBox("记住密码");
jc1.setOpaque(false);
jc1.setPreferredSize(new Dimension(120,30));
add(jc1);
JCheckBox jc2=new JCheckBox("自动登录");
jc2.setOpaque(false);
jc2.setPreferredSize(new Dimension(120,30));
add(jc2);

JButton b1=new JButton("登录");
b1.setBackground(new Color(100,20,30));
b1.setPreferredSize(new Dimension(150,30));
add(b1);

Chuang cc=new Chuang();  //要在初始化方法中定义!不要再main函数

e.addActionListener(cc);
b1.addActionListener(cc);

cc.huoqu(c,e, this);

setVisible(true);
}

public void setBackground()
{
ImageIcon imag = new ImageIcon(this.getClass().getResource("background.png"));
JLabel a=new JLabel(imag);
a.setBounds(0, 0, 300, 600);
getLayeredPane().add(a,new Integer(Integer.MIN_VALUE));

JPanel p=(JPanel)getContentPane();
p.setOpaque(false);

}
}


Chuang类:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Chuang implements ActionListener{
private JTextField textname,textpass;
private JFrame login;
public void huoqu(JTextField textname,JTextField textpass,JFrame login){
this.textname=textname;
this.textpass=textpass;
this.login=login;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name,password;
name=textname.getText();
password=textpass.getText();
if(name.equals("admin") && password.equals("password")){ //不能用"=="
JFrame jf=new JFrame();
jf.setSize(600,800);
jf.setTitle("画图");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(2);
jf.setVisible(true);

login.dispose();
}
else{
JOptionPane.showMessageDialog(login, "密码错误,请重新输入");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 接口