import java.awt.*;
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)富源免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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)// 監(jiān)聽按鍵
{
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 // 監(jiān)聽鍵盤
{
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));
}
}
}
}
是的啊,退格就是\b。你可以用e.KeyCode==8試試。bs(backspace)
我這里有一個ascii碼,你以后可以對照看
String str = "12345";
str = str.substring(0, str.length()-1);
int a = Integer.parseInt(str);
可能是因為eclipse的console吧。??赡茱@示模式是Unicode,然后\b就變成了個框框。你可以試一下直接在cmd(命令提示符)里面用java命令行跑
package?jisuanqi;
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
public?class?Calculator?extends?JFrame?implements?ActionListener{?????//建一個Calculator類繼承JFrame實現(xiàn)ActionListener
public?static?void?main?(String?args[]){
Calculator?calculator=new?Calculator();?????//創(chuàng)建對象calculator
calculator.setVisible(true);???????????????//窗口可見
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??//關(guān)閉圖標(biāo)后結(jié)束程序
}
String?keys?[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//表示計算器的鍵的名字
String?command?[]={"backspace","C"};//功能鍵
JButton?KEYS?[]=new?JButton[keys.length];//創(chuàng)建鍵對象
JButton?COMMAND[]=new?JButton[command.length];//功能鍵對象
static?JTextField?resultSet=new?JTextField();//建一個文本框
double?resultNum;????????//結(jié)果
String?operator="";//運算符??---------------------------
public?Calculator()?{//????構(gòu)造函數(shù)
super();??????
init();//初始化計算器
this.setBackground(Color.WHITE);//this代表用當(dāng)前構(gòu)造方法創(chuàng)建的對象
this.setTitle("簡單計算器");????//窗口標(biāo)題
this.setLocation(500,?300);???//窗口大小
this.pack(); //使組件大小合適
}
void?init()?{??????//初始化計算器
resultSet.setHorizontalAlignment(JTextField.RIGHT);?????//設(shè)置文本框內(nèi)容向右對齊
resultSet.setBackground(Color.WHITE);???????????//設(shè)置文本框背景顏色
JPanel?keysPanel=new?JPanel();//創(chuàng)建一個面板
keysPanel.setLayout(new?GridLayout(4,5,3,3));?//設(shè)置面板的布局為網(wǎng)格型
for?(int?i=0;ikeys.length;i++)?{??????//把數(shù)字鍵和符號鍵設(shè)為按鈕
KEYS[i]=new?JButton(keys[i]);
keysPanel.add(KEYS[i]);????????????//把按鈕加到面板中
}
JPanel?comPanel=new?JPanel();???????//創(chuàng)建命令鍵面板
comPanel.setLayout(new?GridLayout(1,2,3,3));//設(shè)置面板布局為網(wǎng)格型?
for(int?i=0;icommand.length;i++)?{????//把命令鍵設(shè)置為按鈕
COMMAND[i]=new?JButton(command[i]);
comPanel.add(COMMAND[i]);??????????//把按鈕放到面板上
}
JPanel?pane=new?JPanel();??????//創(chuàng)建面板
pane.setLayout(new?BorderLayout());??//面板布局為區(qū)域型
pane.add(comPanel,BorderLayout.NORTH);?????//把命令面板加入到大面板上
pane.add(keysPanel,BorderLayout.CENTER); //把數(shù)字鍵面板加到大面板
getContentPane().setLayout(new?BorderLayout());?????//設(shè)置底層容器的布局為區(qū)域型
getContentPane().add(resultSet,BorderLayout.NORTH);???//把文本框放到北面
getContentPane().add(pane,BorderLayout.CENTER);????//把大面板放到中央
for(int?i=0;ikeys.length;i++){
KEYS[i].addActionListener(this);//為按鈕添加監(jiān)聽器
}
for(int?i=0;icommand.length;i++){
COMMAND[i].addActionListener(this);???
}
}
//處理事件 ?
public?void?actionPerformed(ActionEvent?e){??
String?lable=e.getActionCommand();
if(lable.equals("backspace")){??????//如果命令為backspace
backspace();
}
else?if?(lable.equals("C")){?????//如果命令為C
Clear();
}
else?if?(Character.isDigit(lable.charAt(0)))?{???????//---------------
if(resultSet.getText().equals("0")){
resultSet.setText("");
}
resultSet.setText(resultSet.getText()+lable);
}
else?if?(lable.equals(".")){
point();
}
else?if?(lable.equals("=")){
equal();
}
else?{
doOperation();
resultSet.setText("");
operator=lable;
}
}
void?point()?{
String?s=resultSet.getText();
if(s.indexOf(".")==-1){
resultSet.setText(s+".");
}
}
void?equal()?{
doOperation();
resultSet.setText(""+resultNum);
operator="";
}
void?doOperation()?{
double?m=Double.parseDouble(resultSet.getText());
if(operator.equals("")){
resultNum=m;
}
if(operator.equals("+")){
resultNum+=m;
}
if(operator.equals("-")){
resultNum-=m;
}
if(operator.equals("*")){
resultNum*=m;
}
if(operator.equals("/")){
resultNum/=m;
}
}
void?Clear()?{?????//清零
resultSet.setText("0");
}
void?backspace()?{????//退格
String?text?=resultSet.getText();
if?(text.length()0){
resultSet.setText(text.substring(0,?text.length()-1));//將文本最后一個字符刪除
}
else?if?(text.length()==0){
resultSet.setText("0");
}
}
}
\b?代表?一個單詞的邊界
這是?JDK1.6?中的?API?說的
\b是正則表達(dá)式規(guī)定的一個特殊代碼(某些人叫它元字符,metacharacter),代表著單詞的開頭或結(jié)尾,也就是單詞的分界處。雖然通常英文的單詞是由空格,標(biāo)點符號或者換行來分隔的,但是\b并不匹配這些單詞分隔字符中的任何一個,它只匹配一個位置
;nbsp;??如何這篇文章你能看完,相信一定對你有相當(dāng)大的幫忙