java左右移動的代碼 java左右移動的代碼是什么

請教Eclipse有快捷鍵把一段代碼向左右移動

1)比如你有三個類,并打開了這三個類,名字暫且就叫A.java,B.java,C.java。這時你想快速在這三個文件間切換。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,龍港企業(yè)網(wǎng)站建設,龍港品牌網(wǎng)站建設,網(wǎng)站定制,龍港網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,龍港網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

按ctrl+E,然后如果想到C.java,就再打個c,再回車一下就行了,其它同理。

2)自動補全。我自己的習慣是設置成Alt+/。在window(窗口)——preferences(首選項)——keys中搜索下Content Assist就知道它默認是什么了。

3)自動導入包。ctrl+shift+o。然后選擇正確的包,雖然用2的方法也能導入,但是2的方法導的包可能不是你所想要的。

4)如果有紅線,即ecliepse提示你有錯誤,按ctrl+1。比如上面的未導包,就會提示錯誤,這時也可以通過這個方法導包。

java圖形界面中鍵盤控制物體移動

去掉TextField后的程序,這個程序是要用到repaint()的,具體請參考程序中的注釋位置:

import java.awt.*;

import java.awt.event.*;

class mCar extends Frame{

Color redColor;

int xl=80,yl=80,speed=10,step=5;/*********注意這里***********/

public mCar(){

addKeyListener(new KeyAdapter(){ /*********注意這里***********/

public void keyPressed(KeyEvent e){

if(e.getKeyCode()== KeyEvent.VK_UP){

System.out.println("\n Go Up");

yl-=speed;/*********注意這里***********/

}

else if(e.getKeyCode()== KeyEvent.VK_DOWN){

System.out.println("\n Go Down");

yl+=speed;/*********注意這里***********/

}

else if(e.getKeyCode()== KeyEvent.VK_LEFT){

System.out.println("\n Go Left");

xl-=speed;/*********注意這里***********/

}

else if(e.getKeyCode()== KeyEvent.VK_RIGHT){

System.out.println("\n Go Right");

xl+=speed;/*********注意這里***********/

}

else if(e.getKeyCode()== KeyEvent.VK_F1){

speed+=step;/*********注意這里***********/

System.out.println("\n Speed Up");

}

else if(e.getKeyCode()== KeyEvent.VK_F2){

System.out.println("\n Speed Down");

speed-=step;/*********注意這里***********/

}

else

System.out.println(e.getKeyChar());

repaint();/*********注意這里***********/

}

}

);

setSize(400,300);

setVisible(true);

setLocation(400,200);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dispose();

System.exit(0);

}

}

);

}

public void paint(Graphics g){

g.setColor(Color.GREEN);

g.fillRect(xl, yl, 40, 40);/*********注意這里***********/

}

}

public class miniCar {

public static void main(String[] args){

new mCar();

}

}

java,誰能寫個代碼,能監(jiān)聽上下左右鍵的!我想實現(xiàn)游戲角色的左右移動!在線等!不要復制百度上的,

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

