package dao; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class E12 { public static void main(String[] args) { WindowLogin1 win = new WindowLogin1("四则运算练习器"); } } class WindowLogin1 extends JFrame implements ActionListener{ JButton name,name2; JTextField text1,text2; int a,b,fuhao; WindowLogin1(String s){ super(s); addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { dispose(); } } ); setLayout(null); Container con=getContentPane(); con.setBackground(Color.yellow); setBounds(0,0,450,350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); name2=new JButton("确认答案"); name=new JButton("获得题目"); text1=new JTextField(); text2=new JTextField(); name.setBounds(10,20,100,20); name2.setBounds(10,50,100,20); text1.setBounds(120,20,160,20); text2.setBounds(120,50,160,20); add(name); add(name2); add(text1); add(text2); name2.addActionListener(this); name.addActionListener(this); setVisible(true); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==name) { a=(int)(Math.random()*100)+1; b=(int)(Math.random()*100)+1; fuhao=(int)(Math.random()*4)+1; if(fuhao==1) {text1.setText(a+"+"+b);} if(fuhao==2) {text1.setText(a+"-"+b);} if(fuhao==3) {text1.setText(a+"×"+b);} if(fuhao==4) {text1.setText(a+"÷"+b);} } else if(e.getSource()==name2) { int guess=0; guess=Integer.parseInt(text2.getText()); if(guess==a+b&&fuhao==1) { JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else if(guess==a-b&&fuhao==2) { JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else if(guess==a*b&&fuhao==3) { JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else if(guess==a/b&&fuhao==4) { JOptionPane.showMessageDialog(this,"计算正确","消息对话框",JOptionPane.INFORMATION_MESSAGE); } else{ JOptionPane.showMessageDialog(this,"计算错误","消息对话框",JOptionPane.WARNING_MESSAGE); text2.setText(null); } } } }