用JAVA编个小程序,要详细注释,有急用,求高手解答!!非常感谢(如下图)
答案:1 悬赏:20 手机版
解决时间 2021-04-02 07:18
- 提问者网友:战魂
- 2021-04-01 11:09
用JAVA编个小程序,要详细注释,有急用,求高手解答!!非常感谢(如下图)
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-04-01 11:57
//MySimpleCalculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MySimpleCalculator extends JApplet implements ActionListener{
JTextField txtNum1, txtNum2, txtResult;
JButton btnAdd, btnSubtract, btnProduct, btnDivide;
public void initComponents(){
txtNum1= new JTextField(10);
txtNum2= new JTextField(10);
txtResult= new JTextField(12);
btnAdd = new JButton("+");
btnSubtract = new JButton("-");
btnProduct = new JButton("*");
btnDivide = new JButton("/");
this.setLayout(new FlowLayout());
this.add(new JLabel("输入数字"));
this.add(txtNum1);
this.add(new JLabel("输入数字"));
this.add(txtNum2);
this.add(new JLabel("总数"));
this.add(txtResult);
this.add(btnAdd);
this.add(btnSubtract);
this.add(btnProduct);
this.add(btnDivide);
btnAdd.addActionListener(this);
btnSubtract.addActionListener(this);
btnProduct.addActionListener(this);
btnDivide.addActionListener(this);
this.setSize(300, 300);
this.setVisible(true);
}
public MySimpleCalculator(){
initComponents();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
double num1, num2, result;
if(txtNum1.getText().equals("") || txtNum2.getText().equals("")){
JOptionPane.showMessageDialog(this, "输入数字不能为空");
return;
}
try{
num1 = Double.parseDouble(txtNum1.getText().trim());
num2 = Double.parseDouble(txtNum2.getText().trim());
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "只能输入数字");
return;
}
if(btn.getText().equals("+")){
result = num1 + num2;
}
else if(btn.getText().equals("-")){
result = num1 - num2;
}
else if(btn.getText().equals("*")){
result = num1 * num2;
}
else if(btn.getText().equals("/")){
if(num2 == 0){
JOptionPane.showMessageDialog(this, "除数不能为0");
return;
}
result = num1 / num2;
}
else{
result = 0;
}
txtResult.setText(result + "");
}
}
运行测试:
追问请帮忙写下注释好吗,拜托了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MySimpleCalculator extends JApplet implements ActionListener{
JTextField txtNum1, txtNum2, txtResult;
JButton btnAdd, btnSubtract, btnProduct, btnDivide;
public void initComponents(){
txtNum1= new JTextField(10);
txtNum2= new JTextField(10);
txtResult= new JTextField(12);
btnAdd = new JButton("+");
btnSubtract = new JButton("-");
btnProduct = new JButton("*");
btnDivide = new JButton("/");
this.setLayout(new FlowLayout());
this.add(new JLabel("输入数字"));
this.add(txtNum1);
this.add(new JLabel("输入数字"));
this.add(txtNum2);
this.add(new JLabel("总数"));
this.add(txtResult);
this.add(btnAdd);
this.add(btnSubtract);
this.add(btnProduct);
this.add(btnDivide);
btnAdd.addActionListener(this);
btnSubtract.addActionListener(this);
btnProduct.addActionListener(this);
btnDivide.addActionListener(this);
this.setSize(300, 300);
this.setVisible(true);
}
public MySimpleCalculator(){
initComponents();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
double num1, num2, result;
if(txtNum1.getText().equals("") || txtNum2.getText().equals("")){
JOptionPane.showMessageDialog(this, "输入数字不能为空");
return;
}
try{
num1 = Double.parseDouble(txtNum1.getText().trim());
num2 = Double.parseDouble(txtNum2.getText().trim());
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "只能输入数字");
return;
}
if(btn.getText().equals("+")){
result = num1 + num2;
}
else if(btn.getText().equals("-")){
result = num1 - num2;
}
else if(btn.getText().equals("*")){
result = num1 * num2;
}
else if(btn.getText().equals("/")){
if(num2 == 0){
JOptionPane.showMessageDialog(this, "除数不能为0");
return;
}
result = num1 / num2;
}
else{
result = 0;
}
txtResult.setText(result + "");
}
}
运行测试:
追问请帮忙写下注释好吗,拜托了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