java中设计一个计算器要求实现加减乘除。 可以实现加减,为什么乘除就不行嘞? 高手指点
我的程序如下importjavax.swing.*;importjava.awt.*;importjavax.swing.border.*;importjava.awt....
我的程序如下
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Example7 {
public static void main(String args[]){
WindowBox win=new WindowBox("简单计数器");
}
}
class WindowBox extends Frame implements ActionListener{
Box baseBox,boxV1,boxV2,boxV3;
Label labe1,labe2,labe3;
TextField text1,text2,text3;
Button butt1,butt2,butt3,butt4;
WindowBox(String s){
super(s);
boxV1=Box.createVerticalBox();
labe1=new Label("操作数");
boxV1.add(labe1);
boxV1.add(Box.createVerticalStrut(8));
labe2=new Label("操作数");
boxV1.add(labe2);
boxV1.add(Box.createVerticalStrut(8));
labe3=new Label("结果");
boxV1.add(labe3);
boxV2=Box.createVerticalBox();
text1=new TextField(20);
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
text2=new TextField(20);
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
text3=new TextField(20);
boxV2.add(text3);
boxV3=Box.createVerticalBox();
butt1=new Button("+");
boxV3.add(butt1);
butt1.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt2=new Button("-");
boxV3.add(butt2);
butt2.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt3=new Button("*");
boxV3.add(butt3);
butt3.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt4=new Button("/");
boxV3.add(butt4);
butt4.addActionListener(this);
/*butt1.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
butt4.addActionListener(this);
*/
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(50));
baseBox.add(boxV3);
setLayout(new FlowLayout());
add(baseBox);
setBounds(120,125,350,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
Integer m,n,p;
m=Integer.parseInt(text1.getText());
n=Integer.parseInt(text2.getText());
if(e.getSource()==butt1){
p=m+n;
text3.setText(m+"与"+n+"的和为:"+p);
}
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
if(e.getSource()==butt4){
p=m/n;
text3.setText(m+"与"+n+"的差为:"+p);
}
}
}
}
} 展开
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Example7 {
public static void main(String args[]){
WindowBox win=new WindowBox("简单计数器");
}
}
class WindowBox extends Frame implements ActionListener{
Box baseBox,boxV1,boxV2,boxV3;
Label labe1,labe2,labe3;
TextField text1,text2,text3;
Button butt1,butt2,butt3,butt4;
WindowBox(String s){
super(s);
boxV1=Box.createVerticalBox();
labe1=new Label("操作数");
boxV1.add(labe1);
boxV1.add(Box.createVerticalStrut(8));
labe2=new Label("操作数");
boxV1.add(labe2);
boxV1.add(Box.createVerticalStrut(8));
labe3=new Label("结果");
boxV1.add(labe3);
boxV2=Box.createVerticalBox();
text1=new TextField(20);
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
text2=new TextField(20);
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
text3=new TextField(20);
boxV2.add(text3);
boxV3=Box.createVerticalBox();
butt1=new Button("+");
boxV3.add(butt1);
butt1.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt2=new Button("-");
boxV3.add(butt2);
butt2.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt3=new Button("*");
boxV3.add(butt3);
butt3.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt4=new Button("/");
boxV3.add(butt4);
butt4.addActionListener(this);
/*butt1.addActionListener(this);
butt2.addActionListener(this);
butt3.addActionListener(this);
butt4.addActionListener(this);
*/
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(50));
baseBox.add(boxV3);
setLayout(new FlowLayout());
add(baseBox);
setBounds(120,125,350,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
Integer m,n,p;
m=Integer.parseInt(text1.getText());
n=Integer.parseInt(text2.getText());
if(e.getSource()==butt1){
p=m+n;
text3.setText(m+"与"+n+"的和为:"+p);
}
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
if(e.getSource()==butt4){
p=m/n;
text3.setText(m+"与"+n+"的差为:"+p);
}
}
}
}
} 展开
1个回答
展开全部
楼主太粗心啦,
下面这段代码缺括号
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
if(e.getSource()==butt4){
p=m/n;
text3.setText(m+"与"+n+"的差为:"+p);
}
下面是修改过的
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Example7 {
public static void main(String args[]){
WindowBox win=new WindowBox("简单计数器");
}
}
class WindowBox extends Frame implements ActionListener{
Box baseBox,boxV1,boxV2,boxV3;
Label labe1,labe2,labe3;
TextField text1,text2,text3;
Button butt1,butt2,butt3,butt4;
WindowBox(String s){
super(s);
setLayout(new FlowLayout());
boxV1=Box.createVerticalBox();
labe1=new Label("操作数");
boxV1.add(labe1);
boxV1.add(Box.createVerticalStrut(8));
labe2=new Label("操作数");
boxV1.add(labe2);
boxV1.add(Box.createVerticalStrut(8));
labe3=new Label("结果");
boxV1.add(labe3);
boxV2=Box.createVerticalBox();
text1=new TextField(20);
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
text2=new TextField(20);
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
text3=new TextField(20);
boxV2.add(text3);
boxV3=Box.createVerticalBox();
butt1=new Button("+");
boxV3.add(butt1);
butt1.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt2=new Button("-");
boxV3.add(butt2);
butt2.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt3=new Button("*");
boxV3.add(butt3);
butt3.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt4=new Button("/");
boxV3.add(butt4);
butt4.addActionListener(this);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(50));
baseBox.add(boxV3);
add(baseBox);
setBounds(120,125,350,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
int m,n,p;
m=Integer.parseInt(text1.getText());
n=Integer.parseInt(text2.getText());
if(e.getSource()==butt1){
p=m+n;
text3.setText(m+"与"+n+"的和为:"+p);
}
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
}
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
}
if(e.getSource()==butt4){
p=m/n;
System.out.println(p);
text3.setText(m+"与"+n+"的差为:"+p);
}
}
}
下面这段代码缺括号
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
if(e.getSource()==butt4){
p=m/n;
text3.setText(m+"与"+n+"的差为:"+p);
}
下面是修改过的
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Example7 {
public static void main(String args[]){
WindowBox win=new WindowBox("简单计数器");
}
}
class WindowBox extends Frame implements ActionListener{
Box baseBox,boxV1,boxV2,boxV3;
Label labe1,labe2,labe3;
TextField text1,text2,text3;
Button butt1,butt2,butt3,butt4;
WindowBox(String s){
super(s);
setLayout(new FlowLayout());
boxV1=Box.createVerticalBox();
labe1=new Label("操作数");
boxV1.add(labe1);
boxV1.add(Box.createVerticalStrut(8));
labe2=new Label("操作数");
boxV1.add(labe2);
boxV1.add(Box.createVerticalStrut(8));
labe3=new Label("结果");
boxV1.add(labe3);
boxV2=Box.createVerticalBox();
text1=new TextField(20);
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
text2=new TextField(20);
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
text3=new TextField(20);
boxV2.add(text3);
boxV3=Box.createVerticalBox();
butt1=new Button("+");
boxV3.add(butt1);
butt1.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt2=new Button("-");
boxV3.add(butt2);
butt2.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt3=new Button("*");
boxV3.add(butt3);
butt3.addActionListener(this);
boxV3.add(Box.createVerticalStrut(8));
butt4=new Button("/");
boxV3.add(butt4);
butt4.addActionListener(this);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(50));
baseBox.add(boxV3);
add(baseBox);
setBounds(120,125,350,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e){
int m,n,p;
m=Integer.parseInt(text1.getText());
n=Integer.parseInt(text2.getText());
if(e.getSource()==butt1){
p=m+n;
text3.setText(m+"与"+n+"的和为:"+p);
}
if(e.getSource()==butt2){
p=m-n;
text3.setText(m+"与"+n+"的差为:"+p);
}
if(e.getSource()==butt3){
p=m*n;
text3.setText(m+"与"+n+"的积为:"+p);
}
if(e.getSource()==butt4){
p=m/n;
System.out.println(p);
text3.setText(m+"与"+n+"的差为:"+p);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |