java计算器求解 功能跑步起来 求解!!!!!
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class work implements ActionListener {
public int num1,num2,nun3;
public int add,sub,div,mul;
public String str=null;
public int end;
public static void main(String[] args){
work homewok=new work();
}
public work()
{
JFrame jf1=null;
JPanel jp1=null,jp2=null;
JButton jb0=null,jb1=null,jb2=null,jb3=null,jb4=null,jb5=null,jb6=null,jb7=null,jb8=null,jb9=null,b1=null,b2=null,b3=null,b4=null,c1=null;;
JLabel jl1=null;
JTextField jt1=null;
jf1=new JFrame();
jp1=new JPanel(new FlowLayout());
jp2=new JPanel(new GridLayout(3,5));
jb0=new JButton("0");
jb0.addActionListener(this);
jb1=new JButton("1");
jb1.addActionListener(this);
jb2=new JButton("2");
jb2.addActionListener(this);
jb3=new JButton("3");
jb3.addActionListener(this);
jb4=new JButton("4");
jb4.addActionListener(this);
jb5=new JButton("5");
jb5.addActionListener(this);
jb6=new JButton("6");
jb6.addActionListener(this);
jb7=new JButton("7");
jb7.addActionListener(this);
jb8=new JButton("8");
jb8.addActionListener(this);
jb9=new JButton("9");
jb9.addActionListener(this);
b1=new JButton("+");
b1.addActionListener(this);
b2=new JButton("-");
b2.addActionListener(this);
b3=new JButton("*");
b3.addActionListener(this);
b4=new JButton("/");
b4.addActionListener(this);
c1=new JButton("=");
c1.addActionListener(this);
jl1=new JLabel("请输入数");
jt1=new JTextField(8);
jf1.add(jp1,BorderLayout.NORTH);
jf1.add(jp2,BorderLayout.SOUTH);
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jb0);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.add(jb5);
jp2.add(jb6);
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(b1);
jp2.add(b2);
jp2.add(b3);
jp2.add(b4);
jp2.add(c1);
jf1.setSize(400, 400);
jf1.setTitle("my work 计算器");
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setVisible(true);
}
public void num(int i)
{
String s=null;
s=String.valueOf(i);
if(end==1)
{
jt1.setText(0);
end=0;
}
if((jt1.getText()).equals("0"))
{
str=jt1.getText()+s;
jt1.setText(str);
}
}
public void actionPerformed( ActionEvent e)
{
if(e.getSource()==jb0)
num(0);
else if(e.getSource()==jb1)
num(1);
else if(e.getSource()==jb2)
num(2);
else if(e.getSource()==jb3)
num(3);
else if(e.getSource()==jb4)
num(4);
else if(e.getSource()==jb5)
num(5);
else if(e.getSource()==jb6)
num(6);
else if(e.getSource()==jb7)
num(7);
else if(e.getSource()==jb8)
num(8);
else if(e.getSource()==jb9)
num(9);
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
else if(e.getSource()==c1)
{
num2=Integer.parseInt(jt1.getText());
if(add==1)
num1=num1+num2;
else if(sub==1)
num1=num1+num2;
else if(div==1)
num1=num1/num2;
else if(mul==1)
num1=num1*num2;
jt1.setText(num1);
}
jt1.setText(String.valueOf(num1));
end=1;
}
public void sign(int i)
{
if(i==1)
{
add=1;
sub=0;
div=0;
mul=0;
}
if(i==2)
{
add=0;
sub=1;
div=0;
mul=0;
}
if(i==3)
{
add=0;
sub=0;
div=0;
mul=0;
}
if(i==4)
{
add=0;
sub=0;
div=1;
mul=0;
}
}
}
--------------------编程问答-------------------- 有计算器的简单操作代码
补充:Java , Java相关