Java_【第一个小游戏-单词拼写字母排序】

Game.java

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


import static javax.swing.JFrame.*;
import java.awt.*;
public class Game extends JFrame implements KeyListener,FocusListener{
	JPanel jp1 = new JPanel();//定义三个容器
	JPanel jp2 = new JPanel();
	JPanel jp3 = new JPanel();
	JLabel JL = new JLabel("用鼠标点击字母,按左右键交换位置");//  
	//JButton buts;//定义一个按钮
	JButton[] buts;//定义一个按钮数组
	
	
	JLabel result = new JLabel();//标签
	
	String s= "xujiping";
	String fanyi = "徐吉平";
	
	
	public Game(){
		this.setBounds(300,200,500,200);//窗口大小
		this.setLayout(new GridLayout(3,1));
		
		jp1.add(JL);//把初始提示的标签加入到界面中
		result.setText("提示:"+fanyi);//标签添加内容
		result.setForeground(Color.green);//设置颜色
		jp3.add(result);//
		
		buts = new JButton[s.length()];
		
		RandomString random = new RandomString();
		String str = random.getRandomString(s);
		for(int i=0;i


RandomString.java

public class RandomString {
	
	public String getRandomString(String s){
		String str="";//打乱后的结果
		StringBuffer sbf = new StringBuffer(s);
		while(sbf.length()>0){
			//得到一个从0到字符串长度的随机数
			int index = (int)(Math.random()*sbf.length());
			char c = sbf.charAt(index);
			str+=c;
			sbf = sbf.deleteCharAt(index);
			
		}
		return str;
	}
	
}




 

你可能感兴趣的:(数据结构-学习博文,java,游戏)