上面的面板监听,所以要this.addkeylistener(mb),下面的是控件监听,所以要控件.addactionlistener(创建接口的类的对象)
package 绘图; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class 方块移动 extends JFrame { Wdmb5 mb=null;JButton an=null; public static void main(String[] args) { 方块移动 bb=new 方块移动(); } public 方块移动() { mb=new Wdmb5(); an=new JButton(); this.add(mb);//因为此时mb是作为一个子类存在的 this.addKeyListener(mb);//因为自定义类创建监听接口,所以要被添加,其他同 this.setSize(600,700); this.setLocation(500,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class Wdmb5 extends JPanel implements KeyListener { int x=50,y=60; public void paint(Graphics g) { super.paint(g); g.fillRect(x, y, 50, 50); } public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_DOWN) { y+=5; } else if (e.getKeyCode()==KeyEvent.VK_UP) { y-=5; } else if (e.getKeyCode()==KeyEvent.VK_LEFT) { x-=5; } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { x+=5; } this.repaint();//移动图象,emm,可以这么理解 } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package 绘图; import java.awt.*; import javax.swing.*; import java.awt.event.*;//监听事件包 public class 事件监听 extends JFrame implements ActionListener { Wdmbb mb=null; JButton an1=null; JButton an2=null; Jtz jt=null; public static void main(String[] args) { 事件监听 bb=new 事件监听(); } public 事件监听() { jt=new Jtz(); mb=new Wdmbb(); an1=new JButton("红色"); an2=new JButton("蓝色"); this.add(mb); // this.addKeyListener(mb); an1.addActionListener(this); an1.addActionListener(jt); an1.setActionCommand("111");//区别按钮 an2.addActionListener(this); an2.addActionListener(jt); an2.setActionCommand("222"); this.add(an1,BorderLayout.NORTH); this.add(an2,BorderLayout.SOUTH); mb.setBackground(Color.yellow); this.setSize(600,600); this.setLocation(500,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e)//接口中的抽象方法 { if(e.getActionCommand().equals("111")) { System.out.println("red"); mb.setBackground(Color.red); } else if (e.getActionCommand().equals("222")) { System.out.println("blue"); mb.setBackground(Color.blue); } } } class Wdmbb extends JPanel // implements KeyListener { public void paint(Graphics g) { super.paint(g);//父类的一个方法 } // public void keyPressed(KeyEvent e)//////////添加线程应该能行, // { // if (e.getKeyCode()==KeyEvent.VK_DOWN) // { // System.out.println(e.getKeyCode()); // // System.out.println("键盘输入"); // } // } // // public void keyReleased(KeyEvent e) // {} // public void keyTyped(KeyEvent e) { // // } } //*这个类的意义就是可单独定义一类,从而对控件进行监听,这里的ActionEvent 注意aa要与上面的e不同,按钮监听要添加这个类*/ class Jtz implements ActionListener//控件监听 { public void actionPerformed(ActionEvent aa) { if(aa.getActionCommand().equals("111")) { System.out.println("红色被监听"); } else if (aa.getActionCommand().equals("222")) { System.out.println("蓝色被监听"); } } }