Java程序题
3.一个Java小应用程序,界面有三个文本框和两个按钮,前两个文本框用于输入整数,第三个文本框用于输出结果。两个按钮分别表示加和减运算。当前两个文本框输入整数后,点击相应...
3. 一个Java小应用程序,界面有三个文本框和两个按钮,前两个文本框用于输入整数,第三个文本框用于输出结果。两个按钮分别表示加和减运算。当前两个文本框输入整数后,点击相应按钮,程序就将计算结果输出在第三个文本框中。以下是程序的大部分,其中监视按钮事件的方法未给出,试编写处理按钮事件的方法。
注:这里是给定部分的程序,你要编写的是actionPerformed(ActionEvent e)方法。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Panel1 extends JPanel {
JTextField text1, text2, text3;
JButton addButton, subButton;
Panel1() {
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
setLayout(new GridLayout(5, 1));
addButton = new JButton("加");
subButton = new JButton("减");
add(text1);
add(text2);
add(text3);
add(addButton);
add(subButton);
}
}
public class Classl extends Applet implements ActionListener {
Panel1 panel;
public void init() {
setSize(100, 200);
panel = new Panel1();
add(panel);
(panel.addButton).addActionListener(this);
(panel.subButton).addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//这double a = Double.parseDouble(num1.getText());
double b = Double.parseDouble(num2.getText());
if (e.getSource() == plus)
result.setText(Double.toString(a + b));
if (e.getSource() == minus)
result.setText(Double.toString(a - b));
里是你要编写的代码
}
}
急求 谢谢发我邮箱也可以745160328@qq.com 展开
注:这里是给定部分的程序,你要编写的是actionPerformed(ActionEvent e)方法。
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Panel1 extends JPanel {
JTextField text1, text2, text3;
JButton addButton, subButton;
Panel1() {
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
setLayout(new GridLayout(5, 1));
addButton = new JButton("加");
subButton = new JButton("减");
add(text1);
add(text2);
add(text3);
add(addButton);
add(subButton);
}
}
public class Classl extends Applet implements ActionListener {
Panel1 panel;
public void init() {
setSize(100, 200);
panel = new Panel1();
add(panel);
(panel.addButton).addActionListener(this);
(panel.subButton).addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//这double a = Double.parseDouble(num1.getText());
double b = Double.parseDouble(num2.getText());
if (e.getSource() == plus)
result.setText(Double.toString(a + b));
if (e.getSource() == minus)
result.setText(Double.toString(a - b));
里是你要编写的代码
}
}
急求 谢谢发我邮箱也可以745160328@qq.com 展开
2个回答
展开全部
已经写好了啊
double a = Double.parseDouble(num1.getText());
double b = Double.parseDouble(num2.getText());
if (e.getSource() == plus)
result.setText(Double.toString(a + b));
if (e.getSource() == minus)
result.setText(Double.toString(a - b));
double a = Double.parseDouble(num1.getText());
double b = Double.parseDouble(num2.getText());
if (e.getSource() == plus)
result.setText(Double.toString(a + b));
if (e.getSource() == minus)
result.setText(Double.toString(a - b));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |