📄 game.java
字号:
/*
* Game.java
*
* Created on 2007年4月1日, 上午10:45
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package gilyou;
/**
*
* @author lenovo
*/
//package gilyou;
/*
* Game.java
*
* Created on 2007年3月30日, 下午10:40
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
//package gilyou;
/*
* Game.java
*
* Created on 2007年3月28日, 下午3:29
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*@version 1.0
* @author Gilyou
*/
import java.awt.*;
import java.awt.event.*;
//import java.io.*;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class Game extends JFrame implements ActionListener, WindowListener,
KeyListener {
/**
*
*/
@SuppressWarnings("deprecation")
public Game(String title) {
super(title);
mainFrame = new JFrame(title);
mainFrame.addWindowListener(this);
Container con = mainFrame.getContentPane();
con.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// add first label
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
firstLabel = new JLabel("NO.1");
firstLabel.setHorizontalAlignment(JLabel.RIGHT);
firstLabel.setSize(100, 100);
con.add(firstLabel, gbc);
// add first text field
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
firstTextField = new JTextField("", 1);
firstTextField.setToolTipText("您所猜第1个数:");
firstTextField.addKeyListener(this);
firstTextField.setEditable(false);
firstTextField.setSize(100, 100);
firstTextField.addKeyListener(this);
con.add(firstTextField, gbc);
// add second label
gbc.gridx = 3;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
secondLabel = new JLabel("NO.2");
secondLabel.setHorizontalAlignment(JLabel.RIGHT);
secondLabel.setSize(100, 100);
con.add(secondLabel, gbc);
// add second text field
gbc.gridx = 4;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
secondTextField = new JTextField("", 1);
secondTextField.setToolTipText("您所猜第2个数:");
secondTextField.addKeyListener(this);
secondTextField.setEditable(false);
secondTextField.setSize(100, 100);
secondTextField.addKeyListener(this);
con.add(secondTextField, gbc);
// add third label
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
thirdLabel = new JLabel("NO.3");
thirdLabel.setHorizontalAlignment(JLabel.RIGHT);
thirdLabel.setSize(100, 100);
con.add(thirdLabel, gbc);
// add third text field
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
thirdTextField = new JTextField("", 1);
thirdTextField.setToolTipText("您所猜第3个数:");
thirdTextField.addKeyListener(this);
thirdTextField.setEditable(false);
thirdTextField.setSize(100, 100);
thirdTextField.addKeyListener(this);
con.add(thirdTextField, gbc);
// add forth label
gbc.gridx = 3;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
forthLabel = new JLabel("NO.4");
forthLabel.setHorizontalAlignment(JLabel.RIGHT);
forthLabel.setSize(100, 100);
con.add(forthLabel, gbc);
// add forth text field
gbc.gridx = 4;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
forthTextField = new JTextField("", 1);
forthTextField.setToolTipText("您所猜第4个数:");
forthTextField.addKeyListener(this);
forthTextField.setEditable(false);
forthTextField.setSize(100, 100);
forthTextField.addKeyListener(this);
con.add(forthTextField, gbc);
// add the tip1
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
numLabel = new JLabel("数字正确数:");
con.add(numLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
numTextField = new JTextField();
numTextField.setEditable(false);
//tipLabel.setSize(arg0, arg1)
con.add(numTextField, gbc);
// add the tip2
gbc.gridx = 3;
gbc.gridy = 2;
napLabel = new JLabel("数位正确数:");
con.add(napLabel, gbc);
gbc.gridx = 4;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
napTextField = new JTextField();
napTextField.setEditable(false);
con.add(napTextField, gbc);
// add start button
gbc.gridx = 0;
gbc.gridy = 3;
startButton = new JButton("Start");
startButton.addActionListener(this);
startButton.setMnemonic('S');
startButton.setToolTipText("进入游戏");
startButton.setSize(100, 100);
con.add(startButton, gbc);
// add clear button
gbc.gridx = 1;
gbc.gridy = 3;
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setMnemonic('C');
clearButton.setToolTipText("清空内容");
clearButton.setEnabled(false);
clearButton.setSize(100, 100);
con.add(clearButton, gbc);
// add key button
gbc.gridx = 4;
gbc.gridy = 3;
keyButton = new JButton("Key");
keyButton.addActionListener(this);
keyButton.setMnemonic('K');
keyButton.setToolTipText("查看答案");
keyButton.setEnabled(false);
keyButton.setSize(100, 100);
con.add(keyButton, gbc);
// add again button
gbc.gridx = 1;
gbc.gridy = 4;
againButton = new JButton("Again");
againButton.addActionListener(this);
againButton.setMnemonic('A');
againButton.setToolTipText("再来一局");
againButton.setEnabled(false);
againButton.setSize(100, 100);
con.add(againButton, gbc);
// add exit button
gbc.gridx = 3;
gbc.gridy = 4;
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
exitButton.setMnemonic('E');
exitButton.setToolTipText("退出游戏");
exitButton.setSize(100, 100);
con.add(exitButton, gbc);
// add ok button
gbc.gridx = 3;
gbc.gridy = 3;
okButton = new JButton("OK");
okButton.addActionListener(this);
okButton.setMnemonic('O');
okButton.setToolTipText("确定");
okButton.setEnabled(false);
okButton.setSize(100, 100);
con.add(okButton, gbc);
// add radio group
gbc.gridx = 4;
gbc.gridy = 5;
normalRadio = new JRadioButton("Normal", true);
easyRadio = new JRadioButton("Easy");
levelRadio = new ButtonGroup();
levelRadio.add(easyRadio);
levelRadio.add(normalRadio);
con.add(normalRadio, gbc);
gbc.gridx = 4;
gbc.gridy = 4;
con.add(easyRadio, gbc);
// add tip label
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 4;
tipLabel = new JLabel(
"<html><font color=blue>Welcome to Gilyou game System!</font>");
tipLabel.setToolTipText("<html><body bgcolor = green><font color=blue>欢迎进入 !* 古流枫叶 *! </body></font>");
con.add(tipLabel, gbc);
//add history label
gbc.gridx = 0;
gbc.gridy = 4;
historyLabel = new JLabel("History", JLabel.LEFT);
historys = "<html>无历史可查!";
historyLabel.setToolTipText(historys);
historyLabel.setForeground(Color.green);
con.add(historyLabel, gbc);
//add copy right
gbc.gridx = 0;
gbc.gridy = 6;
copyRight = new JLabel("<html><font color=red>Gilyou® All Rights Reserved©");
copyRight.setToolTipText("<html><font color=red>Gilyou® 版权所有 ©");
con.add(copyRight,gbc);
//level select
if (easyRadio.isSelected()) {
isNormal = false;
} else {
isNormal = true;
}
//init the iArray
for (int i = 0; i < 4; i++) {
iArray[i] = rand
.nextInt(Math.abs((int) System.currentTimeMillis())) % 10;
for (int j = 0; j < i; j++)
if (iArray[i] == iArray[j])
i--;
}
mainFrame.setSize(firstLabel.getWidth()+firstTextField.getWidth()+secondLabel.getWidth()+secondTextField.getWidth(),firstLabel.getHeight()+thirdLabel.getHeight()+numLabel.getHeight()+startButton.getHeight()+againButton.getHeight()+historyLabel.getHeight()+copyRight.getHeight());
mainFrame.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-mainFrame.getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-mainFrame.getSize().height)/2);
//tipLabel.setText(Integer.toString(firstLabel.getWidth())+" "+Integer.toString(firstLabel.getWidth()+firstTextField.getWidth()+secondLabel.getWidth()+secondTextField.getWidth()));
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
// event deal with
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
try {
// start button clicked
if (e.getSource() == startButton) {
if (easyRadio.isSelected())
isNormal = false;
else
isNormal = true;
firstTextField.setEditable(true);
secondTextField.setEditable(true);
thirdTextField.setEditable(true);
forthTextField.setEditable(true);
keyButton.setEnabled(true);
//okButton.setEnabled(true);
//clearButton.setEnabled(true);
againButton.setEnabled(true);
easyRadio.setEnabled(false);
normalRadio.setEnabled(false);
startButton.setNextFocusableComponent(firstTextField);
startButton.setEnabled(false);
} else if (e.getSource() == clearButton) {
firstTextField.setText("");
secondTextField.setText("");
thirdTextField.setText("");
forthTextField.setText("");
if (!isNormal) {
firstTextField.setBackground(Color.white);
secondTextField.setBackground(Color.white);
thirdTextField.setBackground(Color.white);
forthTextField.setBackground(Color.white);
}
clearButton.setNextFocusableComponent(firstTextField);
clearButton.setEnabled(false);
} else if (e.getSource() == againButton) {
firstTextField.setText("");
firstTextField.setBackground(Color.white);
secondTextField.setText("");
secondTextField.setBackground(Color.white);
thirdTextField.setText("");
thirdTextField.setBackground(Color.white);
forthTextField.setText("");
forthTextField.setBackground(Color.white);
numTextField.setText("");
napTextField.setText("");
for (int i = 0; i < 4; i++) {
iArray[i] = rand.nextInt(Math.abs((int) System
.currentTimeMillis())) % 10;
for (int j = 0; j < i; j++) {
if (iArray[i] == iArray[j])
i--;
}
}
againButton.setNextFocusableComponent(startButton);
firstTextField.setEditable(false);
secondTextField.setEditable(false);
thirdTextField.setEditable(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -