急求用VB编辑简易计算器代码

要求有0~9十个数字,一个加号,一个等号,一个小数点,一个清空等14个按键的计算器,还有一个文本框

第1个回答  2008-11-23
简易计算器代码2008-09-25 09:33package javaapplication1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestZ extends JFrame implements ActionListener{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;

public TestZ(){
super("计算器");
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));
resultField=new JTextField("0");
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);
s1=new JButton(" 1 "); s1.addActionListener(this);
s2=new JButton(" 2 "); s2.addActionListener(this);
s3=new JButton(" 3 "); s3.addActionListener(this);
s4=new JButton(" 4 "); s4.addActionListener(this);
s5=new JButton(" 5 "); s5.addActionListener(this);
s6=new JButton(" 6 "); s6.addActionListener(this);
s7=new JButton(" 7 "); s7.addActionListener(this);
s8=new JButton(" 8 "); s8.addActionListener(this);
s9=new JButton(" 9 "); s9.addActionListener(this);
s0=new JButton(" 0 "); s0.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);
f1=new JButton(" . "); f1.addActionListener(this);
f2=new JButton(" = "); f2.addActionListener(this);
jPanel2.add(s1);
jPanel2.add(s2);
jPanel2.add(s3);
jPanel2.add(b1);
jPanel2.add(s4);
jPanel2.add(s5);
jPanel2.add(s6);
jPanel2.add(b2);
jPanel2.add(s7);
jPanel2.add(s8);
jPanel2.add(s9);
jPanel2.add(b3);
jPanel2.add(s0);
jPanel2.add(f1);
jPanel2.add(f2);
jPanel2.add(b4);
con.add(jPanel2,BorderLayout.CENTER);

}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;

}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);

}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);

}
}

public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);

//符号事件
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()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());

if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}

}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
TestZ th1=new TestZ();
th1.show();
}
}
第2个回答  2008-11-23
懒的写了,给你找了一个
Dim data
Dim a

Private Sub Command1_Click(index As Integer)
Text1.Text = Text1.Text + "1"
End Sub
Private Sub Command2_Click(index As Integer)
Text1.Text = Text1.Text + "2"
End Sub
Private Sub Command3_Click(index As Integer)
Text1.Text = Text1.Text + "3"
End Sub
Private Sub Command4_Click(index As Integer)
Text1.Text = Text1.Text + "4"
End Sub
Private Sub Command5_Click(index As Integer)
Text1.Text = Text1.Text + "5"
End Sub
Private Sub Command6_Click(index As Integer)
Text1.Text = Text1.Text + "6"
End Sub
Private Sub Command7_Click(index As Integer)
Text1.Text = Text1.Text + "7"
End Sub
Private Sub Command8_Click(index As Integer)
Text1.Text = Text1.Text + "8"
End Sub
Private Sub Command9_Click(index As Integer)
Text1.Text = Text1.Text + "9"
End Sub
Private Sub Command0_Click(index As Integer)
Text1.Text = Text1.Text + "0"
End Sub

Private Sub f_click()
If Val(Text1.Text) > 0 Then
Text1.Text = "-" + Text1.Text
ElseIf Text1.Text = "" Then
Text1.Text = "-"
Else: Text1.Text = -Val(Text1.Text)
End If
End Sub

Private Sub dot_click()
Text1.Text = Text1.Text + "."
End Sub

Private Sub clean_click()
data = 0
Text1.Text = ""
End Sub
Private Sub clean2_click()
Text1.Text = ""
End Sub

Private Sub add_click()
data = Val(Text1.Text)
a = 1
Text1.Text = ""
End Sub
Private Sub minus_click()
data = Val(Text1.Text)
a = 2
Text1.Text = ""
End Sub
Private Sub times_click()
data = Val(Text1.Text)
a = 3
Text1.Text = ""
End Sub
Private Sub divide_click()
data = Val(Text1.Text)
a = 4
Text1.Text = ""
End Sub
Private Sub pingfanggen_click()
If Val(Text1.Text) >= 0 Then
data = Val(Text1.Text)
Text1.Text = Sqr(data)
Else: Text1.Text = "Error!"
End If
End Sub
Private Sub mi_click()
data = Val(Text1.Text)
a = 5
Text1.Text = ""
End Sub

Private Sub equal_click()
If Text1.Text = "" Then
Text1.Text = data
Else
If a = 1 Then
Text1.Text = data + Val(Text1.Text)
End If
If a = 2 Then
Text1.Text = data - Val(Text1.Text)
End If
If a = 3 Then
Text1.Text = data * Val(Text1.Text)
End If
If a = 4 Then
If text1.text=0 then
Text1.text=”Error!”
else
Text1.Text = data / Val(Text1.Text)
End if
End If
If a = 5 Then
b = 1
For i = 1 To Val(Text1.Text)
b = b * data
Next i
Text1.Text = b
End If
End If
End Sub

参考资料:http://bbs.zdnet.com.cn/viewthread.php?tid=572421

本回答被网友采纳
第3个回答  2008-11-23
Private Sub btnnums_Click(Index As Integer)
If Index = 10 Then
If InStr(lblxs.Caption, ".") Then
Exit Sub
Else
lblxs.Caption = lblxs.Caption + "."
Exit Sub
End If
End If
If lblxs.Caption <> "0" Then
lblxs.Caption = lblxs.Caption + btnnums(Index).Caption
Else
lblxs.Caption = btnnums(Index).Caption
End If
End Sub

Private Sub btnce_Click()
lblxs.Caption = "0"
End Sub

Private Sub btnfh_Click(Index As Integer)
num1 = Val(lblxs.Caption)
lblxs.Caption = "0"
num2 = Val(lblxs.Caption)
If btnfh(0) Then
fh = "+"
ElseIf btnfh(2) Then
fh = "-"
ElseIf btnfh(1) Then
fh = "*"
ElseIf btnfh(3) Then
fh = "/"
End If
End Sub

Private Sub Command3_Click()
num2 = Val(lblxs.Caption)
Select Case fh
Case "+"
num1 = num1 + num2
Case "-"
num1 = num1 - num2
Case "*"
num1 = num1 * num2
Case "/"
num1 = num1 / num2
End Select
lblxs.Caption = num1
End Sub
第4个回答  2008-11-23
超级简单~!没有满意答案的话我再给你做吧

vb:简易计算器(加减乘除)代码
代码如下:Dim t, t1 As Integer Dim x, y As Double Public Sub com()x = Val(l1.Caption)Select Case t1 Case Is = 1: y = y + x Case Is = 2: y = y - x Case Is = 3: y = y * x Case Is = 4: y = y \/ x End Select t1 = 0 t = 0 l1.Caption = "0"...

VB高手进,求一个简单计算器的VB代码
这是一个简易的VB计算器代码。首先定义了几个变量:Num1和Num2作为浮点数储存。StrNum1和StrNum2作为字符串储存输入的数值。FirstNum判断是否是数字开头。PointFlag判断是否已有小数点。Runsign储存运算符号。SignFlag判断是否已有运算符号。接着是ClearData子过程,用于清空储存的数据。Run子过程用于执行运算。

VB简易计算器代码怎么写
Dim a(100) As Double Dim n& Dim Plus As Boolean Dim yunsuan(100) As Integer '---这里增加 Dim a100 As Double Private Sub cmd1_Click()If Plus = True Then Plus = False Text1.Text = "1"Else Text1.Text = Text1.Text & "1"End If End Sub Private Sub cmd2_Click()If...

用Visual Basic 语言怎样编写简单计算器(记事本 )
Private Sub cmdcalc_Click()If opernum < 2 Then Exit Sub Select Case opersymbol Case 0 num1 = num1 + num2 Case 1 num1 = num1 - num2 Case 2 num1 = num1 * num2 case3 If num2 <> 0 Then num1 = num1 \/ num2 Else MsgBox "除数不能为0"End If End Select opernu...

求有数字按键的简单vb计算器代码,谢谢
编写一个简单的VB计算器代码,需要定义一些变量和常量。首先声明`StoredValue`为双精度数,`Operator`表示操作符,初始值设为`opNone`。在代码中,实现删除最后字符的功能,通过`cmdDelete_Click`事件处理。清除显示内容并保存操作符的代码在`cmdClear_Click`中执行。实现清除显示内容的逻辑在于`cmdClear...

急急急,求大神用vb教做一个简易计算器,加减乘除就可以。能用加分
添加1个Command控件等号按钮“=”,name=cmdEqual 添加1个Command控件数组(4个符号)用作加减乘除按钮,name=cmdArithmetic,index=0~3 好了,然后复制以下代码运行即可成功啦:'Private strCalculateType As String '存放运算类型 Private blnCalFlag As Boolean '标记是否已经输入加数、或减数、或乘数、或...

做两个VB程序 简单题目
1.用VB编程序设计一个简易的计算器 代码:Dim num_1, num2, flagPrivate Sub Command1_Click(Index As Integer) Select Case Index Case 0 To 9 Label1.Caption = Label1.Caption + CStr(Command1(Index).Caption) Case 10 Label1.Caption = Label1.Caption + "." Case 1...

用vb设计一个计算器,里面的加减乘除按键的代码怎么写
select case sender.text ' 根据按下的按钮的文本属性确定你要做什么,这里可以用加减乘除号代替 case "加"操作 = "加"case "减"操作 = "减"case "乘"操作 = "乘"case "除"操作 = "除"end select end sub private sub 计算(byval sender as object, byval e as eventargs) handles ...

用VB做一个简单的加减乘除的计算器,代码是什么?
我们现在要把command拉出四个,把里面command1-4都给成+-*\/ 现在我们现在要双击+,我们开始进行输入代码 在里面我们现在要:text3.text=val(text1.text)+val(text2.text)我们要把这个(text3.text=val(text1.text)+val(text2.text))复制,把里面+号分别改成-*\/ 如图,就像这样就可以了,...

在VB中计算器的代码(要求0到9的加减乘除运算)
'最简单的计算器(实现0到9的加减乘除运算)'1、添加Command1控件数组0~14 '2、设置Command1的Caption属性:'Command1(0~9) "0~9"'Command1(10~13) "+、-、*、\/"'Command1(14) "="'3、添加Text1控件 '排列好以上控件 Option Explicit Dim TTemp1, TTemp2 As Double '记录前后输入的...

相似回答