Java学习笔记----事件驱动程序设计

时间:2022-07-03 19:21:22

监听器

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.event.*;

public class ControlCircle extends JFrame{
private JButton jbtEnlarge = new JButton("Enlarge");
private JButton jbtShrink = new JButton("Shrink");
private CirclePanel circle = new CirclePanel();

public ControlCircle()
{
jbtEnlarge.addActionListener(new EnlargeListener());//为按钮安装监听器,监听该按钮的触发事件
jbtShrink.addActionListener(new ShrinkListener());
JPanel panel = new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
this.add(circle,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
}

class EnlargeListener implements ActionListener
/*
* 1.如果某个类只是被某一类使用,最好写成嵌套类.分为内部类和外部类
* 2.每一个触发事件,都有属于它的监听器.
* 如: ActionEvent 的监听器为: ActionListener
*/

{
public void actionPerformed(ActionEvent e)
{
circle.enlarge();//内部类可以直接使用外部类的数据域,包括 private数据域
}
}

class ShrinkListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
circle.shrink();
}
}

public static void main(String[] args)
{
JFrame frame = new ControlCircle();
frame.setSize(300, 300);
frame.setTitle("Control Circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

}

class CirclePanel extends JPanel
{
private int radius = 50;
protected void paintComponent(Graphics g)
{

this.setBackground(Color.GRAY);
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillOval(this.getWidth()/2 - this.radius, this.getHeight()/2 - this.radius, this.radius*2, this.radius*2);

}

void enlarge()
{
this.radius += 5;
this.repaint(); //重新调用一次 paintComponent() 方法
}

void shrink()
{
this.radius -= 5;
this.repaint();
}
}

鼠标事件(鼠标监听器接口适配器)

判断鼠标是否在圆内

*
* 鼠标事件
* 监听器接口适配器
* MouseListener接口中的方法都是抽象的,即使不关注某些事件,还是必须实现所有抽象方法
* MouseAdapter适配器提供了所有监听器中所有方法的默认实现方法,使用适配器代替监听器则可以只关注自己想要实现的方法
*/

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Exercise16_19 extends JFrame{
Exercise16_19()
{
this.add(new Circle1());
}

public static void main(String[] args)
{
JFrame frame = new Exercise16_19();
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setTitle("Exercise16_19");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class Circle1 extends JPanel
{
private int stringY = -100;
private int panelWidth;private int panelHeight;//由于panel的长宽必须在图形布局后才能获得,所以在paint()方法中动态获得panel的长宽
protected void paintComponent(Graphics g)
{
panelWidth = getWidth();panelHeight = getHeight();
super.paintComponent(g);//每次绘画之前,清楚前一次绘画的结果
g.setColor(Color.BLUE);
g.drawOval(panelWidth/2-50, panelHeight/2-50, 100, 100);
g.setColor(Color.RED);
g.drawString("The mouse is in the circle", panelWidth/2-50, stringY);
}
Circle1()
{

this.addMouseMotionListener(new MouseAdapter() //MouseMotionListener接口处理拖动和移动鼠标事件
{
@Override
public void mouseMoved(MouseEvent e)
{
// TODO Auto-generated method stub
double x = e.getX();
double y = e.getY();
if ((Math.abs((x-panelWidth/2))<50) && (Math.abs(y-panelHeight/2)<50))
{
stringY = panelHeight/2;
repaint();
}
else
{
stringY = -100;
repaint();
}
};
});

this.addMouseListener(new MouseAdapter() //MouseListener接口处理鼠标建按下,释放,点击,输入和退出事件
{
@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
stringY += 10;
repaint();
}
});
}
}

键盘事件(键盘接口接听器接口适配器)

方向键控制字符串的移动,其他键改变字符串的内容

其他

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class KeyEventDemo extends JFrame{

static class KeyboardPanel extends JPanel
{
private int charX = 100;private int charY = 100;
private char keyChar = 'A';
KeyboardPanel()
{
addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())//getKeyCode()返回按键定义在KeyEvent表中的值
{
//KeyEvent类封装了关于案件事件的信息
case KeyEvent.VK_DOWN:charY += 10;break;
case KeyEvent.VK_UP:charY -= 10;break;
case KeyEvent.VK_LEFT:charX -=10;break;
case KeyEvent.VK_RIGHT:charX += 10;break;
default: keyChar = e.getKeyChar();//getKeyChar()返回输入的字符
}
repaint();
}
});
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.CYAN);
g.setFont(new Font("TimesRoman",Font.PLAIN,24));
g.drawString(String.valueOf(keyChar), charX, charY);
}
}


KeyEventDemo()
{
KeyboardPanel keyPanel = new KeyboardPanel();
add(keyPanel);
keyPanel.setFocusable(true);//仅有一个焦点的组件才能够接收KeyEvent.所以要将组件设为聚焦的
}

public static void main(String[] args)
{
JFrame frame = new KeyEventDemo();
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setTitle("KEYEVENTDEMO");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Timer类

触边反弹的小球

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class BounceBall extends JFrame
{

static class Ball extends JPanel
{

private int circleX = 100;private int circleY = 100;private int circleRadius = 80;
private int moveX = 1;private int moveY = 1;
Ball()
{
Timer timer = new Timer(1,new TimerListener());//没1毫秒触发一次接听器
timer.start();
this.setBackground(Color.BLACK);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillOval(circleX, circleY, circleRadius*2, circleRadius*2);
circleX += moveX;circleY += moveY;
if((circleX <= 0)||(circleX >= (getWidth()-circleRadius*2)))//触边反弹
moveX = -moveX;
if((circleY <= 0)||(circleY >= (getHeight()-circleRadius*2)))
moveY = -moveY;
}

class TimerListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
repaint();
}
}
}

BounceBall()
{
add(new Ball());

}

public static void main(String[] args)
{
JFrame frame = new BounceBall();
frame.setTitle("BounceBall");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

实例:按紧q键在画布上画圆

public class DrawCircle extends JFrame{
public DrawCircle()
{
add(new circlePanel());
}

public static void main(String[] args)
{
JFrame frame = new DrawCircle();
frame.setTitle("DrawCircle");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class circlePanel extends JPanel
{
private int circleX=-10000000;//圆的初始位置在画布以外
private int circleY=-10000000;
private int circleWidth;
private int circleHeight;
private int startX;
private int startY;
private boolean canPaint;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillOval(circleX, circleY, circleWidth, circleHeight);
}

circlePanel()
{
this.setFocusable(true);//仅有一个焦点的组件可以接受KeyEvent
this.setBackground(Color.BLACK);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_Q)
{
canPaint = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_Q)
{
canPaint = false;
}
}
});

addMouseMotionListener(new MouseMotionAdapter()
{
@Override
public void mouseDragged(MouseEvent e)
{
if(circleX == -10000000)
{
circleX = e.getX();
circleY = e.getY();
startX = circleX;
startY = circleY;
}
circleWidth = e.getX() - startX;
circleHeight = e.getY() - startY;
if (circleWidth<0) //如果圆往左或往上画时,圆的起点座标也需要跟着像左或右移动
{
circleWidth = -circleWidth;
circleX = startX - circleWidth;
}
if(circleHeight<0)
{
circleHeight = -circleHeight;
circleY = startY - circleHeight;
}
if (canPaint)
repaint();
}
});

addMouseListener(new MouseAdapter()
{
@Override
public void mouseReleased(MouseEvent e) {
circleX = -10000000;
circleY = -10000000;
repaint();
}

});
}
}
}