关闭

ActionListener KeyListener

标签: ActionListenerKeyListener
82人阅读 评论(0) 收藏 举报
分类:


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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111163次
    • 积分:4110
    • 等级:
    • 排名:第7809名
    • 原创:289篇
    • 转载:159篇
    • 译文:0篇
    • 评论:61条
    最新评论