java编写计算器实现退格,倒数,小数点,正负号功能代码
Java编写简单的计算器,功能要求实现退格,倒数,小数点,正负号,等,代码、、、例如:importjava.awt.*;importjava.lang.Object;im...
Java编写简单的计算器,功能要求实现退格,倒数,小数点,正负号,等,代码、、、例如:import java.awt.*;import java.lang.Object;import java.awt.event.*;import java.lang.String;import java.lang.String;import javax.swing.*;import java.awt.TextComponent;public class test extends Frame implements ActionListener{Button btn;TextField numText;Panel btnPanel;test(String title){ super(title); String[]strBtn=new String[]{"1","2","3"," ","C","4","5","6","-","退格","7","8","9","*","1/x","0","-/ ",".","/","="}; numText=new TextField("0"); btnPanel=new Panel(); btnPanel.setLayout(new FlowLayout()); for(int i=0;i
展开
2个回答
展开全部
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;i<20;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));
}
}
}
}
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;i<20;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));
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询