当前位置: 技术问答>java相关
如何限制JTextField的输入位数?谢谢
来源: 互联网 发布时间:2015-03-07
本文导语: 另外帮忙看一看http://www.csdn.net/expert/Topic/431/431261.shtm | 这个虽然不是判断getText().length的程序! 但是所用的事件差不多了! 你可以改改! import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing....
另外帮忙看一看http://www.csdn.net/expert/Topic/431/431261.shtm
|
这个虽然不是判断getText().length的程序!
但是所用的事件差不多了!
你可以改改!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class keytest extends JFrame implements KeyListener
{
private JTextField jt=new JTextField();
public keytest()
{
Container ctp=getContentPane();
ctp.setLayout(null);
ctp.add(jt);
jt.setBounds(20,20,100,30);
setTitle("keytest");
setSize(300,300);
jt.addKeyListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void keyPressed(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
//jt.setText("");
if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
{jt.setText("Shit+->");}
if (keyCode==KeyEvent.VK_ENTER) jt.setText("");
//else {jt.setText(""); }
}
public void keyTyped(KeyEvent evt){}
public void keyReleased(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
jt.setText("");
if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
jt.setText("Shit+->");
//else
}
public static void main (String args[])
{
JFrame test =new keytest();
test.setVisible(true);
}
}
但是所用的事件差不多了!
你可以改改!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class keytest extends JFrame implements KeyListener
{
private JTextField jt=new JTextField();
public keytest()
{
Container ctp=getContentPane();
ctp.setLayout(null);
ctp.add(jt);
jt.setBounds(20,20,100,30);
setTitle("keytest");
setSize(300,300);
jt.addKeyListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void keyPressed(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
//jt.setText("");
if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
{jt.setText("Shit+->");}
if (keyCode==KeyEvent.VK_ENTER) jt.setText("");
//else {jt.setText(""); }
}
public void keyTyped(KeyEvent evt){}
public void keyReleased(KeyEvent evt)
{
int keyCode =evt.getKeyCode();
jt.setText("");
if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
jt.setText("Shit+->");
//else
}
public static void main (String args[])
{
JFrame test =new keytest();
test.setVisible(true);
}
}