请Java大神看看,帮忙改下这个程序的(得分)部分,看能不能加个排行榜和暂停功能
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTypeExeciseext...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TypeExecise extends JFrame implements Runnable,KeyListener,ActionListener{
JMenuBar jmb;
JMenu jm;
JMenuItem item1,item2,item3;
int score=0;
Thread[] letter=new Thread[11];
Letter[] label=new Letter[11];
public TypeExecise() {
super("打字窗口");
setVisible(true); setBounds(100,100,500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
jmb=new JMenuBar();
jm=new JMenu("菜单");
item1=new JMenuItem("开始");
item3=new JMenuItem("得分");
item1.addActionListener(this);item3.addActionListener(this);item2=new JMenuItem("结束");item2.addActionListener(this);jm.add(item1);jm.add(item3);jm.add(item2);jmb.add(jm);add(jmb);jmb.setBounds(0,0,500,20);validate();}public void actionPerformed(ActionEvent ae){
if(ae.getSource()==item1)
{for(int i=1;i<=10;i++){label[i]= new Letter(this,i);letter[i]=new Thread(label[i]);letter[i].start();}new Thread(this).start();requestFocus();addKeyListener(this);}else if(ae.getSource()==item2){int n=JOptionPane.showConfirmDialog(this,"确认退出吗?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
System.exit(0);
}
else if(ae.getSource()==item3)
{JOptionPane.showMessageDialog(null,"最终得分为:"+score,"得分",JOptionPane.WARNING_MESSAGE);
int a=0;
score=a;
}}
public void run()
{while(true)
for (int i=1;i<=10;i++)
if(label[i].y>=300)
{letter[i].interrupt();
label[i]= new Letter(this,i);
letter[i]=new Thread(label[i]);
letter[i].start();}
}
public void keyPressed(KeyEvent ke){if(ke.getModifiers()==InputEvent.CTRL_MASK&&ke.getKeyCode()==KeyEvent.VK_X)
JOptionPane.showMessageDialog(null,"最终得分为:"+score,"得分",JOptionPane.WARNING_MESSAGE);
int a=0;
score=a;}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke)
{char key=ke.getKeyChar();
for (int i=1;i<=10;i++)
if(label[i].alpha==key )
{ label[i].setVisible(false);
letter[i].interrupt();
score+=10;
label[i]= new Letter(this,i);
letter[i]=new Thread(label[i]);
letter[i].start();}
}
public static void main(String[] args) {
new TypeExecise();
}}
class Letter extends JLabel implements Runnable
{int order,x,y=0;
char alpha;
TypeExecise te;
int step;
Letter(TypeExecise t,int ord){
order=ord; x=order*50; alpha=(char)((int )'a'+(int)(Math.random()*26));
setText(""+alpha); te=t;
te.add(this);
setBounds(x,0,20,20);
while((step=(int)(Math.random()*5))==0);
}
public void run(){ while(true){setLocation(x,y=y+step);try{Thread.sleep(50);
}catch(InterruptedException ie)
{return;}
}
}} 展开
import java.awt.*;
import java.awt.event.*;
public class TypeExecise extends JFrame implements Runnable,KeyListener,ActionListener{
JMenuBar jmb;
JMenu jm;
JMenuItem item1,item2,item3;
int score=0;
Thread[] letter=new Thread[11];
Letter[] label=new Letter[11];
public TypeExecise() {
super("打字窗口");
setVisible(true); setBounds(100,100,500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
jmb=new JMenuBar();
jm=new JMenu("菜单");
item1=new JMenuItem("开始");
item3=new JMenuItem("得分");
item1.addActionListener(this);item3.addActionListener(this);item2=new JMenuItem("结束");item2.addActionListener(this);jm.add(item1);jm.add(item3);jm.add(item2);jmb.add(jm);add(jmb);jmb.setBounds(0,0,500,20);validate();}public void actionPerformed(ActionEvent ae){
if(ae.getSource()==item1)
{for(int i=1;i<=10;i++){label[i]= new Letter(this,i);letter[i]=new Thread(label[i]);letter[i].start();}new Thread(this).start();requestFocus();addKeyListener(this);}else if(ae.getSource()==item2){int n=JOptionPane.showConfirmDialog(this,"确认退出吗?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
System.exit(0);
}
else if(ae.getSource()==item3)
{JOptionPane.showMessageDialog(null,"最终得分为:"+score,"得分",JOptionPane.WARNING_MESSAGE);
int a=0;
score=a;
}}
public void run()
{while(true)
for (int i=1;i<=10;i++)
if(label[i].y>=300)
{letter[i].interrupt();
label[i]= new Letter(this,i);
letter[i]=new Thread(label[i]);
letter[i].start();}
}
public void keyPressed(KeyEvent ke){if(ke.getModifiers()==InputEvent.CTRL_MASK&&ke.getKeyCode()==KeyEvent.VK_X)
JOptionPane.showMessageDialog(null,"最终得分为:"+score,"得分",JOptionPane.WARNING_MESSAGE);
int a=0;
score=a;}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke)
{char key=ke.getKeyChar();
for (int i=1;i<=10;i++)
if(label[i].alpha==key )
{ label[i].setVisible(false);
letter[i].interrupt();
score+=10;
label[i]= new Letter(this,i);
letter[i]=new Thread(label[i]);
letter[i].start();}
}
public static void main(String[] args) {
new TypeExecise();
}}
class Letter extends JLabel implements Runnable
{int order,x,y=0;
char alpha;
TypeExecise te;
int step;
Letter(TypeExecise t,int ord){
order=ord; x=order*50; alpha=(char)((int )'a'+(int)(Math.random()*26));
setText(""+alpha); te=t;
te.add(this);
setBounds(x,0,20,20);
while((step=(int)(Math.random()*5))==0);
}
public void run(){ while(true){setLocation(x,y=y+step);try{Thread.sleep(50);
}catch(InterruptedException ie)
{return;}
}
}} 展开
展开全部
做排行榜的话,你加一个xml(HighScore=0)文件,每次得分后把得到的分数去跟xml中HighScore比较,如果大于的话,就修xml文件,排行榜就这样。
暂停就把线程挂起
暂停就把线程挂起
追问
这个程序的得分有问题,你能帮忙改下么?
追答
我帮你处理了一下得分问题
1.在Letter 里添加了一条 static int score;
2.这个方法修改了 public void keyTyped(KeyEvent ke){
char key=ke.getKeyChar();
for (int i=1;i<=10;i++){
if(label[i].alpha==key )
{
label[i].score+=10;
label[i].setVisible(false);
letter[i].interrupt();
label[i]= new Letter(this,i);
letter[i]=new Thread(label[i]);
letter[i].start();
}
}
}
3.添加了一个方法
public int lastScore(){
score=label[0].score;
return score;
}
4.最终得分时修改了
else if(ae.getSource()==item3)
{
JOptionPane.showMessageDialog(null,"最终得分为:"+lastScore(),"得分",JOptionPane.WARNING_MESSAGE);
int a=0;
label[0].score=a;
}我试了还行好使,你试试看看
展开全部
给你思路吧,加一个暂停按钮,监听,动作执行Thread.yield()方法,暂停当前线程并执行其他线程。
至于排行榜,可以使用文件,将每次得分记录到文件(采用追加的方式记录,否则每次都会覆盖上一次的得分),程序运行的时候,从文件读取数据到一个集合中,对这个集合排序,并显示出来,就是排行榜
至于排行榜,可以使用文件,将每次得分记录到文件(采用追加的方式记录,否则每次都会覆盖上一次的得分),程序运行的时候,从文件读取数据到一个集合中,对这个集合排序,并显示出来,就是排行榜
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询