📄 highscorepanel.java
字号:
jLabel11.setText("\u5206\u6570");
jLabel12.setText("L");
jLabel13.setText("P");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel9)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jLabel10)
.addGap(18, 18, 18)
.addComponent(jLabel11)
.addGap(32, 32, 32)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(jLabel11)
.addComponent(jLabel13)
.addComponent(jLabel12))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 0, 210, 60));
jPanel4.setLayout(new java.awt.GridLayout(10, 4));
jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
jPanel4.setFont(new java.awt.Font("Tahoma", 0, 10));
add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 60, 210, 240));
}// </editor-fold>//GEN-END:initComponents
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
// TODO add your handling code here:
String writeMe;
writeMe="用户: "+user+" 分数: "+users[user-1].score+" 等级: "+users[user-1].level+" 块数: "+users[user-1].pieces;
Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
setClipboardText(sysc,writeMe);
}//GEN-LAST:event_jButton9ActionPerformed
private void initArray(){
for(int i=0;i<10;i++){
users[i]=new UserInfo();
top10[i]=new UserInfo();
}
}
public void saveRecord(){
try{
record.add(users);
record.add(top10);
FileOutputStream fileOut = new FileOutputStream("record.sav");
ObjectOutputStream objOut=new ObjectOutputStream(fileOut);
objOut.writeObject(record);
fileOut.close();
objOut.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
private void initTop(){
for(int i=0;i<10;i++)
for(int j=0;j<4;j++){
top[i][j]=new JLabel("未知");
jPanel4.add(top[i][j]);
}
}
private void loadRecord(){
try{
FileInputStream fis = new FileInputStream("record.sav");
ObjectInputStream is = new ObjectInputStream(fis);
record=(LinkedList)is.readObject();
users=(UserInfo[])record.poll();
top10=(UserInfo[])record.poll();
fis.close();
is.close();
}
catch(Exception e){
initArray();
System.out.println("没有找到已保存的记录");
saveRecord();
}
}
private void initListener(){
user1.addActionListener(this);
user2.addActionListener(this);
user3.addActionListener(this);
user4.addActionListener(this);
user5.addActionListener(this);
user6.addActionListener(this);
user7.addActionListener(this);
user8.addActionListener(this);
}
protected void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
public void actionPerformed(ActionEvent e) {
JRadioButton radio=(JRadioButton)e.getSource();
if(radio==user1) displayUserInfo(1);
else if(radio==user2) displayUserInfo(2);
else if(radio==user3) displayUserInfo(3);
else if(radio==user4) displayUserInfo(4);
else if(radio==user5) displayUserInfo(5);
else if(radio==user6) displayUserInfo(6);
else if(radio==user7) displayUserInfo(7);
else if(radio==user8) displayUserInfo(8);
}
public void setUserInfo(int score,int level,int pieces,int user){
users[user-1].score=score;
users[user-1].level=level;
users[user-1].pieces=pieces;
users[user-1].name=user;
saveRecord();
}
private void displayUserInfo(int i) {
loadRecord();
user=i;
display.setText("User "+i);
jLabel5.setText(users[i-1].score+"");
jLabel6.setText(users[i-1].level+"");
jLabel7.setText(users[i-1].pieces+"");
}
public void setTop(UserInfo u){
for (int i = 0; i < 10; i++) {
if(u.score>top10[i].score){
for(int j = 9;j>i;j--){//排行榜的重新排序
top10[j]=top10[j-1];
}
top10[i]=u;
break;
}
}
}
public void displayTop10(){
for (int i = 0; i < 10; i++) {
if(top10[i].name==0)top[i][0].setText("未知");
else top[i][0].setText("User "+top10[i].name);
top[i][1].setText(top10[i].score+"");
top[i][2].setText(top10[i].level+"");
top[i][3].setText(top10[i].pieces+"");
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JTextField display;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JRadioButton user1;
private javax.swing.JRadioButton user2;
private javax.swing.JRadioButton user3;
private javax.swing.JRadioButton user4;
private javax.swing.JRadioButton user5;
private javax.swing.JRadioButton user6;
private javax.swing.JRadioButton user7;
private javax.swing.JRadioButton user8;
// End of variables declaration//GEN-END:variables
protected UserInfo[] users=new UserInfo[10];
protected UserInfo[] top10=new UserInfo[10];
private int user=1;
JLabel[][] top=new JLabel[10][4];
LinkedList record=new LinkedList();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -