import java.awt.*; import java.awt.event.*; public class MobileControls extends Frame implements KeyListener, FocusListener { boolean left = false; boolean right = false; boolean up = false; boolean down = false; public MobileControls() { addKeyListener(this); addFocusListener(this); setTitle("王者荣耀移动轮盘"); setSize(200, 200); setVisible(true); } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left = true; break; case KeyEvent.VK_RIGHT: right = true; break; case KeyEvent.VK_UP: up = true; break; case KeyEvent.VK_DOWN: down = true; break; } redraw(); } public void keyReleased(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left = false; break; case KeyEvent.VK_RIGHT: right = false; break; case KeyEvent.VK_UP: up = false; break; case KeyEvent.VK_DOWN: down = false; break; } redraw(); } public void keyTyped(KeyEvent e) { } public void focusGained(FocusEvent e) { redraw(); } public void focusLost(FocusEvent e) { left = false; right = false; up = false; down = false; redraw(); } public void paint(Graphics g) { Dimension d = getSize(); int x = d.width / 2; int y = d.height / 2; int r = 40; g.drawOval(x - r, y - r, r * 2, r * 2); if (left) { g.drawLine(x - r, y, x - r - 10, y); } if (right) { g.drawLine(x + r, y, x + r + 10, y); } if (up) { g.drawLine(x, y - r, x, y - r - 10); } if (down) { g.drawLine(x, y + r, x, y + r + 10); } } public void redraw() { repaint(); } public static void main(String[] args) { MobileControls controls = new MobileControls(); } }我要怎么运行这段代码
时间: 2023-06-09 07:03:41 浏览: 146
你需要将这段代码复制到一个 Java 代码编辑器中,然后保存为一个名为 "MobileControls.java" 的文件。接着,你需要在命令行中进入该文件所在的目录,并使用 Java 编译器编译它,命令为:javac MobileControls.java。编译成功后,你可以直接在命令行中运行它,命令为:java MobileControls。然后你就可以在窗口中使用键盘按键模拟一个移动轮盘了。
相关问题
package test; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; public class Test3{ public static void main (String args[]) { Win win=new Win(); win.setTitle("输入序列号"); win.setBounds(10,10,460,360); } } class Police implements KeyListener,FocusListener{ public void keyPressed(KeyEvent e) { JTextField t=(JTextField)e.getSource(); if (t.getCarePosition()>=6) t.transferFocus(); } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void focusGained(FocusEvent e) { JTextField text=(JTextField)e.getSource(); text.setText(null); } public void focusLost(FocusEvent e){} } class Win extends JFrame { JTextField text[]=new JTextField[3]; Police police; JButton b; Win() { setLayout(new FlowLayout()); police = new Police(); for(int i=0;i<3;i++) { text[i]=new JTextField(7); text[i].addKeyListener(police); //监视键盘事件 text[i].addFocusListener(police); add(text[i]); } b=new JButton("确定"); add(b); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text[0].requestFocusInWindow(); } }哪里有错误
代码的缩进格式不够规范,需要进行调整。此外,代码中的一些注释也需要进行调整,以保证清晰易读。以下是修改后的代码:
```
package test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Test3{
public static void main (String args[]) {
Win win=new Win();
win.setTitle("输入序列号");
win.setBounds(10,10,460,360);
}
}
class Police implements KeyListener,FocusListener{
public void keyPressed(KeyEvent e) {
JTextField t=(JTextField)e.getSource();
if (t.getCarePosition()>=6)
t.transferFocus();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void focusGained(FocusEvent e) {
JTextField text=(JTextField)e.getSource();
text.setText(null);
}
public void focusLost(FocusEvent e){}
}
class Win extends JFrame {
JTextField text[]=new JTextField[3];
Police police;
JButton b;
Win() {
setLayout(new FlowLayout());
police = new Police();
for(int i=0;i<3;i++) {
text[i]=new JTextField(7);
text[i].addKeyListener(police); //监视键盘事件
text[i].addFocusListener(police);
add(text[i]);
}
b=new JButton("确定");
add(b);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text[0].requestFocusInWindow();
}
}
```
(3) 在编程做图形用户界面时,需要进行事件处理,已有语句import java.awt.*,可否将importjava.awt.event.*;省去不写。为什么?
不能将 `import java.awt.event.*;` 省去不写。
虽然 `import java.awt.*;` 已经包含了 `java.awt.event` 包,但是它只是导入了 `java.awt` 包中的类和接口,而没有导入 `java.awt.event` 包中的类和接口。如果要使用 `java.awt.event` 包中的类和接口,仍然需要显式地导入它们。
因此,如果在编程做图形用户界面时需要使用 `java.awt.event` 包中的类和接口,就必须显式地导入它们,例如:
```
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame implements ActionListener {
//...
}
```
阅读全文