我试图在JApplet程序中使用多个键盘侦听器函数,想知道这是否可行。我有一个只允许输入数字,而另一个允许一些额外的字符,如加减去乘除等。我想根据用户所在的标签选择一个特定的一个。试过但是第一个被选中的是即使我打电话给第二个...... 这些是我的两个键盘功能
private void keyboardSetting1()//disable keyboard1
`{//this deactivates the keyboard except for certain characters
Display.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent evt){
char c = evt.getKeyChar();
java.awt.event.ActionEvent e = null;//just to be able to call the button function
if (c=='*') {Multiply_BtnActionPerformed(e);evt.consume();} //override * to x
else if (c=='/') {Divide_BtnActionPerformed(e);evt.consume();}//override / to ÷
else if (c=='+'){Plus_BtnActionPerformed(e);evt.consume();}
else if(c=='-'){Minus_BtnActionPerformed(e);evt.consume();}//override +and - buttons also
else if((c>='0')&&(c<='9'));//allow digits
else if(c=='('){Opn_BracketActionPerformed(e);evt.consume();}
else if(c == ')'){Cls_BracketActionPerformed(e);evt.consume();}//override open and close brackets
else if (c=='^'){PowerBtnActionPerformed(e);evt.consume();}//override power button
else evt.consume();//destroy or else
}
});
private void keyboardSetting2()//disable keyboard1
{//this deactivates the keyboard except for certain numbers
Display.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent evt){
char c = evt.getKeyChar();
java.awt.event.ActionEvent e = null;//just to be able to call the button function
if((c>='0')&&(c<='9'));//allow digits
else evt.consume();//destroy or else
}
});
在激活它们时,我尝试使用此代码来选择:
private void Parent_TabStateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
//this function checks when a the tabbed pane has a new panel selected
Display.setText(null);//clear the Display
if (Parent_Tab.getSelectedComponent()==StandardTab) //for standard panel
{
Btn_DecPnt.setEnabled(true);//enable dec point
keyboardSetting1();//reset the keyboard for this tab specially
}
else if(Parent_Tab.getSelectedComponent()==Base_Converter)
{
keyboardSetting2();
Btn_DecPnt.setEnabled(false);//disable decimal point as not to be used
}
所以我的第一个标签是标准标签,因此它会激活setting1。当我在选择基本转换选项卡时调用设置2时,它不起作用但设置仍然保持..是否不可能使用多个键盘监听器?感谢