谁能帮我用java编写一个程序啊??

2024-11-23 21:40:15
推荐回答(1个)
回答1:

package com.zhh.test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JB extends JFrame implements ActionListener
{
private JTextField Input1,Input2,Input3;
private JButton btnOK;
private static final String OK="计算";
private JTextField ShowAnswer;

public JB(String strTitle)
{
super(strTitle);
Container c=getContentPane();
c.setLayout(new FlowLayout());
Input1=new JTextField(4);
Input2=new JTextField(4);
Input3=new JTextField(4);
ShowAnswer=new JTextField(4);
btnOK=new JButton(OK);
c.add(new JLabel("请输入待计算的两个数据:"));
c.add(Input1);
c.add(Input2);
c.add(new JLabel("请输入计算符(1 for + ,2 for - ,3 for * ,4 for / ):"));
c.add(Input3);
c.add(new JLabel("计算结果:"));
c.add(ShowAnswer);
c.add(btnOK);
btnOK.addActionListener(this);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setSize(310,100);
this.setVisible(true);
}
public static void main(String[] args)
{
new JB("JButtonTest Testing!!!");

}
public void actionPerformed(ActionEvent e){
int num4 = 0;
int num1=Integer.parseInt(Input1.getText());
int num2=Integer.parseInt(Input2.getText());
int num3=Integer.parseInt(Input3.getText());
if(num3==1){ num4=num1+num2 ;}
if(num3==2){ num4=num1-num2 ;}
if(num3==3){ num4=num1*num2 ;}
if(num3==4){ num4=num1/num2 ;}

ShowAnswer.setText(String.valueOf(num4));
}
}