当前位置: 代码迷 >> Java相关 >> keyListener 的问题
  详细解决方案

keyListener 的问题

热度:526   发布时间:2006-05-17 16:40:00.0
keyListener 的问题

大家看看程序 为什么把注释去掉 把label该成textfield 键盘的输入就没什么用拉呀?
想写个有键盘监听的计算器 遇到点问题 把代码提出来 大家帮忙看看


import java.awt.*;
import java.awt.event.*;
public class aa extends Frame implements ActionListener
{private KeyEventHandler keyListener= new KeyEventHandler();
//private TextField text=new TextField();
private Label text=new Label();
private Button bu=new Button();

public aa()
{this.addKeyListener(keyListener);
this.setLayout(new GridLayout(2,2));
add(text);
// add(bu);
bu.addActionListener(this);
// text.setEditable(false);
setSize(200,200);
this.setVisible(true);
}


public void actionPerformed(ActionEvent e)

{
if(e.getSource()==bu)
text.setText("click the Button");


}




public static void main(String args[])
{aa p=new aa();}






public class KeyEventHandler implements KeyListener

{public void keyPressed(KeyEvent Ke)
{
if(Ke.getKeyChar()=='q')
text.setText("aaaa");

}
public void keyReleased(KeyEvent Ke)
{}
public void keyTyped(KeyEvent Ke)
{System.out.println("aa"+Ke.getKeyChar());}


}
}

搜索更多相关的解决方案: keyListener  计算器  private  awt  new  

----------------解决方案--------------------------------------------------------
把label该成textfield 键盘的输入就没什么用拉呀?

----------------解决方案--------------------------------------------------------
这句话是什么意思?

----------------解决方案--------------------------------------------------------
// text.setEditable(false);
我把他设置拉呀
你的效率挺高的说实话
不过我的问题解决拉
自己的程序写的太垃圾拉 和你比起来差远拉
我把自己的代码贴上来你看看吧
还有个问题就是小数点
如果地一个输入的是“.”回出错当你在输入数字后在输入“.”输不进去着是我希望的 不过你在输入几个数字后在输入“。”就有错拉


----------------解决方案--------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.KeyStroke;
public class ff extends JFrame implements ActionListener

{

Panel bu=new Panel();
Panel bu1=new Panel();
private KeyEventHandler keyListener= new KeyEventHandler();
private JButton one=new JButton("1");
private JButton two=new JButton("2");
private JButton three=new JButton("3");
private JButton four=new JButton("4");
private JButton five=new JButton("5");
private JButton six=new JButton("6");
private JButton seven=new JButton("7");
private JButton night=new JButton("8");
private JButton nine=new JButton("9");
private JButton zero=new JButton("0");
private JButton dian=new JButton(".");
private JButton a=new JButton("+");
private JButton b=new JButton("-");
private JButton c=new JButton("*");
private JButton d=new JButton("/");
private JButton de=new JButton("=");
private JButton t=new JButton("退格");
private JButton x=new JButton("exit");
private JButton cl=new JButton("clear");
private JButton jd=new JButton("+/-");
private JTextField text=new JTextField();

JPanel p1=new JPanel(new BorderLayout());
JPanel p2=new JPanel(new GridLayout(4,4));
JPanel p3=new JPanel(new FlowLayout());

public double tmp=0;
public double opt1,opt2;
public int flag;
public ff()
{
super("计算器");

text.setEditable(false);
text.addKeyListener(keyListener);
text.setHorizontalAlignment(JTextField.RIGHT);
Container p=getContentPane();
p.setLayout(new BorderLayout());
JPanel p1=new JPanel(new BorderLayout());
JPanel p2=new JPanel(new GridLayout(4,4));
JPanel p3=new JPanel(new FlowLayout());
p.add(text,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p1.add(de,BorderLayout.EAST);
p1.add(p3,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);
p3.add(t);
p3.add(x);
p3.add(cl);
p2.add(nine);
p2.add(night);
p2.add(seven);
p2.add(b);
p2.add(four);
p2.add(five);
p2.add(six);
p2.add(a);
p2.add(one);
p2.add(two);
p2.add(three);
p2.add(d);
p2.add(zero);
p2.add(dian);
p2.add(jd);
p2.add(c);



addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});

pack();
show();


one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
night.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
x.addActionListener(this);
cl.addActionListener(this);
de.addActionListener(this);
dian.addActionListener(this);
t.addActionListener(this);
jd.addActionListener(this);

}

public void this_keyPressed(KeyEvent e)
{
int kc = e.getKeyCode();
if (kc==KeyEvent.VK_A)
text.setText(text.getText()+"2");

}

public void actionPerformed(ActionEvent e)

