java加减乘除程序。public void actionPerformed(ActionEvent e) 这个下面的我知道要分配下
importjava.applet.Applet;importjava.awt.*;importjava.awt.event.*;publicclassJisuanext...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Jisuan extends Applet implements ActionListener
{
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("相加");
Button button2 = new Button("相减");
Button button3 = new Button("相乘");
Button button4 = new Button("相除");
public void init()
{
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String buttonText = ((Button)e.getSource()).getLabel();
System.out.print("按钮"+buttonText);
int x = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
int y = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(y));
int z = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(z));
int i = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(i));
}
}
貌似buttonText是关键 不知道怎样分配。。给加减乘除 哎。。纠结 。感觉用到SWITCH结构。。 展开
import java.awt.*;
import java.awt.event.*;
public class Jisuan extends Applet implements ActionListener
{
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("相加");
Button button2 = new Button("相减");
Button button3 = new Button("相乘");
Button button4 = new Button("相除");
public void init()
{
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String buttonText = ((Button)e.getSource()).getLabel();
System.out.print("按钮"+buttonText);
int x = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
int y = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(y));
int z = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(z));
int i = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(i));
}
}
貌似buttonText是关键 不知道怎样分配。。给加减乘除 哎。。纠结 。感觉用到SWITCH结构。。 展开
2个回答
展开全部
帮你改好了。现在在前两个框输入数字之后,点击加减乘除按钮之一,label1会跟着变成加号或减号等,并作出正确的计算,结果显示在TextField3中。
添加的地方见注释~
package tt.thread;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddSubMulDiv extends Applet implements ActionListener {
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("add");
Button button2 = new Button("subtract");
Button button3 = new Button("multiply");
Button button4 = new Button("divide");
public void init() {
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String buttonText = ((Button) e.getSource()).getLabel();
System.out.print("按钮" + buttonText);
if (e.getSource().equals(button1)) { //判断本次点击事件点击的是哪个按钮,作出相应操作
int x = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText());
label1.setText("+"); //修改加减乘除号的显示
field3.setText(Integer.toString(x)); //结果显示在第三个文本框
}
if (e.getSource().equals(button2)) {
int y = Integer.parseInt(field1.getText()) - Integer.parseInt(field2.getText()); //点击减号的时候,做减法
label1.setText("-");
field3.setText(Integer.toString(y));
}
if (e.getSource().equals(button3)) {
int z = Integer.parseInt(field1.getText()) * Integer.parseInt(field2.getText());//点击乘号的时候,做乘法
label1.setText("*");
field3.setText(Integer.toString(z));
}
if (e.getSource().equals(button4)) {
int i = Integer.parseInt(field1.getText()) / Integer.parseInt(field2.getText());//点击除号的时候,做除法
label1.setText("/");
field3.setText(Integer.toString(i));
}
}
}
添加的地方见注释~
package tt.thread;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddSubMulDiv extends Applet implements ActionListener {
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("add");
Button button2 = new Button("subtract");
Button button3 = new Button("multiply");
Button button4 = new Button("divide");
public void init() {
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String buttonText = ((Button) e.getSource()).getLabel();
System.out.print("按钮" + buttonText);
if (e.getSource().equals(button1)) { //判断本次点击事件点击的是哪个按钮,作出相应操作
int x = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText());
label1.setText("+"); //修改加减乘除号的显示
field3.setText(Integer.toString(x)); //结果显示在第三个文本框
}
if (e.getSource().equals(button2)) {
int y = Integer.parseInt(field1.getText()) - Integer.parseInt(field2.getText()); //点击减号的时候,做减法
label1.setText("-");
field3.setText(Integer.toString(y));
}
if (e.getSource().equals(button3)) {
int z = Integer.parseInt(field1.getText()) * Integer.parseInt(field2.getText());//点击乘号的时候,做乘法
label1.setText("*");
field3.setText(Integer.toString(z));
}
if (e.getSource().equals(button4)) {
int i = Integer.parseInt(field1.getText()) / Integer.parseInt(field2.getText());//点击除号的时候,做除法
label1.setText("/");
field3.setText(Integer.toString(i));
}
}
}
追问
顺便问下。。前面加个Package 什么意思?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package cn.ShawnChu.calculator;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("相加");
Button button2 = new Button("相减");
Button button3 = new Button("相乘");
Button button4 = new Button("相除");
public void init()
{
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// String buttonText = ((Button)e.getSource()).getLabel();
// System.out.print("按钮"+buttonText);
try {
if (e.getSource()==button1) {
label1.setText("+");
int x = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
else if (e.getSource()==button2) {
label1.setText("-");
int y = Integer.parseInt(field1.getText())-Integer.parseInt(field2.getText());
field3.setText(Integer.toString(y));
}
else if (e.getSource()==button3) {
label1.setText("*");
int z = Integer.parseInt(field1.getText())*Integer.parseInt(field2.getText());
field3.setText(Integer.toString(z));
}
else if (e.getSource()==button4) {
label1.setText("/");
int i = Integer.parseInt(field1.getText())/Integer.parseInt(field2.getText());
field3.setText(Integer.toString(i));
}
else {
System.err.println("错误");
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
楼主,不知道这样对你有帮助么。
1.通过e.getsource来判断是哪一个Button触发的listener
2.加入try来处理异常
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
Label label1 = new Label("+");
Label label2 = new Label("=");
TextField field1 = new TextField(6);
TextField field2 = new TextField(6);
TextField field3 = new TextField(6);
Button button1 = new Button("相加");
Button button2 = new Button("相减");
Button button3 = new Button("相乘");
Button button4 = new Button("相除");
public void init()
{
add(field1);
add(label1);
add(field2);
add(label2);
add(field3);
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// String buttonText = ((Button)e.getSource()).getLabel();
// System.out.print("按钮"+buttonText);
try {
if (e.getSource()==button1) {
label1.setText("+");
int x = Integer.parseInt(field1.getText())+Integer.parseInt(field2.getText());
field3.setText(Integer.toString(x));
}
else if (e.getSource()==button2) {
label1.setText("-");
int y = Integer.parseInt(field1.getText())-Integer.parseInt(field2.getText());
field3.setText(Integer.toString(y));
}
else if (e.getSource()==button3) {
label1.setText("*");
int z = Integer.parseInt(field1.getText())*Integer.parseInt(field2.getText());
field3.setText(Integer.toString(z));
}
else if (e.getSource()==button4) {
label1.setText("/");
int i = Integer.parseInt(field1.getText())/Integer.parseInt(field2.getText());
field3.setText(Integer.toString(i));
}
else {
System.err.println("错误");
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
楼主,不知道这样对你有帮助么。
1.通过e.getsource来判断是哪一个Button触发的listener
2.加入try来处理异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询