课后作业之鼠标点击速度比赛游戏
题目:
利用Java Swing 技术设计一个鼠标点击速度比费游戏程序。程序显示一个按钮和文本框。用户点击按钮,文本框显示鼠标点击次数。两个人同时运行本程序,即进行比赛。程序运行效果如图4-6 所示。
源代码:
package cn.whpu.action;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game {
public static void main(String[] args) {
new MyFrame(" 鼠标点击大比拼");
}
}
@SuppressWarnings("serial")
class MyFrame extends JFrame implements ActionListener{
JLabel explainLabel=new JLabel (" 点击次数");
JTextField text=new JTextField(" ");
int count=0;
JButton button=new JButton("请连续点击");
public MyFrame(String title) {
setTitle(title);
setLayout(new FlowLayout());
add(explainLabel);
add(text);
button.addActionListener(this);
add(button);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
count++;
text.setText(" "+count+" ");
}
}