作业5 四则运算 测试与封装 5.2
编译环境:Eclipse
开发人员:钟鹏升
开发时间:2015-05-8
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.*; import java.math.BigDecimal; import java.util.Random; import java.awt.EventQueue; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class SZYS extends JFrame { int a; int b; int op; String text1,text2,text3; int num1,num2,num3; int result; private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SZYS frame = new SZYS(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SZYS() { String Operator = null; getContentPane().setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 250); setLocationRelativeTo(null); setResizable(false); setTitle("四则运算"); JLabel lable1=new JLabel("四则运算"); lable1.setFont(new Font("宋体", Font.PLAIN, 24)); lable1.setBounds(170, 15, 96, 30); getContentPane().add(lable1); JTextField textField1 = new JTextField(); textField1.setBounds(50, 65, 40, 30); getContentPane().add(textField1); JTextField textField2 = new JTextField(); textField2.setBounds(160, 65, 40, 30); getContentPane().add(textField2); JLabel lable3=new JLabel("="); lable3.setFont(new Font("宋体", Font.PLAIN, 24)); lable3.setBounds(230, 65, 40, 30); getContentPane().add(lable3); JTextField textField3 = new JTextField(); textField3.setBounds(290, 65, 40, 30); getContentPane().add(textField3); JButton Button1 = new JButton("确定"); Button1.setBounds(350, 65, 60, 30); getContentPane().add(Button1); JButton Button2 = new JButton("再做一次"); Button2.setBounds(170, 180, 90, 30); getContentPane().add(Button2); int op=new Random().nextInt(4); switch(op) { case 0: Operator=new String("+"); break; case 1: Operator=new String("-"); break; case 2: Operator=new String("×"); break; case 3: Operator=new String("÷"); break; } JLabel lable2=new JLabel(Operator); lable2.setFont(new Font("宋体", Font.PLAIN, 24)); lable2.setBounds(110, 65, 40, 30); getContentPane().add(lable2); Button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { text1=textField1.getText(); text2=textField2.getText(); text3=textField3.getText(); num1=Integer.parseInt(text1); num2=Integer.parseInt(text2); num3=Integer.parseInt(text3); if(op==0) { result=num1+num2; } if(op==1) { result=num1-num2; } if(op==2) { result=num1*num2; } if(op==3) { result=num1/num2; } if(num3==result) { JOptionPane.showMessageDialog(null, "恭喜你!答对了!","提示",JOptionPane.ERROR_MESSAGE); } else JOptionPane.showMessageDialog(null, "答错了!答案是"+result+"!","提示",JOptionPane.ERROR_MESSAGE); } }); // JTextArea textbox = new JTextArea(200,20); // textbox.setBounds(120, 110, 200, 60); // textbox.setText("恭喜你!\n答对了!"); // textbox.setEditable(false); // getContentPane().add(textbox); } }
重新写了一遍代码,还没写完,仍待完善,未加封装等。