点击按钮控制背景颜色变化
package chen.com;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//这个类NotePad就是监听者
public class Notepad extends JFrame implements ActionListener{
Panel mp = null;
JButton jb1 = null,jb2 = null;
Cat cat;
public static void main(String[] args) {
Notepad myclass = new Notepad();
}
public Notepad(){
mp = new Panel();
jb1 = new JButton("Red");
jb2 = new JButton("Black");
cat = new Cat();
//这里事件源就是jb1 和 jb2
//注册监听
jb1.addActionListener(this);
jb1.addActionListener(cat);
//指定Action命令
jb1.setActionCommand("jb1");
jb2.addActionListener(this);
jb2.setActionCommand("jb2");
mp.setBackground(Color.blue); //默认的颜色
this.add(jb1,BorderLayout.NORTH);
this.add(jb2,BorderLayout.SOUTH);
this.add(mp);
this.setSize(300,200);
this.setLocation(200,100);
this.setTitle("change the background");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//下面就是对事件处理的方法
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//判断是哪个按钮被点击
if(e.getActionCommand().equals("jb1")){
mp.setBackground(Color.RED);
}else{
mp.setBackground(Color.BLACK);
}
}
}
class Cat implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("jb1")){
System.out.println("Cat knows you press the Red!");
}else{
System.out.println("Cat knows you press the Black!");
}
}
}
在这里,事件源就是两个按钮,当点击按钮的时候将发生ActionEvent事件,当用户点击按钮时将由程序产生一个ActionEvent的对象。这个程序中,猫和这个窗口就是监听者,因为他们在两个按钮上注册了监听。因此当按钮被点击后,监听者就会通过actionPerformed函数来处理。
让小球受到键盘的控制,上下左右移动
小球会随着键盘的控制来上下左右移动,但是不会越界
package chen.com;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
//这个类NotePad就是监听者
public class Notepad extends JFrame{
MyPanel mp = null;
public static void main(String[] args) {
Notepad myclass = new Notepad();
}
public Notepad(){
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
// this.setFocusable(true); //这句话很奇怪啊,之前如果加了别的监听就必须得加,以后再解决!!
this.setSize(300,200);
this.setLocation(200,100);
this.setTitle("change the background");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener{
int x = 10,y = 100;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.fillOval(x, y, 10, 10);
}
//键的某个值被输出
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
//键被按下
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_DOWN){
y+=5;
if (y >165) y = 168;
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
x+=5;
if (x >290) x = 290;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
x-=5;
if(x<0)x = 0;
}else if(e.getKeyCode() == KeyEvent.VK_UP){
y-=5;
if(y<0)y = 0;
}
//调用repaint函数来重绘界面
this.repaint();
}
//键被松开
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}