📄 recording.java
字号:
package MiniGame;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
public class Recording extends JDialog implements Serializable
{ private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
public JTextField jName = null;
private JLabel jLabel1 = null;
private JList jPlayerList = new JList();
private JButton jbtConfirm = null;
private JButton jbtCancel = null;
private static final int COUNTS = 10;
public int peopleNumber = 0;//记录个数
///////////////////////////////////////////////////////////////
private static Recordcomparator cmp = new Recordcomparator();
/////////////////////////////////////////////////////////////////
private String filename = "TenTop.dat" ;
private static ArrayList <People> TopTen = new ArrayList<People>();
public Recording()
{
super();
peopleNumber = 0;
readRecords();
initialize();
}
private void initialize()
{
this.setSize(297, 379);
this.setContentPane(getJContentPane());
this.setTitle("Top 10");
//////////////////////////////////////////
this.setModal(true);
//////////////////////////////////
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new Recording_this_windowAdapter(this));
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setResizable(false);
////////////////////////////////////////////////////////
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
////////////////////////////////////////////////
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2 ;
int y = (dim.height - h) / 2 ;
this.setLocation(x,y);
this.setVisible(true);
}
public void displayTopTen()
{ if(TopTen.size() > 0)
{
////////////////////////////////////////////////////////////////
Collections.sort(TopTen, cmp);
Object[]list = (Object []) TopTen.toArray(new Object[0]);
jPlayerList.setListData(TopTen.toArray(list));
//////////////////////////////////////////////////////////////////////////
}
}
boolean isTopTen(int score)
{
Collections.sort(TopTen, cmp);
if(TopTen.size() < COUNTS)
return true;
else
{
if(score > TopTen.get(TopTen.size() - 1).getScore())
return true;
else
return false;
}
}
public void readRecords()
{
try
{
File file = new File(".\\.\\" + filename);
if(file.exists())
{
//////////////////////////////////////////////////////////////////
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
///////////////////////////////////////////////////////////
peopleNumber = ( int )in.read();
for(int i = 0; i < peopleNumber; i ++)
{
TopTen.add((People)in.readObject()) ;
}
in.close();
displayTopTen();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public void SaveRecords()
{
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
peopleNumber = TopTen.size();
out.write(peopleNumber);
for(int i = 0 ; i < TopTen.size(); i ++)
{
out.writeObject(TopTen.get(i));
}
out.close();
}
catch( Exception ex )
{
System.out.println( ex );
}
}
public void Update(String name, int score )
{
Collections.sort(TopTen, cmp);
if(TopTen.size() == COUNTS)
TopTen.remove(TopTen.size() - 1);
People person = new People(name, score);
TopTen.add(person);
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.insets = new Insets(7, 8, 17, 25);
gridBagConstraints5.gridy = 3;
gridBagConstraints5.ipadx = 34;
gridBagConstraints5.ipady = 3;
gridBagConstraints5.gridx = 1;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.insets = new Insets(7, 21, 17, 23);
gridBagConstraints4.gridy = 3;
gridBagConstraints4.ipadx = 56;
gridBagConstraints4.ipady = 3;
gridBagConstraints4.gridx = 0;
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.BOTH;
gridBagConstraints3.gridwidth = 2;
gridBagConstraints3.gridx = 0;
gridBagConstraints3.gridy = 2;
gridBagConstraints3.ipadx = 245;
gridBagConstraints3.ipady = 188;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.weighty = 1.0;
gridBagConstraints3.insets = new Insets(3, 21, 7, 25);
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.insets = new Insets(4, 116, 2, 110);
gridBagConstraints2.gridx = 0;
gridBagConstraints2.gridy = 1;
gridBagConstraints2.ipadx = 18;
gridBagConstraints2.ipady = 6;
gridBagConstraints2.gridwidth = 2;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
gridBagConstraints1.gridx = 1;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.ipadx = 120;
gridBagConstraints1.ipady = 4;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(17, 2, 3, 14);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.insets = new Insets(17, 9, 3, 2);
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 3;
gridBagConstraints.ipady = 8;
gridBagConstraints.gridx = 0;
jLabel1 = new JLabel();
jLabel1.setText("TOP 10");
jLabel = new JLabel();
jLabel.setText("Please Enter Your Name");
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(jLabel, gridBagConstraints);
jContentPane.add(getJName(), gridBagConstraints1);
jContentPane.add(jLabel1, gridBagConstraints2);
jContentPane.add(getJPlayerList(), gridBagConstraints3);
jContentPane.add(getJbtConfirm(), gridBagConstraints4);
jContentPane.add(getJbtCancel(), gridBagConstraints5);
}
return jContentPane;
}
public JTextField getJName()
{
if (jName == null)
{
jName = new JTextField();
jName.setText("");
jName.setEditable(false);
if (isTopTen(MyPanel.grade))
{
jName.setEditable(true);
}
jName.setHorizontalAlignment(JTextField.CENTER);
}
return jName;
}
private JList getJPlayerList()
{
if (jPlayerList == null)
{
jPlayerList = new JList();
jPlayerList.setFixedCellWidth(30);
}
return jPlayerList;
}
private JButton getJbtConfirm()
{
if (jbtConfirm == null)
{jbtConfirm = new JButton();
jbtConfirm.setText("OK");
jbtConfirm.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
if(isTopTen(MyPanel.grade) &&
!jName.getText().equals("")/* && MyPanel.isAdded == false*/)
{
Update(jName.getText(), MyPanel.grade);
jName.setText("");
// MyPanel.isAdded= true;
jbtConfirm.setEnabled(false);
}
displayTopTen();
}
});
}
return jbtConfirm;
}
private JButton getJbtCancel()
{
if (jbtCancel == null)
{
jbtCancel = new JButton();
jbtCancel.setText("Quit");
jbtCancel.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
SaveRecords();
TopTen.clear();
dispose();
}
});
}
return jbtCancel;
}
public void this_windowClosing(WindowEvent e) {
SaveRecords();
TopTen.clear();
dispose();
}
}
class Recording_this_windowAdapter extends WindowAdapter {
private Recording adaptee;
Recording_this_windowAdapter(Recording adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -