在 Java 中,如何用Swing實現(xiàn)打字游戲.需要掌握多線程,JFrame的創(chuàng)建方法。多線程的創(chuàng)建方法,一個是繼承Thread類,一個是實現(xiàn)Runnable接口??蚣艿膶崿F(xiàn),直接創(chuàng)建一個類繼承JFrame.
基本思路是,1.創(chuàng)建一個JFrame 2.啟動十個線程,每個線程攜帶了一個JLabel,用到了隨機(jī)函數(shù),每個JLabel的剛出現(xiàn)的時候,他們的y坐標(biāo)都是一樣的,x坐標(biāo)是隨機(jī)的(隨機(jī)范圍在JFrame的寬度內(nèi).JLabel顯示的值,是Ascii對應(yīng)的拉丁字母 .3.線程控制每個JLael的y坐標(biāo)增加,這樣字母就在往下掉 4.響應(yīng)鍵盤事件
//載入包
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
// 創(chuàng)建一個線程
class MyThread extends Thread {
int height=0;
// JLabel的x坐標(biāo)
int intx=0;
// JLabel 的y坐標(biāo)
int inty=0;
static int com=0;
JLabel lb1;
public MyThread(ThreadGroup g,String name,int width,int fheight){
this.setName(name);
height=fheight;
// 設(shè)置JLabel顯示的大寫字母,通過隨機(jī)值控制.65是大寫A的Ascii值
lb1=new JLabel(“”+(char)(int)(Math.random()*26+65));
// 設(shè)置JLabel的背景顏色,紅,綠,藍(lán)組成自然界所有色彩
lb1.setForeground(new Color((int)(Math.random()*250),(int)(Math.random()*250),(int)(Math.random()*250)));
intx=(int)(Math.random()*(width-20));
//線程啟動
start();
}
public void run(){
try{
while(true){
//睡眠50毫秒往下掉
sleep((int)(Math.random()*50));
lb1.setBounds(intx,inty,15,15);
//改變y坐標(biāo)
inty++;
if(inty>=(height-20)){
lb1.show(false);
MyThread.com++;
break;
}
}
}catch(Exception e){}
}
}
//創(chuàng)建窗口
class TestKey extends JFrame
{
JButton b1,b2;
static int index=0;
Container conent=getContentPane();
static int idd=0;
static int count=0;
int ixx;
static ThreadGroup dg = new ThreadGroup(“group1”);
static ThreadGroup dg1;
static MyThread t1;
Thread list[];
public TestKey() throws ArrayStoreException
{
setSize(400,600);
b1=new JButton(“開始”);
b2=new JButton(“結(jié)束”);
conent.setLayout(null);
b1.setBounds(getWidth()/2-100,getHeight()-70,80,30);
b2.setBounds(getWidth()/2+50,getHeight()-70,80,30);
conent.add(b1);
conent.add(b2);
conent.setBackground(Color.blue);
Container cont=getContentPane();
conent.setBackground(Color.white);
show();
//響應(yīng)按鈕事件
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent even)
{
for(int i=0;i<10;i++)
{
t1 =new MyThread(dg,”t”+i,getWidth(),getHeight());
getContentPane().add(t1.lb1);
}
}
});
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent even)
{
int num=conent.getComponentCount()-2;
for(int i=2;i
{
conent.remove(2);
conent.repaint();
}
if((float)num/count<=0.02)
JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”只有”+num+”個未打掉!n您真是高手,佩服,佩服!”);
else if((float)num/count<=0.15)
JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n水平不錯,繼續(xù)努力吧!”);
else if((float)num/count<=0.3)
JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n您很有潛力,不要虛度光陰哦!”);
else if((float)num/count<=0.4)
JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n挺危險的,加把勁啊!”);
else
JOptionPane.showMessageDialog(null,”共有”+count+”個字母n”+”有”+num+”個未打掉!n不蠻你,你真的很差勁,抓緊練習(xí)吧!”);
}
});
//監(jiān)聽窗口事件
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
if(conent.getComponentCount()==2)
JOptionPane.showMessageDialog(null,”您真的要退出嗎?n記得經(jīng)常來練習(xí)啊!”);
else
JOptionPane.showMessageDialog(null,”您確定要退出嗎?n我還沒為您評分的啊!”);
System.exit(0);
}
});
b1.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent event)
{
list = new Thread[dg.getParent().activeCount()];
int count = dg.getParent().enumerate(list);
for(int i=0;i
{
try
{
MyThread mt=null;
mt = (MyThread)(list[i]);// instanceof MyThread)
if(event.getKeyChar()== mt.lb1.getText().charAt(0))
{
mt.lb1.setVisible(false);
MyThread hh=new MyThread(dg,”t”+i,getWidth(),getHeight());
getContentPane().add(hh.lb1);
}
}catch(Exception e){}
}
}
});
}
public static void main(String args[]){
TestKey mf=new TestKey();
Container cont=mf.getContentPane();
cont.setBackground(Color.white);
cont.setLayout(null);
//創(chuàng)建十個線程,攜帶十個字母往下掉
for(int i=0;i<10;i++)
{
t1 =new MyThread(dg,”t”+i,mf.getWidth(),mf.getHeight());
cont.add(t1.lb1);
}
}
}

大家有不同的實現(xiàn)方式,歡迎在下面留言
本文由網(wǎng)上采集發(fā)布,不代表我們立場,轉(zhuǎn)載聯(lián)系作者并注明出處:http://m.zmlzfb.cn/shbk/39367.html