String str = "12345";
为阿鲁科尔沁等地区用户提供了全套网页设计制作服务,及阿鲁科尔沁网站建设行业解决方案。主营业务为成都做网站、成都网站建设、成都外贸网站建设、阿鲁科尔沁网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
str = str.substring(0, str.length()-1);
int a = Integer.parseInt(str);
是的啊,退格就是\b。你可以用e.KeyCode==8试试。bs(backspace)
我这里有一个ascii码,你以后可以对照看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class General extends JFrame implements ActionListener
{
String args[]={"退格","总清","清零","/","1","2","3","+","4","5","6","-","7","8","9","*","0","+/-",".","="};
JButton jb[]=new JButton[20];
JPanel p1=new JPanel();
JTextField jtf=new JTextField(15);
double num1,num2,jieguo;
char c,ch;
public General()
{
Container ct=getContentPane();
jtf.addKeyListener(new KA());
jtf.setHorizontalAlignment(JTextField.RIGHT);
ct.add(jtf,"North");
p1.setLayout(new GridLayout(5,4));
for(int i=0;i20;i++)
{
jb[i]=new JButton(args[i]);
jb[i].addActionListener(this);
p1.add(jb[i]);
}
ct.add(p1);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new General();
}
public void actionPerformed(ActionEvent e)// 监听按键
{
String str=e.getActionCommand().trim();
if("0123456789.".indexOf(str)!=-1)
{
if(jtf.getText().equals("0"))jtf.setText(null);
jtf.setText(jtf.getText()+str);
return;
}
if("+-*/".indexOf(str)!=-1)
{
num1=Double.parseDouble(jtf.getText());
c=str.charAt(0);
jtf.setText(null);
return;
}
else if(str.equals("="))
{
num2=Double.parseDouble(jtf.getText());
switch(c)
{
case '+': jieguo=num1+num2; break;
case '-': jieguo=num1-num2; break;
case '*': jieguo=num1*num2; break;
case '/': jieguo=num1/num2; break;
}
jtf.setText(Double.toString(jieguo));
}
else if(str.equals("+/-"))
{
if(jtf.getText().isEmpty()==false)
{
Double lnum=-(Double.parseDouble(jtf.getText()));
jtf.setText(Double.toString(lnum));
}
else
{
jtf.setText("-");
}
}
else if(str.equals("清空"))
{
jtf.setText(null);
}
else if(str.equals("清零"))
{
jtf.setText("0");
}
else if(str.equals("退格"))
{
jtf.setText(jtf.getText().substring(0,jtf.getText().length()-1));
}
}
class KA extends KeyAdapter // 监听键盘
{
public void keyPressed(KeyEvent e)
{
char ch=e.getKeyChar();
if("+-*/".indexOf(ch)!=-1)
{
num1=Double.parseDouble(jtf.getText());
c=ch;
}
if(ch=='='|ch==e.VK_ENTER)
{
num2=Double.parseDouble(jtf.getText());
switch(c)
{
case '+': jieguo=num1+num2; break;
case '-': jieguo=num1-num2; break;
case '*': jieguo=num1*num2; break;
case '/': jieguo=num1/num2; break;
}
}
}
public void keyReleased(KeyEvent e)
{
char ch=e.getKeyChar();
if("+-*/".indexOf(ch)!=-1)
{
jtf.setText(null);
}
if(ch=='='|e.getKeyCode()==e.VK_ENTER)
{
jtf.setText(Double.toString(jieguo));
}
}
}
}