模拟答题

来源:互联网 发布:《深入浅出数据分析》 编辑:程序博客网 时间:2024/04/29 18:30

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Teacher{
 public static void main(String[] args){
  ComputerFrame cf = new ComputerFrame();
  cf.setVisible(true);
  cf.setBounds(100, 100, 660, 130);
  cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

}

class ComputerFrame extends JFrame {
 JButton j1 = new JButton("获取题目");
 JButton j2 = new JButton("确认答案");
 JTextField t1 = new JTextField(15);
 JTextField t2 = new JTextField(15);
 JTextField t3 = new JTextField(15);
 JLabel l1 = new JLabel("+");
 JLabel l2 = new JLabel("=");
 JLabel l3 = new JLabel("你还没有回答那");
 JPanel p1 = new JPanel(new FlowLayout());
 JPanel p2 = new JPanel(new FlowLayout());
 public ComputerFrame(){
  p2.add(j1);
  p2.add(t1);
  p2.add(l1);
  p2.add(t2);
  p2.add(l2);
  p2.add(t3);
     p1.add(j2);
     p1.add(l3);
  p2.add(p1);
  add(p2,BorderLayout.CENTER);
  p2.setBackground(Color.red);
  p1.setBackground(Color.green);
  
  j1.addActionListener(new ButtonListener());
  j2.addActionListener(new ButtonListener());
  
 }
 
 class ButtonListener implements ActionListener {

  int Result = 0;
  double d = Math.random() * 30;
  public void actionPerformed(ActionEvent arg0) {
   // TODO Auto-generated method stub
   if(((JButton)arg0.getSource()) == j1) {
    t1.setText(String.valueOf(d));
    t2.setText(String.valueOf(d));
    if((Math.random() * 20) > 100){
     l1.setText("-");
     
    }
    else if((Math.random() * 20) <= 0){
     l1.setText("*");
    }
   }
   if(((JButton)arg0.getSource()) == j2){
    if(t3.getText() == String.valueOf(Result)){
     l3.setText("回答正确!");
    }
    else {
     l3.setText("回答错误!");
    }
   }
  }
  
 }
 
}

原创粉丝点击