ActionListener KeyListener


package cn.csdn.service;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

class JFrameA extends JFrame implements ActionListener {
	private JButton jb1, jb2, jb3, jb4, jb5;

	public JFrameA() {
		jb1 = new JButton("north");
		jb1.setActionCommand("north");

		jb2 = new JButton("south");
		jb2.setActionCommand("south");
		jb3 = new JButton("west");
		jb3.setActionCommand("west");
		jb4 = new JButton("east");
		jb4.setActionCommand("east");
		jb5 = new JButton("center");
		jb5.setActionCommand("center");

		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		jb4.addActionListener(this);
		jb5.addActionListener(this);

		this.add(jb1, BorderLayout.NORTH);
		this.add(jb2, BorderLayout.SOUTH);
		this.add(jb3, BorderLayout.WEST);
		this.add(jb4, BorderLayout.EAST);
		this.add(jb5, BorderLayout.CENTER);

		this.setTitle("BorderLayout");
		this.setVisible(true);
		this.setSize(400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("north")) {
			System.out.println("north pressed");
		} else if (e.getActionCommand().equals("south")) {
			System.out.println("south pressed");
		} else if (e.getActionCommand().equals("west")) {
			System.out.println("west pressed");
		} else if (e.getActionCommand().equals("east")) {
			System.out.println("east pressed");
		} else if (e.getActionCommand().equals("center")) {
			System.out.println("center pressed");
		} else {
			System.out.println("nothing");
		}
	}
}

public class JFrameDemo {

	public static void main(String[] str) {
		JFrameA fb = new JFrameA();
	}
}




package cn.demo;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Box {
	int x;
	int y;
	int direction;
	int width;

	public Box(int x, int y, int d, int w) {
		this.x = x;
		this.y = y;
		this.direction = d;
		this.width = w;
	}
}

class XPanle extends JPanel implements KeyListener, Runnable {

	Box box = null;

	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(box.x, box.y, box.width, box.width);
	}

	public XPanle(int boxWidth) {
		box = new Box(10, 10, 1, 10);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			if (box.direction == 0) {
				box.y -= box.width;
			} else if (box.direction == 1) {
				box.y += box.width;
			} else if (box.direction == 2) {
				box.x -= box.width;
			} else {
				box.x += box.width;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("x:" + box.x + ",y:" + box.y);
			this.repaint();
			if (box.x > 300 || box.x < 0 || box.y > 400 || box.y < 0) {
				break;
			}
		}
	}

	@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_W) {
			box.direction = 0;
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			box.direction = 1;
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			box.direction = 2;
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			box.direction = 3;
		}
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}

class XFrame extends JFrame {
	XPanle panel = null;

	public XFrame(int width, int height, int boxWidth) {
		panel = new XPanle(boxWidth);
		Thread t = new Thread(panel);
		t.start();
		
		this.addKeyListener(panel);

		this.add(panel);
		this.setSize(width, height);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}

public class TestKeyListner {
	public static final int BOX_NUM = 15;
	public static final int GAME_WIDTH = 300;
	public static final int GAME_HEIGHT = 400;

	public static void main(String[] str) {
		int boxWidth = GAME_WIDTH / BOX_NUM;
		XFrame frame = new XFrame(GAME_WIDTH, GAME_HEIGHT, boxWidth);
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值