import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* 设置时间的窗口类
*/
@SuppressWarnings("serial")
public class TimeRuleFrame extends JFrame implements ActionListener {
//标签
JLabel lblNewLabel;//一局所用时
JLabel hours;
JLabel minute_1;
JLabel seconds_1;
JLabel label;//步秒
JLabel minute_2;
JLabel seconds_2;
//在规则面板上的JTexitPane,输入框
private JPanel contentPane;
private JTextField hour;
private JTextField minute1;
private JTextField seconds1;
private JTextField minute2;
private JTextField seconds2;
//把设置的时间显示在棋盘界面上
String red1;
String red2;
String black1;
String black2;
//创建对象
private TimeRuleConfig timeRuleConfig;
TimeRule timerule;
//按钮
JButton confirm;
JButton cancel ;
//界面设计
public TimeRuleFrame() {
//创建对象
timerule =new TimeRule();
timeRuleConfig=new TimeRuleConfig();
contentPane = new JPanel();
contentPane.setBackground(SystemColor.inactiveCaptionBorder);
contentPane.setLocation(-28, -57);
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(SystemColor.control);
panel.setBounds(35, 36, 434, 116);
contentPane.add(panel);
panel.setLayout(null);
//一局所用时
lblNewLabel = new JLabel("一局限用时:");
lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
lblNewLabel.setBounds(10, 10, 101, 36);
panel.add(lblNewLabel);
minute_1 = new JLabel("分钟");
minute_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
minute_1.setBounds(299, 20, 36, 15);
panel.add(minute_1);
seconds_1 = new JLabel("秒");
seconds_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
seconds_1.setBounds(388, 20, 23, 15);
panel.add(seconds_1);
hours = new JLabel("小时");
hours.setFont(new Font("微软雅黑", Font.PLAIN, 14));
hours.setBounds(213, 20, 36, 15);
panel.add(hours);
//步秒
label = new JLabel("以后每一步限用时:");
label.setFont(new Font("微软雅黑", Font.BOLD, 15));
label.setBounds(10, 56, 151, 23);
panel.add(label);
minute_2 = new JLabel("分钟");
minute_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
minute_2.setBounds(213, 59, 45, 15);
panel.add(minute_2);
seconds_2 = new JLabel("秒");
seconds_2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
seconds_2.setBounds(299, 59, 36, 15);
panel.add(seconds_2);
hour = new JTextField();
hour.setBounds(160, 18, 45, 21);
panel.add(hour);
hour.setText("0");
hour.setColumns(10);
minute1 = new JTextField();
minute1.setBounds(259, 18, 36, 21);
minute1.setText("30");
panel.add(minute1);
minute1.setColumns(10);
seconds1 = new JTextField();
seconds1.setColumns(10);
seconds1.setBounds(342, 18, 36, 21);
seconds1.setText("0");
panel.add(seconds1);
minute2 = new JTextField();
minute2.setColumns(10);
minute2.setBounds(160, 57, 45, 21);
minute2.setText("0");
panel.add(minute2);
seconds2 = new JTextField();
seconds2.setColumns(10);
seconds2.setBounds(259, 57, 36, 21);
seconds2.setText("3");
panel.add(seconds2);
confirm = new JButton("确认");
confirm.setFont(new Font("微软雅黑", Font.PLAIN, 12));
confirm.setBounds(94, 174, 93, 40);
contentPane.add(confirm);
cancel = new JButton("取消");
cancel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
cancel.setBounds(300, 174, 93, 40);
contentPane.add(cancel);
//添加监听事件
confirm.addActionListener(this);
cancel.addActionListener(this);
ImageIcon img = new ImageIcon("image//背景2.jpg");// 这是背景图片
JLabel imgLabel = new JLabel(img);// 将背景图放在标签里。
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());// 设置背景标签的位置
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));// 注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。
((JPanel) contentPane).setOpaque(false); // 注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。
// 窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
this.setLocation((screenSize.width - frameSize.width) / 2 - 280,
(screenSize.height - frameSize.height) / 2 - 350);
// 设置
this.setSize(507, 263);
this.setTitle("中国象棋对战时间设置");
this.setIconImage(new ImageIcon("image\\红将.GIF").getImage());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 子窗口关闭,不关闭主窗口
this.setResizable(false);
this.setVisible(true);
}
//保存时间的设置
private void saveChange() throws NumberFormatException{
long fhours,fminutes,fseconds;
long sminutes,sseconds;
long total1,total2;
fhours = Long.parseLong(hour.getText());//getText():返回该标签所显示的文本字符串
fminutes = Long.parseLong(minute1.getText());
fseconds = Long.parseLong(seconds1.getText());
sminutes = Long.parseLong(minute2.getText());
sseconds = Long.parseLong(seconds2.getText());
total1 = fhours * 3600 + fminutes * 60 + fseconds;
total2 = sminutes * 60 + sseconds;
if (total1 < 0) {
JOptionPane.showMessageDialog( null, "请输入正确信息,不允许输入负数!");
}else
timeRuleConfig.setBaseTime(total1);//设置局秒
if (total2 < 0) {
JOptionPane.showMessageDialog( null,"请输入正确信息,不允许输入负数!");
}else
timeRuleConfig.setAddTimePerStep(total2);//设置步秒
}
//显示输入的时间
private void display(){
long fhours,fminutes,fseconds;
long sminutes,sseconds;
fhours = timeRuleConfig.getBaseTime() / 3600;
fminutes = timeRuleConfig.getBaseTime() % 3600 / 60;
fseconds = timeRuleConfig.getBaseTime() % 60;
sminutes = timeRuleConfig.getAddTimePerStep() / 60;
sseconds = timeRuleConfig.getAddTimePerStep() % 60;
this.red1=fhours+":"+fminutes+":"+fseconds;
this.black1=fhours+":"+fminutes+":"+fseconds;
this.red2=sminutes+":"+sseconds;
this.black2=sminutes+":"+sseconds;
timerule.setRed1(this.red1);
timerule.setBlack1(this.black1);
timerule.setRed2(this.red2);
timerule.setBlack2(this.black2);
//System.out.println(black1);//测试得出red1...black2都被记录在timerule
}
//关闭窗口
public void closeDialog(){
this.dispose();
}
//监听按钮事件
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(confirm)){
try{
saveChange();
display();
closeDialog();
}catch(NumberFormatException exc){
System.err.println("Invalid data you inputed!!!"+exc.getMessage());
}
}else if (e.getSource().equals(cancel)){
closeDialog();
}
}
/* 使ChessMainFrame新创建的TimeRule类可以==在TimeRuleFrame新创建的TimeRule类,否则在
ChessMainFrame新创建的TimeRule类会是一个新的对象,就无法调用在TimeRuleFrame里对
TimeRule属性(Red1...black2)保存改变过的值,所以在ChessMainFrame里调用TimeRule的属性
(Red1..black2)的值就会被默认为是null了
*/
public TimeRule getTimerule() {
return this.timerule;
}
public TimeRuleConfig getTimeRuleConfig() {
return this.timeRuleConfig;
}
}