hubmygirl
发贴: 7
积分: 0
|
于 2007-09-12 00:12
package tarema.com.cn;
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.Applet;
public class Test_Awt extends JApplet implements ActionListener {
int m = 0, n = 0, inputNumber, value;
JLabel inputLabel; JTextField inputField; JButton beginButton;
private boolean flag = false;
public void init() { Container container = getContentPane(); container.setLayout(new FlowLayout());
inputLabel = new JLabel("Enter a key "); container.add(inputLabel);
inputField = new JTextField(10);
container.add(inputField); beginButton = new JButton("Begin"); container.add(beginButton); beginButton.addActionListener(this); }
public void actionPerformed(ActionEvent e) {
// if ( e.getActionCommand().equals()) // System.out.println ( e.getActionCommand() ); if ( inputField.getText().equals("")&& flag == false ){ flag = true ; value = product(); displayMessage(); return ; } // System.out.println ( value ); inputNumber = Integer.parseInt(inputField.getText()); System.out.println ( inputNumber ); if (value == inputNumber){ flag = false; inputField.setText(""); showStatus("good"); } else{ showStatus("Try again"); inputField.setText(""); flag = false; } }
public int product() { m = 1 + (int) (Math.random() * 9); n = 1 + (int) (Math.random() * 9); return m * n; }
public void displayMessage() { showStatus(m + " * " + n + " = ?"); } public static void main ( String [] args) { new Test_Awt().init(); } } //呵呵 ,你看看,你的程序会有异常,最简单的方法就是跳过此异常, 在出现之前返回!
|