class Win extends Frame implements KeyListener,FocusListener
{
TextField text[]=new TextField[3];
Win()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
for(int i=0;i<3;i++)
{
text[i]=new TextField(7);
text[i].addKeyListener(this);
text[i].addFocusListener(this);
add(text[i]);
}
text[0].requestFocusInWindow();
setBounds(10,10,300,300);
setVisible(true);
validate();
}
public void keyPressed(KeyEvent e)
{
TextField text=(TextField)e.getSource();
if(text.getCaretPosition()>=6)
{
text.transferFocus();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void focusGained(FocusEvent e)
{
// TextField text=(TextField)e.getSource();
// text.setText(null);
}
public void focusLost(FocusEvent e){}
}
为什么我的代码实现不了满6个字节自动跳到下个框,数字能够连续不断的输入呢?
求高手指教。
{
TextField text[]=new TextField[3];
Win()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
for(int i=0;i<3;i++)
{
text[i]=new TextField(7);
text[i].addKeyListener(this);
text[i].addFocusListener(this);
add(text[i]);
}
text[0].requestFocusInWindow();
setBounds(10,10,300,300);
setVisible(true);
validate();
}
public void keyPressed(KeyEvent e)
{
TextField text=(TextField)e.getSource();
if(text.getCaretPosition()>=6)
{
text.transferFocus();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void focusGained(FocusEvent e)
{
// TextField text=(TextField)e.getSource();
// text.setText(null);
}
public void focusLost(FocusEvent e){}
}
为什么我的代码实现不了满6个字节自动跳到下个框,数字能够连续不断的输入呢?
求高手指教。
经测试是7个才会跳,因为楼主的跳到下个框是写在了keyPressed方法中,建议写在keyReleased中。
另外,可能是我理解的有误,感觉已经实现了吧!只是第三个框之后又跳回了第一个框,数字也是一样会跳的呀!
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;class Win extends Frame implements KeyListener, FocusListener {
TextField text[] = new TextField[3]; public Win() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLayout(new FlowLayout());
for (int i = 0; i < 3; i++) {
text[i] = new TextField(7);
text[i].addKeyListener(this);
text[i].addFocusListener(this);
add(text[i]);
}
text[0].requestFocusInWindow();
setBounds(10, 10, 300, 300);
setVisible(true);
validate();
} public void keyPressed(KeyEvent e) {
TextField text = (TextField) e.getSource();
if (text.getCaretPosition() > 4) {
text.transferFocus();
}
} public void keyTyped(KeyEvent e) {
} public void keyReleased(KeyEvent e) {
} public void focusGained(FocusEvent e) {
TextField text=(TextField)e.getSource();
text.setText("");
} public void focusLost(FocusEvent e) {
}
public static void main(String[] args) {
Win w = new Win();
}
}