ActionListener KeyListener

来源:互联网 发布:手机电子书阅读器软件 编辑:程序博客网 时间:2024/05/22 10:49


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);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (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);}@Overridepublic void run() {// TODO Auto-generated method stubwhile (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 blocke.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;}}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubif (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();}@Overridepublic 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