哈哈 我昨天刚做了这个作业 你够幸运import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.math.*;public class Homework10_3 { public static void main(String args[]){ MathWindow win=new MathWindow() 展开
哈哈 我昨天刚做了这个作业 你够幸运import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.math.*;public class Homework10_3 { public static void main(String args[]){ MathWindow win=new MathWindow();}}class MathWindow extends JFrame{ JTextField text1,text2,text3;JPanel ps,pn;MathWindow(){ text1=new JTextField(10);text2=new JTextField(10);text3=new JTextField(10);JButton button1,button2,button3,button4;button1=new JButton("加");button2=new JButton("减");button3=new JButton("乘");button4=new JButton("除");ps=new JPanel();pn=new JPanel();pn.add(text1);pn.add(text2);pn.add(text3);ps.add(button1);ps.add(button2);ps.add(button3);ps.add(button4);add(pn,BorderLayout.CENTER);add(ps,BorderLayout.SOUTH);setBounds(100,100,370,150);setVisible(true);validate();button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s1=text1.getText();String s2=text2.getText();try{ BigInteger n1=new BigInteger(s1);BigInteger n2=new BigInteger(s2);n2=n1.add(n2);text3.setText(n2.toString());} catch(NumberFormatException ee){ text3.setText("请输入数字字符");text1.setText(null);text2.setText(null);} } });button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s1=text1.getText();String s2=text2.getText();try{ BigInteger n1=new BigInteger(s1);BigInteger n2=new BigInteger(s2);n2=n1.subtract(n2);text3.setText(n2.toString());} catch(NumberFormatException ee){ text3.setText("请输入数字字符");text1.setText(null);text2.setText(null);} } });button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s1=text1.getText();String s2=text2.getText();try{ BigInteger n1=new BigInteger(s1);BigInteger n2=new BigInteger(s2);n2=n1.multiply(n2);text3.setText(n2.toString());} catch(NumberFormatException ee){ text3.setText("请输入数字字符");text1.setText(null);text2.setText(null);} } });button4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String s1=text1.getText();String s2=text2.getText();try{ BigInteger n1=new BigInteger(s1);BigInteger n2=new BigInteger(s2);if(n2.toString()=="0"){ text3.setText("除数不能为0");} else { n2=n1.divide(n2);text3.setText(n2.toString());} } catch(NumberFormatException ee){ text3.setText("请输入数字字符");text1.setText(null);text2.setText(null);} } });setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}} 收起