java对话框游戏代码 java对话框游戏代码是什么( 二 )


SwingUtilities.updateComponentTreeUI(MainFrame.this);
} catch (Exception ex) {
}
}
// 初始化组件
private void initComponent() {
lblResult = new JLabel();
lblInfo = new JLabel("准备好了吗?", JLabel.LEFT);
txtGuessRecord = new JTextArea(5, 30);
btnGuess = new JButton("猜一下");
btnRestart = new JButton("开始");
btnClear = new JButton("清除");
btnSave = new JButton("保存");
String[] item = new String[] { "数字", "字母" };
cbType = new JComboBox(item);
cbType.setMaximumSize(new Dimension(70, 20));
String ps = "[a-zA-Z\\d]";
Pattern p = Pattern.compile(ps);
txtInput = new JFormattedTextField(new RegexFormatter(p));
txtInput.setMaximumSize(new Dimension(55, 20));
txtInput.setMinimumSize(new Dimension(55, 20));
btnRestart.addActionListener(this);
btnGuess.addActionListener(this);
btnClear.addActionListener(this);
btnGuess.setEnabled(false);
txtGuessRecord.setEditable(false);
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
Box mainBox = Box.createVerticalBox();
add(mainBox);
setText('?');
mainBox.add(lblResult);
Box box = Box.createHorizontalBox();
box.add(lblInfo);
mainBox.add(box);
Box b = Box.createHorizontalBox();
b.add(txtInput);
Box hBox = Box.createHorizontalBox();
hBox.add(b);
hBox.add(btnGuess);
hBox.add(Box.createHorizontalStrut(3));
hBox.add(btnRestart);
hBox.add(Box.createHorizontalStrut(3));
hBox.add(btnClear);
hBox.add(Box.createHorizontalStrut(3));
hBox.add(btnSave);
hBox.add(Box.createHorizontalStrut(3));
hBox.add(cbType);
mainBox.add(hBox);
JScrollPane scroll = new JScrollPane(txtGuessRecord);
mainBox.add(scroll);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnRestart) {
// 重玩
restart();
} else if (e.getSource() == btnGuess)
// 猜
guess();
else if (e.getSource() == btnClear)
// 清除
clear();
else if (e.getSource() == btnSave)
save();
}
// 设置显示答案
private void setText(char c) {
StringBuilder sb = new StringBuilder();
sb.append("htmlfont size = 72 color = redcenter");
sb.append(c);
sb.append("/center/font/html");
lblResult.setText(sb.toString());
}
// 创建随机字符
private char createRandomChar(int type) {
Random rand = new Random();
int low = 0;
int range = 0;
switch (type) {
case NUM:
low = (int) '0';
range = (int) '9' - low + 1;
break;
case LETTER:
low = (int) 'A';
range = (int) 'Z' - low + 1;
break;
}
int i = rand.nextInt(range) + low;
char c = (char) i;
return c;
}
// 向记录框中输出时间
private void recordTime() {
Date date = new Date();
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.US);
String sd = formater.format(date);
【java对话框游戏代码 java对话框游戏代码是什么】txtGuessRecord.append(sd);
}
private void guess() {
if (txtInput.getValue() == null)
return;
// 用户的答案
player_answer = Character.toUpperCase(txtInput.getValue().toString()
.charAt(0));
// 判断答案
if (player_answercurrent_puzzle)
txtGuessRecord.append("小了\n");
else if (player_answercurrent_puzzle)
txtGuessRecord.append("大了\n");
else {
txtGuessRecord.append("正确\n");
isRight = true;
}
// 猜了一次
++current_time;
// 显示游戏信息
showInfo();
if (isRight || current_time = MAX_TIME)
stop();
}
// 重新开始

推荐阅读