代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Text7 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);//创建窗口
frame.setLocationRelativeTo(null);//设置居中
frame.setLayout(null);//取消默认布局
frame.setDefaultCloseOperation(3);//设置关闭模式
JButton btn = new JButton("按钮");//创建按钮
btn.setBounds(0, 0, 100, 100);//设置按钮位置以及大小
frame.getContentPane().add(btn);//获取窗口并向窗口添加按钮
btn.addActionListener(new actionListionimp());//Action是一个接口需要被实现
frame.setVisible(true);//窗口可视化
//事件源btn,确定动作:按钮被按下了 or 空格被按下了
}
}
class actionListionimp implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {//实现方法
// TODO Auto-generated method stub
System.out.println("按钮被按下了");
}
}
代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Text7 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);//创建窗口
frame.setLocationRelativeTo(null);//设置居中
frame.setLayout(null);//取消默认布局
frame.setDefaultCloseOperation(3);//设置关闭模式
JButton btn = new JButton("按钮");//创建按钮
btn.setBounds(0, 0, 100, 100);//设置按钮位置以及大小
frame.getContentPane().add(btn);//获取窗口并向窗口添加按钮
btn.addMouseListener(new mouseListenerimpl());
frame.setVisible(true);//窗口可视化
//事件源btn,确定动作:按钮被按下了 or 空格被按下了
}
}
class mouseListenerimpl implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标被点击了");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标被按下了");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标被松开了");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标划入了");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标划出了");
}
}
代码:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Scanner;
import javax.swing.JFrame;
public class Text6 extends JFrame implements KeyListener{//继承父类Jframe,获取父类方法
public static void main(String[] args) {
new Text6().init();
}
public void init() {
setSize(500, 500);//创建窗口
setLocationRelativeTo(null);//设置居中
setLayout(null);//取消默认布局
setDefaultCloseOperation(3);//设置
this.addKeyListener(this);//本身就是Listener的实现类
setVisible(true);//可视化
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("键盘按下了");
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("键盘被松开了");
}
}