发布网友 发布时间:2024-10-23 08:35
共3个回答
热心网友 时间:1天前
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI_Calculate extends JApplet implements ActionListener{
JTextField txtNum, txtSum, txtProduct;
JButton btnCalculate;
public GUI_Calculate(){
txtNum = new JTextField(5);
txtSum = new JTextField(30);
txtProduct = new JTextField(30);
btnCalculate = new JButton("计算");
this.setLayout(new FlowLayout());
this.add(txtNum);
this.add(btnCalculate);
this.add(txtSum);
this.add(txtProduct);
btnCalculate.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
}
public void init(){
new GUI_Calculate();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("计算")){
int num;
if(txtNum.getText().equals("")){
JOptionPane.showMessageDialog(this, "必须输入一个数字");
}
try{
num = Integer.parseInt(txtNum.getText());
txtSum.setText(doSum(num));
txtProduct.setText(doProduct(num));
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "必须输入一个数字");
}
}
}
public String doProduct(int num){
String ret = "";
int i;
long product = 1;
if(num == 0 || num == 1){
return num + "=" + num;
}
for(i=1; i<num; i++){
ret += i + "*";
product *= i;
}
product *= num;
ret += num + "=" + product;
return ret;
}
public String doSum(int num){
String ret = "";
int i, sum = 0;
if(num == 0 || num == 1){
return num + "=" + num;
}
for(i=1; i<num; i++){
ret += i + "+";
sum += i;
}
sum += num;
ret += num + "=" + sum;
return ret;
}
}
界面效果:
热心网友 时间:1天前
哎呀,没抢到呀,楼上真速度热心网友 时间:1天前
热心网友 时间:1天前
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI_Calculate extends JApplet implements ActionListener{
JTextField txtNum, txtSum, txtProduct;
JButton btnCalculate;
public GUI_Calculate(){
txtNum = new JTextField(5);
txtSum = new JTextField(30);
txtProduct = new JTextField(30);
btnCalculate = new JButton("计算");
this.setLayout(new FlowLayout());
this.add(txtNum);
this.add(btnCalculate);
this.add(txtSum);
this.add(txtProduct);
btnCalculate.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
}
public void init(){
new GUI_Calculate();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn.getText().equals("计算")){
int num;
if(txtNum.getText().equals("")){
JOptionPane.showMessageDialog(this, "必须输入一个数字");
}
try{
num = Integer.parseInt(txtNum.getText());
txtSum.setText(doSum(num));
txtProduct.setText(doProduct(num));
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "必须输入一个数字");
}
}
}
public String doProduct(int num){
String ret = "";
int i;
long product = 1;
if(num == 0 || num == 1){
return num + "=" + num;
}
for(i=1; i<num; i++){
ret += i + "*";
product *= i;
}
product *= num;
ret += num + "=" + product;
return ret;
}
public String doSum(int num){
String ret = "";
int i, sum = 0;
if(num == 0 || num == 1){
return num + "=" + num;
}
for(i=1; i<num; i++){
ret += i + "+";
sum += i;
}
sum += num;
ret += num + "=" + sum;
return ret;
}
}
界面效果:
热心网友 时间:1天前
哎呀,没抢到呀,楼上真速度热心网友 时间:1天前