public?class?CelMove?extends?JFrame?{

private?static?final?long?serialVersionUID?=?3171295325956127838L;

CelJPanel?cjp;

static?int?width?=?500,?height?=?380;

public?CelMove()?{

//?設置方塊的初始位置

cjp?=?new?CelJPanel(width?/?2,?height?/?2);

//?設置方塊的背景顏色

cjp.setBackground(Color.YELLOW);

//?設置繪制方塊的面板的大小

cjp.setSize(width,?height);

//?添加鼠標事件?讓方塊跟著鼠標移動

this.addMouseMotionListener(new?MouseMotionListener()?{

@Override

public?void?mouseMoved(MouseEvent?e)?{

Point?p?=?e.getPoint();//得到鼠標點擊的位置

cjp.lx?=?p.x;//設置當方塊x坐標=點擊的x作弊

if?(cjp.lx??width?-?28)?{//?28是空出來的一個左右邊框大小.為了不讓方塊移動出了界面

cjp.lx?=?width?-?28;//?如果超過邊界.就設置方塊的x?,回到邊框內(nèi)

}

if?(cjp.lx??0)?{

cjp.lx?=?0;

}

cjp.ly?=?p.y;

if?(cjp.ly??height?-?50)?{//?50是空出來的一個上下邊框大小.為了不讓方塊移動出了界面

cjp.ly?=?height?-?50;

}

if?(cjp.ly??0)?{

cjp.ly?=?0;

}

//?lx,ly坐標設置完成,才執(zhí)行repaint()重繪

cjp.repaint();

}

//?當拖動鼠標的時候..

@Override

public?void?mouseDragged(MouseEvent?e)?{

}

});

//?添加一個鍵盤事件

this.addKeyListener(new?KeyAdapter()?{

@Override

public?void?keyPressed(KeyEvent?e)?{

int?speed?=?10;

//?S?和?下箭頭?可以向下移動

if?(e.getKeyCode()?==?KeyEvent.VK_S?||?e.getKeyCode()?==?KeyEvent.VK_DOWN)?{

//?這里沒有寫是否出界的代碼.你可以先判斷移動后是否會超過邊框

cjp.ly?=?cjp.ly?+?speed;

cjp.repaint();

}

//?W?和?上箭頭?可以向上移動

if?(e.getKeyCode()?==?KeyEvent.VK_W?||?e.getKeyCode()?==?KeyEvent.VK_UP)?{

cjp.ly?=?cjp.ly?-?speed;

cjp.repaint();

}

//?A?和?左箭頭?可以向左移動

if?(e.getKeyCode()?==?KeyEvent.VK_A?||?e.getKeyCode()?==?KeyEvent.VK_LEFT)?{

cjp.lx?=?cjp.lx?-?speed;

cjp.repaint();

}

//?D?和?右箭頭?可以向右移動

if?(e.getKeyCode()?==?KeyEvent.VK_D?||?e.getKeyCode()?==?KeyEvent.VK_RIGHT)?{

cjp.lx?=?cjp.lx?+?speed;

cjp.repaint();

}

}

});

//?設置主窗口的相關屬性

this.setLayout(null);

this.add(cjp);

this.setTitle("移動方塊");

this.setLocation(150,?100);

this.setSize(width,?height);

this.setResizable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}

public?static?void?main(String[]?args)?{

new?CelMove();

}

//?繪制方塊的類

class?CelJPanel?extends?JPanel?{

int?lx,?ly;

public?CelJPanel(int?lx,?int?ly)?{

super();

this.lx?=?lx;

this.ly?=?ly;

}

@Override

public?void?paint(Graphics?g)?{

super.paint(g);

g.setColor(Color.RED);

g.fillRect(lx,?ly,?20,?20);

}

}

}

你參考下吧,很久前寫的

如何用java編寫一個T形四顆*的上下左右移動

移動圓,改變它的圓心即可,可以通過給圓心設置一個運動軌跡函數(shù)實現(xiàn),實例代碼為;

public class joinDemo1 extends JFrame{ //繼承 private int x=100, y=100, r=100; //初始值 public joinDemo1() { super("小圖形"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800, 600); this.setVisible(true); Thread thread=new Thread(new Graphicss()); thread.start(); } public void paint(Graphics g) { super.paint(g); g.fillOval(x, y, r, r); } public static void main(String[] args) { new joinDemo1(); } class Graphicss implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int j = 0; j = 240; j++) { revalidate(); // System.out.println(j); try { Thread.sleep(1000);// 當前線程休眠0.01秒 } catch (InterruptedException e) { e.printStackTrace(); } y=y+j; repaint(); } }}}

當前標題:java左右移動的代碼 java左右移動的代碼是什么
網(wǎng)頁地址:http://www.muchs.cn/article14/dosecge.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化定制開發(fā)、服務器托管網(wǎng)站策劃、Google、網(wǎng)站設計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化