监听器
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
{
public void actionPerformed(ActionEvent e)
{
circle.enlarge();
}
}
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();
}
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;
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()
{
@Override
public void mouseMoved(MouseEvent e)
{
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()
{
@Override
public void mousePressed(MouseEvent e)
{
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())
{
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();
}
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);
}
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());
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);
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();
}
});
}
}
}