房天下 > 房天下问答 > 业主生活 > 其他
  • 编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。

    编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。单击相应的按钮,两个文本框的数字做算术运算,在第3个文本框中显示结果。要求处理NumberFormatException。本题是Java小程序,题来自JAVA 2 实用教程(第二版) 清华大学出版社。130页 第十章 习题3急需这道题的完整代码。能运行出结果的!

    提问者:liaghao_Lu

    发布于2010-12-18

共2个回答
  • 伟德国际 丨Lv 4
    哈哈 我昨天刚做了这个作业 你够幸运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); }}
    +14 2010-12-19 举报
  • 搜房网友 丨Lv 10
     求一个用c++编写的小应用程序
    +1 2011-12-29 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。