java课程设计——猜数游戏
1.团队课程设计博客链接
http://www.cnblogs.com/springbreezemiles/p/7064135.html
2.个人负责模块或任务说明
本人任务: 编写主界面以及排行榜代码,玩家信息的储存,排行榜界面设计。
3.自己的代码提交记录截图
4.自己负责模块或任务详细说明
界面设计:
排行榜部分代码:
按照玩家的猜对次数进行排名,次数越少排名越高
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import javax.swing.JFrame;
/**
*
* @author spring
*/
public class Rank extends javax.swing.JFrame {
private ArrayList<User> arrayList;
/**
* Creates new form paihang
* @throws FileNotFoundException
*/
public Rank() throws FileNotFoundException {
initComponents();
arrayList = new ArrayList<>();
File datafile = new File("D:\\rankdata.txt");
Scanner scanner = new Scanner(new FileInputStream(datafile));
while (scanner.hasNextLine()) {
String string = scanner.nextLine();
String[] strings = string.split(" ");
String name = strings[0];
int count = Integer.parseInt(strings[1]);
User user = new User(name, count);
arrayList.add(user);
}
Collections.sort(arrayList, new UserComparator());
for (int i = 0; i < arrayList.size(); i++) {
String username = arrayList.get(i).getUsername();
String count = arrayList.get(i).getCount() + "";
if (i == 0) {
jLabel5.setText(username);
jLabel10.setText(count);
} else if (i == 1) {
jLabel8.setText(username);
jLabel11.setText(count);
} else if (i == 2) {
jLabel7.setText(username);
jLabel12.setText(count);
} else {
break;
}
}
scanner.close();
}
玩家输入姓名界面
import java.io.BufferedWriter;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
/**
*
* @author spring
*/
public class Username extends javax.swing.JFrame {
/**
* Creates new form usernanme
*/
private int count;
public Username(int count) {
this.count = count;
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
y = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("答对了,请输入你的姓名");
y.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
yActionPerformed(evt);
}
});
jButton1.setText("确定");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
jButton1ActionPerformed(evt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(y, javax.swing.GroupLayout.PREFERRED_SIZE, 210,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 177,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(146, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1).addGap(43, 43, 43)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(y, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1)
.addContainerGap(15, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void yActionPerformed(java.awt.event.ActionEvent evt) {
username = y.getText();
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
// TODO add your handling code here:
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\rankdata.txt", true));
String name = y.getText();
User user = new User(name, count);
bufferedWriter.write(user.toString());
bufferedWriter.close();
dispose();
}
private String username;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField y;
// End of variables declaration
}
5.课程设计感想
作为组长,不仅仅是负责好自己的模块,更重要的是团队的协调合作。
经过这次课设我对我自己的能力有了深刻的认识,各类代码的应用还不熟练,要能在将来继续认真的学习代码,提高自身水平。