http://www.cnblogs.com/springbreezemiles/p/7064135.htmlhtml
本人任務: 編寫主界面以及排行榜代碼,玩家信息的儲存,排行榜界面設計。java
界面設計:
spring
排行榜部分代碼:
按照玩家的猜對次數進行排名,次數越少排名越高學習
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(); }
玩家輸入姓名界面this
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 }
做爲組長,不單單是負責好本身的模塊,更重要的是團隊的協調合做。
通過此次課設我對我本身的能力有了深入的認識,各種代碼的應用還不熟練,要能在未來繼續認真的學習代碼,提升自身水平。設計