我是想在控制台中输入上下左右键,但是这里是移动位置!!! 求大神
------解决方案--------------------
没遇到过。。。
------解决方案--------------------
不知道你是不是要的这样的效果:
请输入一个上:↑
上:↑
请输入一个下:↓
下:↓
请输入一个左:←
左:←
请输入一个右:→
右:→
代码如下:
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个上:");
String up = sc.nextLine();
System.out.println("上:"+up);
System.out.print("请输入一个下:");
String down = sc.nextLine();
System.out.println("下:"+down);
System.out.print("请输入一个左:");
String left = sc.nextLine();
System.out.println("左:"+left);
System.out.print("请输入一个右:");
String right = sc.nextLine();
System.out.println("右:"+right);
------解决方案--------------------
控制台中做不到
------解决方案--------------------
- Java code
public class OtherTest extends JFrame { private static final long serialVersionUID = 1L; public static void main(String[] args) { MyPanel mp = new MyPanel(); OtherTest ot = new OtherTest(); ot.add(mp); ot.addKeyListener(mp); ot.setSize(400, 300); ot.setTitle("Moving XO"); ot.setLocationRelativeTo(null); ot.setVisible(true); }}class MyPanel extends JPanel implements KeyListener { private static final long serialVersionUID = 1L; int x = 10; int y = 10; public void paint(Graphics g) { super.paint(g); g.fillOval(x, y, 10, 10); } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_DOWN) { y++; } else if (e.getKeyCode() == KeyEvent.VK_UP) { y--; } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { x--; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { x++; } this.repaint(); } public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } }