0$房天下问答|编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。$https://m.fang.com/ask/ask_1494585.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=1494585
共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); }}
-
-
-
搜房网友
丨Lv 10
求一个用c++编写的小应用程序
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功