Monitor and control panel monitor

The above panel monitor, so this.addkeylistener (mb), the following controls are listening, so to control .addactionlistener (object creation interface class)

package graphics;
 Import in java.awt *. ;
 Import in javax.swing *. ;
 Import the java.awt.event *. ;
 public  class block movement of the extends the JFrame 
{ 
    Wdmb5 MB = null ; the JButton AN = null ;
     public  static  void main (String [ ] args) 
    { 
        block moves BB = new new block move (); 
        
    } 
    public block movement () 
    { 
        MB = new new Wdmb5 (); 
        AN = new newJButton ();
         the this .add (mb); // because the mb as a subclass of the existence of 
        the this .addKeyListener (mb); // because create a custom class monitor interface, to be added, the other with 
        the this . the setSize (600,700 );
         the this .setLocation (500,600 );
         the this .setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         the this .setVisible ( to true ); 
        
        
    } 
} 
 class Wdmb5 the extends the JPanel the implements the KeyListener 
{ 
     int X = 50, Y = 60 ;
     public  void Paint ( G Graphics) 
    { 
        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 ; 
        } 
        the this .repaint (); // moving image, emm, can be understood 
    }
     public  void the keyTyped (KeyEvent E) { 
        
    } 
    public  void the keyReleased (KeyEvent E) { 
        
    } 
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package graphics;
 Import in java.awt *. ;
 Import in javax.swing *. ;
 Import the java.awt.event *;. // listening event package 
public  class Event Listener the extends the JFrame the implements the ActionListener 
{ 
    Wdmbb MB = null ; the JButton AN1 = null ; AN2 = the JButton null ; JTZ JT = null ;
     public  static  void main (String [] args) 
    { 
        event listener BB = new new event listener (); 
    } 
    public    event listener ()
    {
        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); 
        } 
        the else  IF (e.getActionCommand () the equals ( "222." )) 
        { 
            System.out.println ( "Blue" ); 
            mb.setBackground (Color.BLUE); 
        } 
    } 
} 
class Wdmbb the extends the JPanel // the implements the KeyListener 
{
     public  void Paint (Graphics G) 
    { 
        Super .paint (G); // a parent class 
    }
 //     public void the keyPressed (KeyEvent E) ///////// / Add thread should do it,
 //    {
 //         IF (e.getKeyCode () == KeyEvent.VK_DOWN)
 //     {
 //         System.out.println (e.getKeyCode ());
 //     
//         System.out.println ( "Input");
 //     }
 //     }
 //     
//     public void the keyReleased (KeyEvent E)
 //     {}
 //     public void the keyTyped (KeyEvent E) {
 //         
//     } 
}
 // * significance of this class is a class may be defined individually to listen to the control, where the ActionEvent aa Note e above to the different listening button to add the class * / 
class JTZ the implements the ActionListener // control listening 
{
     public void the actionPerformed (the ActionEvent AA) 
    { 
        IF (. aa.getActionCommand () the equals ( "111" )) 
        { 
            System.out.println ( "Red-listen" ); 
        } 
        the else  IF . (aa.getActionCommand () the equals ( "222 " )) 
        { 
            System.out.println ( " blue monitored " ); 
        } 
            
    } 
}

 

Guess you like

Origin www.cnblogs.com/JLU-Dragon/p/11933149.html