{
if(e.getSource()==x)
System.exit(0);
else if(e.getSource()==one)
text.setText(text.getText()+"1");
else if(e.getSource()==two)
text.setText(text.getText()+"2");
else if(e.getSource()==three)
text.setText(text.getText()+"3");
else if(e.getSource()==four)
text.setText(text.getText()+"4");
else if(e.getSource()==five)
text.setText(text.getText()+"5");
else if(e.getSource()==six)
text.setText(text.getText()+"6");
else if(e.getSource()==seven)
text.setText(text.getText()+"7");
else if(e.getSource()==night)
text.setText(text.getText()+"8");
else if(e.getSource()==nine){
text.setText(text.getText()+"9");
}



else if(e.getSource()==t)
{ if(text.getText().equals(""))return;
else{
StringBuffer sb=new StringBuffer(text.getText());
sb.deleteCharAt(sb.length()-1);
String s9=new String(sb);
text.setText(s9);}
}



else if(e.getSource()==dian)
{ int gm;
gm=text.getText().length();

StringBuffer aaa=new StringBuffer(text.getText().substring(gm-1));
String aa=new String(aaa);
String bb=".";

if(aa.equals(bb)){return;}
else {

text.setText(text.getText()+".");
}

}

else if(e.getSource()==jd)
{ if(text.getText().length()>0){
double aa,bb;
aa=Double.valueOf(text.getText()).doubleValue();
bb=-(aa);
text.setText(String.valueOf(bb));
}
}

else if(e.getSource()==zero)
{StringBuffer aaa=new StringBuffer(text.getText());
String aa=new String(aaa);
String bb="0";
if(aa.equals(bb)&&aa.length()==1){}
else
text.setText(text.getText()+"0");
}

else if(e.getSource()==cl)
{ text.setText("");
opt1=0;
opt2=0;
}
else if(e.getSource()==a)
{if(text.getText().length()>0)
{ flag=1;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");}
}
else if(e.getSource()==b)
{ if(text.getText().length()>0)
{ flag=2;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");}
}

else if(e.getSource()==c)
{if(text.getText().length()>0){
flag=3;
opt1=Double.valueOf(text.getText()).doubleValue();

text.setText("");
}}
else if(e.getSource()==d)
{if(text.getText().length()>0){
flag=4;
opt1=Double.valueOf(text.getText()).doubleValue();

text.setText("");}
}
else if(e.getSource()==de)
{if(text.getText().length()>0){
opt2=Double.valueOf(text.getText()).doubleValue();
if(flag==1){
opt1=opt1+opt2;
text.setText(String.valueOf(opt1));
}
if(flag==2){
opt1=opt1-opt2;
text.setText(String.valueOf(opt1));
}
if(flag==3){
opt1=opt1*opt2;
text.setText(String.valueOf(opt1));
}
if(flag==4){


if(opt2==0)
{
text.setText("除数不可以为零");
}
else {opt1=opt1/opt2;
text.setText(String.valueOf(opt1));

}
}

}}


}



public static void main(String args[])
{ff mf =new ff();
mf.setSize(250,205);
mf.setResizable(false);
mf.setVisible(true);
}

public class KeyEventHandler implements KeyListener

{public void keyPressed(KeyEvent Ke)
{
if(Ke.getKeyChar()=='1')
text.setText(text.getText()+"1");
else if(Ke.getKeyChar()=='2')
text.setText(text.getText()+"2");
else if(Ke.getKeyChar()=='3')
text.setText(text.getText()+"3");
else if(Ke.getKeyChar()=='4')
text.setText(text.getText()+"4");
else if(Ke.getKeyChar()=='5')
text.setText(text.getText()+"5");
else if(Ke.getKeyChar()=='6')
text.setText(text.getText()+"6");
else if(Ke.getKeyChar()=='7')
text.setText(text.getText()+"7");
else if(Ke.getKeyChar()=='8')
text.setText(text.getText()+"8");
else if(Ke.getKeyChar()=='9')
text.setText(text.getText()+"9");
else if(Ke.getKeyChar()=='0')

{
StringBuffer aaa=new StringBuffer(text.getText());
String aa=new String(aaa);
String bb="0";
if(aa.equals(bb)&&aa.length()==1){}
else
text.setText(text.getText()+"0");
}
else if(Ke.getKeyChar()=='.')
{ int gm;
gm=text.getText().length();

StringBuffer aaa=new StringBuffer(text.getText().substring(gm-1));
String aa=new String(aaa);
String bb=".";

if(aa.equals(bb)){return;}
else {

text.setText(text.getText()+".");
}
}//




else if(Ke.getKeyChar()=='+')
{
if(text.getText().length()>0)
{ flag=1;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}


}//

else if(Ke.getKeyChar()=='-')
{
if(text.getText().length()>0)
{ flag=2;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}


}//

else if(Ke.getKeyChar()=='*')
{
if(text.getText().length()>0)
{ flag=3;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}


}//

else if(Ke.getKeyChar()=='/')
{
if(text.getText().length()>0)
{ flag=4;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}


}//


else if(Ke.getKeyChar()=='=')
{if(text.getText().length()>0){
opt2=Double.valueOf(text.getText()).doubleValue();
if(flag==1){
opt1=opt1+opt2;
text.setText(String.valueOf(opt1));
}
if(flag==2){
opt1=opt1-opt2;
text.setText(String.valueOf(opt1));
}
if(flag==3){
opt1=opt1*opt2;
text.setText(String.valueOf(opt1));
}
if(flag==4){


if(opt2==0)
{
text.setText("除数不可以为零");
}
else {opt1=opt1/opt2;
text.setText(String.valueOf(opt1));

}
}

}} //
else if(Ke.getKeyChar()=='q')
{System.exit(0);}
else if(Ke.getKeyChar()=='b')
{
if(text.getText().equals(""))return;
else{
StringBuffer sb=new StringBuffer(text.getText());
sb.deleteCharAt(sb.length()-1);
String s9=new String(sb);
text.setText(s9);}}
else if(Ke.getKeyChar()=='d')

{text.setText("");
opt1=0;
opt2=0;}

}


public void keyReleased(KeyEvent Ke)
{}
public void keyTyped(KeyEvent Ke)
{}


}

}



问题还是很多的大家帮我看看呀
写的很垃圾 和你们的没法比 大家看看帮帮我呀!

[此贴子已经被作者于2006-5-17 18:50:59编辑过]


----------------解决方案--------------------------------------------------------

你这个也写得太长了吧


----------------解决方案--------------------------------------------------------