Example:
KeyStroke ks = KeyStroke.getKeyStroke("shift C"); textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
"CustomAction");
textfield.getActionMap().put("CustomAction", new AbstractAction() { public void actionPerformed(ActionEvent ae) {
// whatever action
}
});
KeyStroke ks = KeyStroke.getKeyStroke("shift C"); textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
"CustomAction");
textfield.getActionMap().put("CustomAction", new AbstractAction() { public void actionPerformed(ActionEvent ae) {
// whatever action
}
});
解决方案 »
- 请问如果用JSWING得到这样的界面效果?
- 用JDBC如何获得刚刚插入记录的主键值(自动生成)
- JAVA 新手问题 如何在JFrame里画一条直线
- 缓存可以用内存实现,也可以用文件实现,请问这两者有何区别和联系?
- 请大家帮我写一篇论文好吗?
- 送分来了,在eclipse中怎么运行外部文件,怎么用导入导出功能
- java能不能实现内存的copy
- 敬请高手总结一下JDK结构:JDK(j2sdk1.4.2)下的那些文件夹、文件都是干什么的?若题目太大,介绍一些文章吧。谢谢!
- 问一个简单的问题,确认一下
- 大家帮我翻译一下这句话,多谢
- 如何写sql像 select * from itop_public_message where name like '%'%'这样的语句
- 运行第一个程序就搞不定了,大家帮忙看一看
//加入textfield到界面
...
//界面初始化的时候加入以下代码
KeyStroke ks = KeyStroke.getKeyStroke("shift C");//你可以自己定义键盘按键,可以是ctrl c或者其他,查看jdk文档
textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
"CustomAction");
textfield.getActionMap().put("CustomAction", new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
//这里调用你自己的方法
copy();
}
});...
private void copy(){
//执行代码
}
那么在当光标在textfield的时候
你按下相应键盘就会执行copy方法
//加入textfield到界面
...
//界面初始化的时候加入以下代码
KeyStroke ks = KeyStroke.getKeyStroke("shift C");//你可以自己定义键盘按键,可以是ctrl c或者其他,查看jdk文档
textfield.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks,
"CustomAction");
textfield.getActionMap().put("CustomAction", new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
//这里调用你自己的方法
copy();
}
});...
private void copy(){
//执行代码
}
那么在当光标在textfield的时候
你按下相应键盘就会执行copy方法
IntTextField实现监听:
class IntTextDocument extends PlainDocument
{
public void insertString(int offs, String str,
AttributeSet a)
throws BadLocationException
{
if (str == null) return; String oldString = getText(0, getLength());
String newString = oldString.substring(0, offs)
+ str + oldString.substring(offs); if (canBecomeValid(newString))
super.insertString(offs, str, a);
} /**
A helper function that tests whether a string is a valid
integer
@param s a string
@return true if s is a valid integer
*/
public static boolean isValid(String s)
{
try
{
Integer.parseInt(s);
return true;
}
catch(NumberFormatException e)
{
return false;
}
} /**
A helper function that tests whether a string is a
substring of a valid integer
@param s a string
@return true if s can be extended to a valid integer
*/
public static boolean canBecomeValid(String s)
{
return s.equals("") || s.equals("-") || isValid(s);
}
}/**
A text field for editing integer values
*/
class IntTextField extends JTextField
{
/**
Constructs an IntTextField.
@param defval the default value
@param size the field size
*/
public IntTextField(int defval, int size)
{
super("" + defval, size);
setInputVerifier(new IntTextFieldVerifier());
// Document doc = getDocument();
// doc.addUndoableEditListener(new UndoListener());
} protected Document createDefaultModel()
{
return new IntTextDocument();
} /**
Checks if the contents of this field is a valid integer.
@return true of the field contents is valid
*/
public boolean isValid()
{
return IntTextDocument.isValid(getText());
} /**
Gets the numeric value of the field contents.
@param the number that the user typed into the field, or
0 if the field contents is not valid.
*/
public int getValue()
{
try
{
return Integer.parseInt(getText());
}
catch(NumberFormatException e)
{
return 0;
}
}
}/**
A verifier that checks if the contents of a text component
is a valid integer.
*/
class IntTextFieldVerifier extends InputVerifier
{
public boolean verify(JComponent component)
{
String text = ((JTextComponent)component).getText();
return IntTextDocument.isValid(text);
}
}
这样不是很好吗?
当然java2技术核心上的那个方法也很不错
他是用TextField.getDocument();来获取一个Document对象
然后由Document对象来实现对输入的监听
可以这么说你所见到的textfield只是个外表,实际上处理任务的是document对象