📄 timepanel.java
字号:
package cchess;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TimePanel extends JPanel{
private TimerThread myTimerThread;
private TimerThread yourTimerThread;
private int myH, myS, myM;
private int yourH, yourS, yourM;
private int mySH, mySS, mySM;
private int yourSH, yourSS, yourSM;
private JLabel upLabel;
private JLabel downLabel;
public TimePanel(){
myH = myS = myM = yourH = yourS = yourM = 0;
mySH = mySS = mySM = yourSH = yourSS = yourSM = 0;
this.setBackground(Color.LIGHT_GRAY);
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
this.setBounds(ChessApp.getX(250),ChessApp.getY(550),ChessApp.getX(250),ChessApp.getY(58));
this.setLayout(new FlowLayout());
upLabel = new JLabel("己方-局时:" + myH + ":" + myM + ":" + myS +
" 对方-局时:" + yourH + ":" + yourM + ":" + yourS);
downLabel = new JLabel(" 步时" + mySH + ":" + mySM + ":" + mySS +
" 步时" + yourSH + ":" + yourSM + ":" + yourSS);
this.add(upLabel);
this.add(downLabel);
}
public void gameStart(){
timeClear();
myTimerThread = new TimerThread(true);
yourTimerThread = new TimerThread(false);
}
public void timeStart(){
yourTimerThread.setStop();
myTimerThread = new TimerThread(true);
myTimerThread.setRun();
}
public void timeStop(){
myTimerThread.setStop();
yourTimerThread = new TimerThread(false);
yourTimerThread.setRun();
}
public void yourStepClear(){
yourSH = yourSS = yourSM = 0;
}
public void myStepClear(){
mySH = mySS = mySM = 0;
}
public void timeClear(){
if (myTimerThread != null)
myTimerThread.setStop();
if (yourTimerThread != null)
yourTimerThread.setStop();
myH = myS = myM = yourH = yourS = yourM = 0;
mySH = mySS = mySM = yourSH = yourSS = yourSM = 0;
reLabel();
}
public void reLabel(){
upLabel.setText("己方-局时:" + myH + ":" + myM + ":" + myS +
" 对方-局时:" + yourH + ":" + yourM + ":" + yourS);
downLabel.setText(" 步时" + mySH + ":" + mySM + ":" + mySS +
" 步时" + yourSH + ":" + yourSM + ":" + yourSS);
}
class TimerThread extends Thread{
private boolean myTime;
private boolean stop;
public TimerThread(boolean my){
myTime = my;
stop = true;
}
public void setStop(){
stop = true;
}
public void setRun(){
stop = false;
this.start();
}
public void run(){
while(true){
try{
sleep(1000);
if (stop)
break;
else{
if (myTime){
///////////////////
if (myS < 59){
++ myS;
}
else{
myS = 0;
if (myM < 59){
++ myM;
}
else{
myM = 0;
++ myH;
}
}
if (mySS < 59){
++ mySS;
}
else{
mySS = 0;
if (mySM < 59){
++ mySM;
}
else{
mySM = 0;
++ mySH;
}
}
///////////////////
}
else{
///////////////////
if (yourS < 59){
++ yourS;
}
else{
yourS = 0;
if (yourM < 59){
++ yourM;
}
else{
yourM = 0;
++ yourH;
}
}
if (yourSS < 59){
++ yourSS;
}
else{
yourSS = 0;
if (yourSM < 59){
++ yourSM;
}
else{
yourSM = 0;
++ yourSH;
}
}
///////////////////
}
reLabel();
}
}catch(InterruptedException e){
System.out.print(e.toString());
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -