在 Java 中,如何用Swing實(shí)現(xiàn)打字游戲.需要掌握多線程,JFrame的創(chuàng)建方法。多線程的創(chuàng)建方法,一個(gè)是繼承Thread類(lèi),一個(gè)是實(shí)現(xiàn)Runnable接口。框架的實(shí)現(xiàn),直接創(chuàng)建一個(gè)類(lèi)繼承JFrame.
基本思路是,1.創(chuàng)建一個(gè)JFrame 2.啟動(dòng)十個(gè)線程,每個(gè)線程攜帶了一個(gè)JLabel,用到了隨機(jī)函數(shù),每個(gè)JLabel的剛出現(xiàn)的時(shí)候,他們的y坐標(biāo)都是一樣的,x坐標(biāo)是隨機(jī)的(隨機(jī)范圍在JFrame的寬度內(nèi).JLabel顯示的值,是Ascii對(duì)應(yīng)的拉丁字母 .3.線程控制每個(gè)JLael的y坐標(biāo)增加,這樣字母就在往下掉 4.響應(yīng)鍵盤(pán)事件
//載入包
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
// 創(chuàng)建一個(gè)線程
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顯示的大寫(xiě)字母,通過(guò)隨機(jī)值控制.65是大寫(xiě)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));
//線程啟動(dòng)
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(“開(kāi)始”);
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+”個(gè)字母n”+”只有”+num+”個(gè)未打掉!n您真是高手,佩服,佩服!”);
else if((float)num/count<=0.15)
JOptionPane.showMessageDialog(null,”共有”+count+”個(gè)字母n”+”有”+num+”個(gè)未打掉!n水平不錯(cuò),繼續(xù)努力吧!”);
else if((float)num/count<=0.3)
JOptionPane.showMessageDialog(null,”共有”+count+”個(gè)字母n”+”有”+num+”個(gè)未打掉!n您很有潛力,不要虛度光陰哦!”);
else if((float)num/count<=0.4)
JOptionPane.showMessageDialog(null,”共有”+count+”個(gè)字母n”+”有”+num+”個(gè)未打掉!n挺危險(xiǎn)的,加把勁啊!”);
else
JOptionPane.showMessageDialog(null,”共有”+count+”個(gè)字母n”+”有”+num+”個(gè)未打掉!n不蠻你,你真的很差勁,抓緊練習(xí)吧!”);
}
});
//監(jiān)聽(tīng)窗口事件
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
if(conent.getComponentCount()==2)
JOptionPane.showMessageDialog(null,”您真的要退出嗎?n記得經(jīng)常來(lái)練習(xí)啊!”);
else
JOptionPane.showMessageDialog(null,”您確定要退出嗎?n我還沒(méi)為您評(píng)分的啊!”);
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)建十個(gè)線程,攜帶十個(gè)字母往下掉
for(int i=0;i<10;i++)
{
t1 =new MyThread(dg,”t”+i,mf.getWidth(),mf.getHeight());
cont.add(t1.lb1);
}
}
}

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