十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.awt.*;
成都创新互联公司主营昂昂溪网站建设的网络公司,主营网站建设方案,重庆APP开发公司,昂昂溪h5小程序制作搭建,昂昂溪网站营销推广欢迎昂昂溪等地区企业咨询
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class jsq
{
public static void main(String[] str)
{
jisuanqi jsq=new jisuanqi();
jsq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jsq.setVisible(true);jsq.setSize(300,300);
}
}
class jisuanqi extends JFrame implements ActionListener
{
double a=0,b=0,c=0,fuhao=5;
Boolean has=false;
Container A;
JTextField tf1;
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,bt_dian;
JButton bt_jia,bt_jian,bt_cheng,bt_chu,bt_fuhao;
JButton bt_clear,bt_jisuan;
public jisuanqi()
{
setTitle("猛棚简单计算器");
A=getContentPane();
GridBagLayout f=new GridBagLayout();
A.setLayout(f);
GridBagConstraints g=new GridBagConstraints();
g.fill=GridBagConstraints.BOTH;
g.gridwidth=1;
g.gridheight=1;
//面板的实例化
//显示框的实例化
tf1=new JTextField(10);tf1.setEditable(false);
//数字键的实例化
bt1=new JButton("1");bt2=new JButton("2");bt3=new JButton("3");
bt4=new JButton("4");bt5=new JButton("5");bt6=new JButton("6");
bt7=new JButton("7");bt8=new JButton("8");bt9=new JButton("腔知知9");
bt0=new JButton("0");bt_dian=new JButton(".");bt_fuhao=new JButton("+/-");
//计算符号的实例化
bt_jia=new JButton("+");bt_jian=new JButton("-");
bt_cheng=new JButton("*");bt_chu=new JButton("/伍消");
//清空和计算按钮的实例化
bt_clear=new JButton("clear");bt_jisuan=new JButton("=");
//布局
g.gridx=1;g.gridy=0;A.add(tf1,g);
g.gridx=0;g.gridy=1;A.add(bt_clear,g);
g.gridx=3;g.gridy=1;A.add(bt_jisuan,g);
g.gridx=0;g.gridy=2;A.add(bt7,g);
g.gridx=1;g.gridy=2;A.add(bt8,g);
g.gridx=2;g.gridy=2;A.add(bt9,g);
g.gridx=3;g.gridy=2;A.add(bt_jia,g);
g.gridx=0;g.gridy=3;A.add(bt4,g);
g.gridx=1;g.gridy=3;A.add(bt5,g);
g.gridx=2;g.gridy=3;A.add(bt6,g);
g.gridx=3;g.gridy=3;A.add(bt_jian,g);
g.gridx=0;g.gridy=4;A.add(bt1,g);
g.gridx=1;g.gridy=4;A.add(bt2,g);
g.gridx=2;g.gridy=4;A.add(bt3,g);
g.gridx=3;g.gridy=4;A.add(bt_cheng,g);
g.gridx=0;g.gridy=5;A.add(bt0,g);
g.gridx=1;g.gridy=5;A.add(bt_fuhao,g);
g.gridx=2;g.gridy=5;A.add(bt_dian,g);
g.gridx=3;g.gridy=5;A.add(bt_chu,g);
//添加监听
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
//清除、小数点、符号添加监听
bt_clear.addActionListener(this);
bt_dian.addActionListener(this);
bt_fuhao.addActionListener(this);
//符号添加监听
bt_jia.addActionListener(this);
bt_jian.addActionListener(this);
bt_cheng.addActionListener(this);
bt_chu.addActionListener(this);
//计算符号添加监听
bt_jisuan.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//数字键
if(e.getSource()==bt1)
{tf1.setText(tf1.getText()+"1");}
else if(e.getSource()==bt2)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"2");
}
else if(e.getSource()==bt3)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"3");
}
else if(e.getSource()==bt4)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"4");
}
else if(e.getSource()==bt5)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"5");
}
else if(e.getSource()==bt6)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"6");
}
else if(e.getSource()==bt7)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"7");
}
else if(e.getSource()==bt8)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"8");
}
else if(e.getSource()==bt9)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"9");
}
else if(e.getSource()==bt0)
{
if(!tf1.getText().equals("0"))
{
tf1.setText(tf1.getText()+"0");
}
}
else if(e.getSource()==bt_dian)//小数点符号
{
if(tf1.getText().indexOf(".")==-1tf1.getText().length()0)
{
tf1.setText(tf1.getText()+".");
}
}
//运算符号
//fuhao的0,1,2,3分别表示加、减、乘、除
else if(e.getSource()==bt_jia)
{
if(tf1.getText().trim().length()0)
{ a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=0;
has=true;
}
}
else if(e.getSource()==bt_jian)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=1;
has=true;
}
}
else if(e.getSource()==bt_cheng)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=2;
has=true;
}
}
else if(e.getSource()==bt_chu)
{
if(tf1.getText().trim().length()0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=3;
has=true;
}
}
//计算、符号、清空
else if(e.getSource()==bt_jisuan)//计算结果
{
try
{
if(tf1.getText().length()0has)
{
b=Double.valueOf(tf1.getText());
if(fuhao==0)
{
c=a+b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==1)
{
c=a-b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==2)
{
c=a*b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==3)
{
c=a/b;
tf1.setText(String.valueOf(c));
has=false;
}
}
}
catch(Exception ex)
{tf1.setText(ex.getMessage().toString());}
}
else if(e.getSource()==bt_fuhao)
{
if(tf1.getText().indexOf("-")==-1tf1.getText().length()0)
{
String s=tf1.getText();
tf1.setText("-"+s);
}
else
{
if(tf1.getText().length()0)
{
String s=tf1.getText().substring(1);
tf1.setText(s);
}
}
}
else if(e.getSource()==bt_clear)
{
tf1.setText("");
}
}
}