画坦克__事件处理机制__2个监听
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了画坦克__事件处理机制__2个监听相关的知识,希望对你有一定的参考价值。
一、代码如下
package www.tainiu.gui; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class ah__JianTing__V1 extends JFrame implements ActionListener { JPanel mp=null; JButton jb1= null; JButton jb2= null; public static void main(String[] args) { // TODO Auto-generated method stub ah__JianTing__V1 jv= new ah__JianTing__V1(); } public ah__JianTing__V1() { // TODO Auto-generated constructor stub mp= new MyPanel_V9(); jb1= new JButton("黑色"); jb2= new JButton("红色"); this.add(jb1, BorderLayout.NORTH); this.add(this.mp); this.add(jb2, BorderLayout.SOUTH); mp.setBackground(Color.blue); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); //注册监听 jb1.addActionListener(this); jb1.setActionCommand("黑色"); jb2.addActionListener(this); jb2.setActionCommand("红色"); jb1.addActionListener(new Cat()); //jb1.setActionCommand("黑色"); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //System.out.println("ok"); if(e.getActionCommand().equals("黑色")) { System.out.println("您好,您点击的是【黑色】按钮"); mp.setBackground(Color.BLACK); } else if(e.getActionCommand().equals("红色")) { System.out.println("您好,您点击的是【红色】按钮"); mp.setBackground(Color.red); } } } class MyPanel_V9 extends JPanel{ @Override public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); } } class Cat implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("黑色")) { System.out.println("猫猫监听,您点击的是【黑色】按钮"); //mp.setBackground(Color.BLACK); } else if(e.getActionCommand().equals("红色")) { System.out.println("猫猫监听,您点击的是【红色】按钮"); //mp.setBackground(Color.red); } } }
package www.tainiu.gui; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.JPanel; public class ah__JT_V2 extends JFrame { JPanel mp= null; public static void main(String[] args) { // TODO Auto-generated method stub ah__JT_V2 jv= new ah__JT_V2(); } public ah__JT_V2() { // TODO Auto-generated constructor stub mp= new MyPanel__V10(); this.add(mp); this.setSize(400, 300); this.setVisible(true); //绑定监听操作 this.addKeyListener((KeyListener) mp); } } class MyPanel__V10 extends JPanel implements KeyListener{ int x= 10; int y= 10; @Override public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); g.fillOval(this.x, this.y, 20, 20); } @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) { this.y ++; }else if(e.getKeyCode() == KeyEvent.VK_UP) { this.y --; }else if(e.getKeyCode() == KeyEvent.VK_LEFT) { this.x --; }else if(e.getKeyCode() == KeyEvent.VK_RIGHT) { this.x ++; } this.repaint(); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }
以上是关于画坦克__事件处理机制__2个监听的主要内容,如果未能解决你的问题,请参考以下文章