📄 controlpanel.java
字号:
/**
* This class models a HannoiTower.
*
* @author 程宇恒
* @version 1.0.0
*/
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
final class ControlPanel extends JPanel
{
static final int MAX_DELAY = 1000;
private JPanel discsPanel;
protected JButton bDiscsMinus,bDiscsPlus,bReset,bSolve,bPlace;
private Checkbox cbTimer;
protected JTextField tfDiscs,tfTimer,tName;//信息条
private Hanoi11 main;
private int discs,delay;
private ActionListener b排行榜 = new ActionListener (){
public void actionPerformed(ActionEvent event)
{
if(main.gameOver)
{
String name;
System.out.println("wozaihere if before");
System.out.println("zheshi:"+""+"ff");
System.out.println("zheshi:"+tName.getText()+"ff");
if(tName.getText()!="NULL"){
name = tName.getText();
System.out.println("wo zai if");
}else{
name = "无名氏";
System.out.println("wozai else");
}
/* if(tName.getText()==null)
{
name = "无名氏";
System.out.println("wozaihere11");
}
else
{
name = tName.getText();
System.out.println("wozaihere else");
}*/
System.out.println("wozaihere if After");
StringTokenizer token = new StringTokenizer(tfDiscs.getText()," ");
int floor = Integer.parseInt(token.nextToken());
int step = main.bd.getMoveCount();
StringTokenizer timeToken = new StringTokenizer(tfTimer.getText()," ");
String time = timeToken.nextToken() ;
StringTokenizer smallToken = new StringTokenizer(":"+time,":");
String hours = smallToken.nextToken();
String mins =smallToken.nextToken();
String sec =smallToken.nextToken();
int secS = Integer.parseInt(hours)*3600 + Integer.parseInt(mins)*60 + Integer.parseInt(sec);
String ss = name+" "+step+" "+secS;
System.out.println(ss);
List1 d1 = new List1(ss);
System.out.println(floor);
d1.setFileName(floor);
d1.readFromFile();
// System.out.println("Before initializationarrays()");
// d1.printList();
d1.initializationarrays();
// System.out.println("Before initializationarrays() ");
// d1.printList();
System.out.println(d1.toString());
d1.reWriteList();
d1.printList();
System.out.println(d1.toString());
d1. writeOnDisk();
/*计分方法*/
/* int score =floor*2000 -step*10-secS*10;
System.out.println("玩家"+" "+"盘子层数"+" "+"完成步数"+" "+"完成时间"+" "+"得分");
System.out.println(name+" "+tfDiscs.getText()+" "+main.bd.getMoveCount()+" "+time+" "+score);*/
// main.restartGame();
}
else
{
// main.restartGame();
}
}
};
private ActionListener bsolve = new ActionListener (){
public void actionPerformed(ActionEvent event){
setAutoSolveEnable(false);
main.startSolveThread();//这是要做的自动演示的事
}
};
private ActionListener bplus = new ActionListener (){
public void actionPerformed(ActionEvent event){
setDiscs(++discs);
setPlusMinusEnable();
main.restartGame();
}
};
private ActionListener bminus = new ActionListener (){
public void actionPerformed(ActionEvent event){
setDiscs(--discs);
setPlusMinusEnable();
main.restartGame();
}
};
private ActionListener breset = new ActionListener (){
public void actionPerformed(ActionEvent event){
main.restartGame();
}
};
ControlPanel(Hanoi11 hanoi11)
{
Hanoi11 _tmp = main;
delay=200;
discs = 3;
main = hanoi11;
setLayout(new GridLayout(8, 1, 0, 3));
Hanoi11 _tmp1 = hanoi11;
setFont(Hanoi11.textFont);
// DISKS
discsPanel = new JPanel();
discsPanel.setLayout(new BorderLayout(2, 0));
tfDiscs = new JTextField(3);
Hanoi11 _tmp2 = hanoi11;
tfDiscs.setFont(Hanoi11.monoFont);
tfDiscs.setForeground(Color.black);
tfDiscs.setBackground(Color.lightGray);
tfDiscs.setEditable(false);
setDiscs(discs);
discsPanel.add("West", bDiscsMinus = new JButton("-"));
bDiscsMinus.addActionListener(bminus);
Hanoi11 _tmp3 = hanoi11;
bDiscsMinus.setFont(Hanoi11.monoFont);
discsPanel.add("Center", tfDiscs);
discsPanel.add("East", bDiscsPlus = new JButton("+"));
bDiscsPlus.addActionListener(bplus);
Hanoi11 _tmp4 = hanoi11;
bDiscsPlus.setFont(Hanoi11.monoFont);
discsPanel.validate();
bSolve = new JButton("AUTOSOLVE");
bSolve.addActionListener(bsolve);
// TIMER
cbTimer = new Checkbox(" 计时器");
cbTimer.setBackground(Color.gray);
tfTimer = new JTextField(10);
tName = new JTextField(10);
tName.setText("名字");// 很危险!!
tName.setForeground(Color.black);
Hanoi11 _tmp5 = hanoi11;
tfTimer.setFont(Hanoi11.monoFont);
tfTimer.setForeground(Color.white);
tfTimer.setBackground(Color.darkGray);
tfTimer.setEditable(false);
//name input textfield
// construct panel
add(new Label("盘子层数", 1));
add(discsPanel);
add(bReset = new JButton("重新开始"));
bReset.addActionListener(breset);
add(bSolve = new JButton("AUTOSOLVE"));
bSolve.addActionListener(bsolve);
add(bPlace = new JButton("排行榜"));
bPlace.addActionListener(b排行榜);
add(cbTimer);
add(tfTimer);
tName.setFont(Hanoi11.monoFont);
tName.setForeground(Color.white);
tName.setBackground(Color.darkGray);
tName.setEditable(true);
add(tName);
tName.addActionListener(b排行榜);
// validate();
setPlusMinusEnable();
cbTimer.setState(true);
}
void setTimerEnable(boolean flag)
{
cbTimer.setEnabled(flag);
}
void setAutoSolveEnable(boolean flag)
{
bSolve.setEnabled(flag);
}
void setPlusMinusEnable()
{
Hanoi11 _tmp = main;
bDiscsPlus.setEnabled(discs < 9);
Hanoi11 _tmp1 = main;
bDiscsMinus.setEnabled(discs > 3);
}
void setDiscs(int i)
{
String s = Integer.toString(i);
if(s.length() == 1)
s = " "+s;
else
if(s.length() == 2)
s = " "+s;
tfDiscs.setText(s);
}
void setTName(String s)
{
tName.setText(s);
}
void setTimer(String s)
{
tfTimer.setText(s);
}
int getDiscs()
{
return discs;
}
int getDelay()
{
return delay;
}
boolean isTimerOn()
{
return cbTimer.getState();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -