?? exam10_7.java
字號:
/*這是一個輸入速度計數的測試程序示例
*程序名為: Exam10_7.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exam10_7 extends JFrame implements Runnable,ActionListener
{
JLabel time=new JLabel("90");
int time_count=90;
int answer_count=0;
JTextField no = new JTextField(10);
JTextField result = new JTextField(10);
JButton next=new JButton("下一個");
JButton start_end=new JButton("開始");
static Thread thread1;
public Exam10_7()
{
Container pane=getContentPane(); //獲得擺放組件的窗口容器
pane.setLayout(new GridLayout(0,2)); //在容器上以網格布局擺放組件
pane.add(new JLabel("總的測試時間90分鐘,剩余時間:"));
pane.add(time);
pane.add(new JLabel("學號")); //將組件擺放到窗格上
pane.add(no); //......
pane.add(new JLabel("成績")); //......
pane.add(result); //......
pane.add(start_end); //......
pane.add(next); //......
start_end.addActionListener(this); //注冊exit按鈕的監聽對象
next.addActionListener(this); //注冊next按鈕的監聽對象
next.setEnabled(false); //設置組件不可操作
no.setEditable(false); //設置組件不可操作
result.setEditable(false); //設置組件不可操作
setSize(400,120); //設置窗口的大小
setVisible(true);
setDefaultCloseOperation(3);
}
/************main()*************/
public static void main(String [] args)
{
thread1=new Thread(new Exam10_7());
}
/*********ActionListener接口方法*************/
public void actionPerformed(ActionEvent e) //實現單擊按鈕事件
{
Object obj=e.getSource(); //獲取事件源
if(obj==next) //設置下一個
{
answer_count++;
no.setText("");
result.setText("");
no.requestFocus(); //定位輸入位置
}
else if(obj==start_end) //開始或退出
{
if(time_count>0) //開始
{
no.setEditable(true); //設置組件可操作
result.setEditable(true); //設置組件可操作
next.setEnabled(true); //設置組件可操作
start_end.setEnabled(false); //設置組件不可操作
thread1.setDaemon(true); //設置線程為守護線程
thread1.start(); //啟動線程
}
else System.exit(0); //結束程序
}
}
/*********Runnable接口方法*************/
public void run()
{
try
{
while(true)
{
time.setText(time_count+""); //顯示倒計時
time_count--;
if(time_count<0) //測試時間到
{
next.setEnabled(false); //設置組件不可操作
no.setEditable(false); //設置組件不可操作
result.setEditable(false); //設置組件不可操作
start_end.setEnabled(true); //設置組件可操作
start_end.setText("結束");
time.setText("時間到,輸入"+answer_count+"個");
thread1.interrupt(); //中斷線程的運行
}
thread1.sleep(600L); //休眠1分鐘
}
}
catch(InterruptedException e) { System.out.println(e.toString()); }
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -