| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3413 人关注过本帖
标题:如何在JFrame中添加jpanel,并在jpanel中绘制一个矩形,使用keylistener矩形 ...
只看楼主 加入收藏
李胜利
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-3-24
结帖率:50%
收藏
 问题点数:0 回复次数:2 
如何在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 
2011-11-30 21:39
李胜利
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-3-24
收藏
得分:0 
这是我写的实现不了,不知道为什么?求高手指教啊
2011-11-30 21:40
安若123
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-9-27
收藏
得分:0 
可以参考下这个程序,改改就能得到你想要的。
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);
    }
}
   
2012-09-27 23:43
快速回复:如何在JFrame中添加jpanel,并在jpanel中绘制一个矩形,使用keylisten ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061980 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved