当前位置: 代码迷 >> Java相关 >> 如何在JFrame中添加jpanel,并在jpanel中绘制一个矩形,使用keylistener矩形 ...
  详细解决方案

如何在JFrame中添加jpanel,并在jpanel中绘制一个矩形,使用keylistener矩形 ...

热度:495   发布时间:2011-11-30 21:39:39.0
如何在JFrame中添加jpanel,并在jpanel中绘制一个矩形,使用keylistener矩形移动?????
package com.lishengli.homework;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class KeyTest extends JFrame
{

    private JFrame jFrame;
   
    private JPanel jPanel;
   
    private Paint jPanelPanel;
   
    private MyPaint panel;
   
    private Graphics g;
   
    private int x1=20,y1=20,x2=40,y2=40;
   
   

    public KeyTest()
    {
        jFrame = new JFrame("welcome to tianyaleke...");
        panel = new MyPaint(x1,y1,x2,y2);
        
        panel.setFocusable(true);
        jFrame.add(panel);
        jFrame.setSize(400,400);

        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        panel.addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                int key = e.getKeyCode();
                System.out.println(e.VK_UP);
                if(key==e.VK_UP)
                {
                    panel.repaint(x1-5,y1,x2-5,y2);
                    
                }
                else if(key==e.VK_DOWN)
                {
                    panel.repaint(x1+5,y1,x2+5,y2);
                }
                else if(key==e.VK_LEFT)
                {
                    panel.repaint(x1,y1-5,x2,y2-5);
                }
                else if(key==e.VK_RIGHT)
                {
                    panel.repaint(x1,y1+5,x2,y2+5);
                }
               
            }
        });
        
    }
   
    public static void main(String[] args)
    {

            EventQueue.invokeLater(new Runnable()
            {        
                public void run()
                {
                    KeyTest kt = new KeyTest();
                    kt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            });

    }

}

class MyPaint extends JPanel
{
    private int x1,y1,x2,y2;
   
    public MyPaint(int x1,int y1,int x2,int y2)
    {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }
    public void paintComponent(Graphics g)
    {
        g.fillRect(x1,y1,x2,y2);
    }
}
搜索更多相关的解决方案: class  移动  private  package  public  

----------------解决方案--------------------------------------------------------
这是我写的实现不了,不知道为什么?求高手指教啊
----------------解决方案--------------------------------------------------------
可以参考下这个程序,改改就能得到你想要的。
package com.hisilicon.xxd;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.List;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class test extends JPanel {
    private static Point2D point;
    static test sk;
    static int x=100;
    static int y=100;
    int z=10;

   
   
   
   
   
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.fillRect(x, y, z, z);
        }
   
    public static void main(String [] args){
        sk=new test();
        JFrame frame =new JFrame();
        Container container=frame.getContentPane();
        point =new Point2D.Double(x,y);
        sk.addKeyListener(new KeyListener(){
            public void keyTyped(KeyEvent e) {
               
            }

            
            public void keyPressed(KeyEvent e) {
                int keycode=e.getKeyCode();
                if(e.VK_UP==keycode) { y=y+5; sk.repaint();}
                if(e.VK_DOWN==keycode) { y=y-5; sk.repaint();}
                if(e.VK_LEFT==keycode) { x=x-5; sk.repaint();}
                if(e.VK_RIGHT==keycode) { x=x+5; sk.repaint();}
            }

            
            public void keyReleased(KeyEvent e) {
               
            }
            });
        sk.setFocusable(true);
        frame.add(sk);
        frame.setSize(200,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
   
----------------解决方案--------------------------------------------------------