您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
【第18章:图形界面】_事件处理
发布时间:2021-01-17 12:56:45编辑:雪饮阅读()
窗口事件监听
窗口事件监听需要实现java.awt.event.WindowListener接口
import java.awt.event.WindowListener ;
import java.awt.event.WindowEvent ;
import java.awt.Color ;
import javax.swing.JFrame ;
class MyWindowEventHandle implements WindowListener{
public void windowActivated(WindowEvent e){
System.out.println("windowActivated --> 窗口被选中") ;
}
public void windowClosed(WindowEvent e){
System.out.println("windowClosed --> 窗口被关闭") ;
}
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
public void windowDeactivated(WindowEvent e){
System.out.println("windowDeactivated --> 取消窗口选中") ;
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified --> 窗口从最小化恢复") ;
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified --> 窗口最小化") ;
}
public void windowOpened(WindowEvent e){
System.out.println("windowOpened --> 窗口被打开") ;
}
};
public class TestJava{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kasumi") ;
//监听窗口事件
frame.addWindowListener(new MyWindowEventHandle()) ;
frame.setSize(300,150) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
D:\>javac TestJava.java
D:\>java TestJava
windowActivated --> 窗口被选中
windowOpened --> 窗口被打开
windowClosing --> 窗口关闭
java.awt.event.WindowEvent接口对应有一个适配器,则可以通过适配器来快速实现窗口监听
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.awt.Color ;
import javax.swing.JFrame ;
class MyWindowEventHandle extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
};
public class TestJava{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kasumi") ;
frame.addWindowListener(new MyWindowEventHandle()) ;
frame.setSize(300,150) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
D:\>javac TestJava.java
D:\>java TestJava
windowClosing --> 窗口关闭
内部类实现窗口监听
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import javax.swing.JFrame ;
public class TestJava{
public static void main(String args[]){
JFrame frame = new JFrame("Welcome To kasumi") ;
//内部类实现窗口监听
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.out.println("windowClosing --> 窗口关闭") ;
System.exit(1) ;
}
}) ;
frame.setSize(300,150) ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
}
D:\>javac TestJava.java
D:\>java TestJava
windowClosing --> 窗口关闭
按钮的操作事件
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JTextField ;
import java.util.Date;
import java.text.SimpleDateFormat;
class ActionHandle{
private JButton but = new JButton("click me");
private JTextField text = new JTextField() ;
public ActionHandle(){
JFrame frame = new JFrame("Welcome To MLDN") ;
frame.setLayout(new GridLayout(2,1)) ;
//创建输入框
frame.add(text) ;
//创建按钮
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//判断事件来源是否是指定按钮
if(e.getSource()==but){
text.setText("你点击了click me"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) ;
}
}
}) ;
frame.add(but) ;
frame.pack() ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new ActionHandle() ;
}
}
在上面例子中判断事件来源还可以通过instanceof关键字
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JTextField ;
import java.util.Date;
import java.text.SimpleDateFormat;
class ActionHandle{
private JButton but = new JButton("click me");
private JTextField text = new JTextField() ;
public ActionHandle(){
JFrame frame = new JFrame("Welcome To MLDN") ;
frame.setLayout(new GridLayout(2,1)) ;
//创建输入框
frame.add(text) ;
//创建按钮
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//判断事件来源是否是指定按钮
if(e.getSource() instanceof JButton){
text.setText("你点击了click me"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) ;
}
}
}) ;
frame.add(but) ;
frame.pack() ;
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new ActionHandle() ;
}
}
用ActionListener以内部类的形式实现密码输入框中密码的获取、重置
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JPasswordField ;
import javax.swing.JLabel ;
class ActionHandle{
private JButton submit = new JButton("获取密码");
private JButton reset = new JButton("重置");
private JPasswordField passText = new JPasswordField() ;
private JLabel nameLab = new JLabel() ;
public ActionHandle(){
JFrame frame = new JFrame("Welcome To kasumi") ;
frame.setLayout(new GridLayout(5,1)) ;
//输入密码提示标签
JLabel infoLab = new JLabel("请输入密码") ;
frame.add(infoLab);
//密码输入框
frame.add(passText);
//获取密码
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
nameLab.setText("获取的密码是:"+new String(passText.getPassword()));
}
}
}) ;
frame.add(submit);
//重置密码
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
passText.setText("") ;
nameLab.setText("密码被重置");
}
}
}) ;
frame.add(reset);
//信息输出区
frame.add(nameLab);
frame.pack() ;
frame.setVisible(true) ;
}
}
public class TestJava{
public static void main(String args[]){
new ActionHandle();
}
}
键盘监听事件
实现java.awt.event.KeyListener接口可以对键盘事件进行监听
import java.awt.event.KeyListener ;
import java.awt.event.KeyEvent ;
import javax.swing.JFrame ;
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JTextArea ;
class MyKeyHandle extends JFrame implements KeyListener{
private JTextArea text = new JTextArea() ;
public MyKeyHandle(){
super.setTitle("Welcome To MLDN") ;
text.setBounds(5,5,300,200) ;
super.add(text) ;
//键盘监听事件
text.addKeyListener(this) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
public void keyPressed(KeyEvent e){
text.append("键盘“" + KeyEvent.getKeyText(e.getKeyCode())+ "”键按下\n") ;
}
public void keyReleased(KeyEvent e){
text.append("键盘“" + KeyEvent.getKeyText(e.getKeyCode())+ "”键松开\n") ;
}
public void keyTyped(KeyEvent e){
text.append("输入的内容是:" + e.getKeyChar() + "\n") ;
}
};
public class TestJava{
public static void main(String args[]){
new MyKeyHandle();
}
}
用适配器(匿名内部类)实现键盘监听
import java.awt.event.KeyEvent ;
import javax.swing.JFrame ;
import java.awt.event.ActionListener ;
import java.awt.event.ActionEvent ;
import java.awt.GridLayout ;
import javax.swing.JFrame ;
import javax.swing.JTextArea ;
import java.awt.event.KeyAdapter ;
class MyKeyHandle extends JFrame{
private JTextArea text = new JTextArea() ;
public MyKeyHandle(){
super.setTitle("Welcome To MLDN") ;
text.setBounds(5,5,300,200) ;
super.add(text) ;
//键盘监听事件
text.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
text.append("输入的内容是:" + e.getKeyChar() + "\n") ;
}
}) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new MyKeyHandle();
}
}
鼠标事件的监听
实现java.awt.event.MouseListener接口可以对鼠标事件进行监听
import java.awt.event.MouseListener ;
import java.awt.event.MouseEvent ;
import javax.swing.JFrame ;
import javax.swing.JTextArea ;
import javax.swing.JScrollPane ;
class MyMouseHandle extends JFrame implements MouseListener{
private JTextArea text = new JTextArea() ;
public MyMouseHandle(){
super.setTitle("Welcome To MLDN") ;
JScrollPane scr = new JScrollPane(text) ;
scr.setBounds(5,5,300,200) ;
super.add(scr) ;
//鼠标事件监听
text.addMouseListener(this) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
public void mouseClicked(MouseEvent e){
int c = e.getButton() ;
String mouseInfo = null ;
if(c==MouseEvent.BUTTON1){
mouseInfo = "左键" ;
}
if(c==MouseEvent.BUTTON3){
mouseInfo = "右键" ;
}
if(c==MouseEvent.BUTTON2){
mouseInfo = "滚轴" ;
}
text.append("鼠标单击:" + mouseInfo + "\n") ;
}
public void mouseEntered(MouseEvent e){
text.append("鼠标进入组件。\n") ;
}
public void mouseExited(MouseEvent e){
text.append("鼠标离开组件。\n") ;
}
public void mousePressed(MouseEvent e){
text.append("鼠标按下。\n") ;
}
public void mouseReleased(MouseEvent e){
text.append("鼠标松开。\n") ;
}
};
public class TestJava{
public static void main(String args[]){
new MyMouseHandle();
}
}
适配器模式监听鼠标左右键及滚轮
import java.awt.event.MouseAdapter ;
import java.awt.event.MouseEvent ;
import javax.swing.JFrame ;
import javax.swing.JFrame ;
import javax.swing.JTextArea ;
import javax.swing.JScrollPane ;
class MyMouseHandle extends JFrame{
private JTextArea text = new JTextArea() ;
public MyMouseHandle(){
super.setTitle("Welcome To kasumi") ;
JScrollPane scr = new JScrollPane(text) ;
scr.setBounds(5,5,300,200) ;
super.add(scr) ;
text.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
int c = e.getButton() ;
String mouseInfo = null ;
if(c==MouseEvent.BUTTON1){
mouseInfo = "左键" ;
}
if(c==MouseEvent.BUTTON3){
mouseInfo = "右键" ;
}
if(c==MouseEvent.BUTTON2){
mouseInfo = "滚轴" ;
}
text.append("鼠标单击:" + mouseInfo + "\n") ;
}
}) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new MyMouseHandle() ;
}
}
鼠标拖拽事件监听
内部类实现java.awt.event.MouseMotionListener则可以对鼠标拖拽事件进行监听
import java.awt.event.MouseMotionListener ;
import java.awt.event.MouseEvent ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
class MyMouseMotionHandle extends JFrame{
private JLabel lab=new JLabel();
public MyMouseMotionHandle(){
super.setTitle("Welcome To kasumi") ;
super.add(lab);
super.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e){
String str="鼠标拖拽到:X = " + e.getX() + ",Y = " + e.getY();
lab.setText(str);
}
public void mouseMoved(MouseEvent e){
String str="鼠标移动到窗体。";
lab.setText(str);
}
}) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new MyMouseMotionHandle() ;
}
}
同样,这家伙也支持适配器操作
import java.awt.event.MouseMotionListener ;
import java.awt.event.MouseEvent ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import java.awt.event.MouseMotionAdapter ;
class MyMouseMotionHandle extends JFrame{
private JLabel lab=new JLabel();
public MyMouseMotionHandle(){
super.setTitle("Welcome To kasumi") ;
super.add(lab);
super.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
String str="鼠标拖拽到:X = " + e.getX() + ",Y = " + e.getY();
lab.setText(str);
}
}) ;
super.setSize(310,210) ;
super.setVisible(true) ;
}
};
public class TestJava{
public static void main(String args[]){
new MyMouseMotionHandle() ;
}
}
关键字词:java,事件