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(); } }哪里有错误
时间: 2024-01-15 19:05:11 浏览: 97
JAVA 程序 源代码 import java.applet.*;import java.awt.*
代码的缩进格式不够规范,需要进行调整。此外,代码中的一些注释也需要进行调整,以保证清晰易读。以下是修改后的代码:
```
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();
}
}
```
阅读全文