代码:
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{@Overridepublic void actionPerformed(ActionEvent e) {//实现方法// TODO Auto-generated method stubSystem.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{@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标被点击了");}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标被按下了");}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标被松开了");}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println("鼠标划入了");}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stubSystem.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 stubSystem.out.println("键盘按下了");
}
@Override
public void keyReleased(KeyEvent e) {// TODO Auto-generated method stubSystem.out.println("键盘被松开了");
}
}