📄 gamemain.java
字号:
b_moneyAdd.setText("+");
b_moneyAdd.setBounds(605, 306, 50, 20);
game.getCenterPanel().add(b_moneyAdd);
//赌注减按钮
final JButton b_moneySub = new JButton();
b_moneySub.setText("-");
b_moneySub.setBounds(665, 306, 50, 20);
game.getCenterPanel().add(b_moneySub);
/*
* 玩家姓名显示
*/
//电脑方
final JLabel l_computerName = new JLabel();
l_computerName.setText("电脑");
l_computerName.setBounds(136, 210, 60, 15);
game.getCenterPanel().add(l_computerName);
//玩家方
final JLabel l_userName = new JLabel();
l_userName.setText(gameUser.getUserName());
l_userName.setBounds(136, 320, 60, 15);
game.getCenterPanel().add(l_userName);
/*
* 玩家图片显示
*/
final JLabel computerIco = new JLabel("",new ImageIcon(imageFile + computerUser.getUserIco()),JLabel.CENTER);
computerIco.setBounds(60, 200, 80, 80);
game.getCenterPanel().add(computerIco);
final JLabel userIco = new JLabel("",new ImageIcon(imageFile + gameUser.getUserIco()),JLabel.CENTER);
userIco.setBounds(60, 290, 80, 80);
game.getCenterPanel().add(userIco);
/*
*玩家金钱显示
*
*/
//电脑金钱
final JLabel l_computerMoney = new JLabel();
l_computerMoney.setText("金钱:" + computerUser.getUserMoney());
l_computerMoney.setBounds(136, 235, 60, 15);
game.getCenterPanel().add(l_computerMoney);
//玩家金钱
final JLabel l_userMoney = new JLabel();
l_userMoney.setText("金钱:" + gameUser.getUserMoney());
l_userMoney.setBounds(136, 340, 60, 15);
game.getCenterPanel().add(l_userMoney);
/*
* 玩家的点数显示
*/
//玩家方得点
final JLabel l_userValue = new JLabel();
l_userValue.setBounds(470, 375, 60, 15);
game.getCenterPanel().add(l_userValue);
//电脑方得点
final JLabel l_computerValue = new JLabel();
l_computerValue.setBounds(470, 210, 60, 15);
game.getCenterPanel().add(l_computerValue);
//赢标志显示
final JLabel l_win = new JLabel();
l_win.setForeground(new Color(255, 128, 64));
l_win.setFont(new Font("", Font.BOLD, 26));
l_win.setText("赢");
l_win.setBounds(470, 220, 33, 44);
l_win.setVisible(false);
game.getCenterPanel().add(l_win);
/*--------------------事件处理------------*/
/*
*菜单栏按钮事件
*
*/
//退出按钮事件
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
//用户设置事件
setUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//System.exit(0);
final JFrame userSetFrame = new JFrame();
userSetFrame.setSize(400, 300);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int left = (screen.width - userSetFrame.getWidth()) / 2;
int top = (screen.height - userSetFrame.getHeight()) / 2;
userSetFrame.setLocation(left, top);
userSetFrame.setLayout(null);
userSetFrame.setResizable(false);
final JLabel l_setUserName = new JLabel();
l_setUserName.setText("用户名:");
l_setUserName.setBounds(100, 10, 100, 20);
final JTextField t_userName = new JTextField();
t_userName.setText(gameUser.getUserName());
t_userName.setBounds(160, 10, 100, 20);
final JLabel l_setUserMoney = new JLabel();
l_setUserMoney.setText("用户金钱:");
l_setUserMoney.setBounds(90, 40, 100, 20);
final JTextField t_userMoney = new JTextField();
t_userMoney.setText(gameUser.getUserMoney()+"");
t_userMoney.setBounds(160, 40, 100, 20);
final ButtonGroup chooseIco = new ButtonGroup();
final JRadioButton ico1 = new JRadioButton();
ico1.setText("天河");
ico1.setBounds(60, 160, 60, 20);
final JRadioButton ico2 = new JRadioButton();
ico2.setText("梦瑶");
ico2.setBounds(160, 160, 60, 20);
final JRadioButton ico3 = new JRadioButton();
ico3.setText("菱纱");
ico3.setBounds(245, 160, 60, 20);
chooseIco.add(ico1);
chooseIco.add(ico2);
chooseIco.add(ico3);
final JLabel l_ico1 = new JLabel("",new ImageIcon(imageFile + "1.png"),JLabel.CENTER);
final JLabel l_ico2 = new JLabel("",new ImageIcon(imageFile + "2.png"),JLabel.CENTER);
final JLabel l_ico3 = new JLabel("",new ImageIcon(imageFile + "3.png"),JLabel.CENTER);
final JPanel icoPanel = new JPanel();
icoPanel.add(l_ico1);
icoPanel.add(l_ico2);
icoPanel.add(l_ico3);
icoPanel.setBounds(30, 70, 300, 85);
final JButton b_setUser = new JButton();
b_setUser.setBounds(80, 200, 100, 20);
b_setUser.setText("确定");
final JButton b_setUserCancel = new JButton();
b_setUserCancel.setBounds(200, 200, 100, 20);
b_setUserCancel.setText("取消");
userSetFrame.add(l_setUserName);
userSetFrame.add(t_userName);
userSetFrame.add(l_setUserMoney);
userSetFrame.add(t_userMoney);
userSetFrame.add(ico1);
userSetFrame.add(ico2);
userSetFrame.add(ico3);
userSetFrame.add(icoPanel);
userSetFrame.add(b_setUser);
userSetFrame.add(b_setUserCancel);
userSetFrame.setVisible(true);
b_setUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
gameUser.setUserMoney(Integer.parseInt(t_userMoney.getText()));
if(ico1.isSelected())
gameUser.setUserIco("1.png");
else if(ico2.isSelected())
gameUser.setUserIco("2.png");
else if(ico3.isSelected())
gameUser.setUserIco("3.png");
gameUser.save();
l_userMoney.setText("金钱:" + gameUser.getUserMoney());
userIco.setIcon(new ImageIcon(imageFile + gameUser.getUserIco()));
userSetFrame.setVisible(false);
}
});
b_setUserCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
userSetFrame.setVisible(false);
}
});
}
});
//帮助事件
gameHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String message = "<html><h2>21点小游戏游戏规则</h2>";
message += "<p>游戏牌为一副除去大小王的牌</p>";
message += "<p>小于等于21点的点为有效点,大于21点的点为无效点</p>";
message += "<p>2-10按牌的数字点数计点,J,Q,K按10点算,A即可作为1点也可以作为2点</p>";
message += "<p>每个人最多可拿5张牌</p>";
message += "<p>玩家牌的点数为最接近21点的有效点赢,任何有效点都大于无效点</p>";
message += "<p>当玩家的牌的点数相等时,庄家赢</p>";
message += "<p>当玩家的点数刚好为21时,输或赢的金钱都为赌注的2倍</p>";
JOptionPane.showMessageDialog(game,message );
}
});
//关于按钮事件
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String message = "<html><h2>21点小游戏程序</h2>";
message += "<p>Author : FeiFei</p>";
message += "<p>E-Mail : feifei@dreammx.com</p></html>";
JOptionPane.showMessageDialog(game,message );
}
});
//音乐开按钮事件
musicOn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
game.soundPlay();
musicOn.setEnabled(false);
musicOff.setEnabled(true);
}
});
//音乐关按钮事件
musicOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
game.soundStop();
musicOff.setEnabled(false);
musicOff.setEnabled(true);
}
});
//stopSound
//soundPlay
/*
* 工具栏按钮事件处理
*
*/
//发牌按钮事件
b_dealCard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
game.countCardsValue(1);
if(game.getUserCards().size() < 5 && game.getCardsValue(1)< 21){
game.addCard(1);
game.countCardsValue(1);
game.getUserCardsPanel().removeAll();
game.getUserCardsPanel().updateUI();
}
else if(game.getUserCards().size() < 5 && game.getCardsValue(1) == 21){
String message = "<html><p>你21点啊,还不开牌...</p></html>";
JOptionPane.showMessageDialog(game,message );
}
else if(game.getUserCards().size() < 5){
String message = "<html><p>不好意思!你的点数大于21</p><p>你挂了... -_-</p></html>";
JOptionPane.showMessageDialog(game,message );
}
else{
String message = "<html><p>已经发了5张牌了,不能再发了</p></html>";
JOptionPane.showMessageDialog(game,message );
}
}
});
//重新开局按钮事件
b_newGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//发牌按钮可用
b_dealCard.setEnabled(true);
//开牌按钮可用
b_chechout.setEnabled(true);
//重新初始化游戏
game.gameStart();
//更新空间
game.getComputerCardsPanel(false).removeAll();
game.getComputerCardsPanel(false).updateUI();
game.getUserCardsPanel().removeAll();
game.getUserCardsPanel().updateUI();
//重新设置部分空间显示
l_userValue.setText("");
l_computerValue.setText("");
l_win.setVisible(false);
}
});
//开牌按钮事件
b_chechout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//将电脑方牌正面显示
game.getComputerCardsPanel(false).removeAll();
game.getComputerCardsPanel(true).updateUI();
game.countCardsValue(0);
game.countCardsValue(1);
//发牌按钮不可用
b_dealCard.setEnabled(false);
b_chechout.setEnabled(false);
//显示玩家得点
l_userValue.setText(game.getCardsValue(1) + "");
l_computerValue.setText(game.getCardsValue(0) + "");
//设置赌注
game.setGameMoney(Integer.parseInt(T_gameMoney.getText()));
//判断输赢
if(
(game.getCardsValue(1) < 21 && game.getCardsValue(0) < game.getCardsValue(1))
||
(game.getCardsValue(1) < 21 && game.getCardsValue(0) > 21)
){
//玩家赢单倍
l_win.setBounds(470, 340, 33, 44);
gameUser.setUserMoney(gameUser.getUserMoney() + game.getGameMoney());
computerUser.setUserMoney(computerUser.getUserMoney() - game.getGameMoney());
}
else if(game.getCardsValue(0) == 21 ){
//庄家得双倍
l_win.setBounds(470, 220, 33, 44);
computerUser.setUserMoney(computerUser.getUserMoney() + 2 * game.getGameMoney());
gameUser.setUserMoney(gameUser.getUserMoney() + game.getGameMoney());
}
else if(game.getCardsValue(1) == 21 && game.getCardsValue(0) != 21){
//玩家赢双倍
l_win.setBounds(470, 340, 33, 44);
gameUser.setUserMoney(gameUser.getUserMoney() + 2 * game.getGameMoney());
computerUser.setUserMoney(computerUser.getUserMoney() - 2 * game.getGameMoney());
}
else{
//庄家赢单倍
l_win.setBounds(470, 220, 33, 44);
computerUser.setUserMoney(computerUser.getUserMoney() + 2 * game.getGameMoney());
gameUser.setUserMoney(gameUser.getUserMoney() - game.getGameMoney());
}
l_computerMoney.setText(computerUser.getUserMoney() + "");
l_userMoney.setText(gameUser.getUserMoney() + "");
l_win.setVisible(true);
gameUser.save();
computerUser.save();
}
});
//赌注输入事件处理
T_gameMoney.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent arg0) {
int tempMoney = Integer.parseInt(T_gameMoney.getText());
if( tempMoney < 10){
T_gameMoney.setText("10");
}
else if(tempMoney < gameUser.getUserMoney() && tempMoney < computerUser.getUserMoney() ){
game.setGameMoney(tempMoney);
T_gameMoney.setText(tempMoney + "");
}
else if(tempMoney > gameUser.getUserMoney() || tempMoney > computerUser.getUserMoney()){
T_gameMoney.setText(Math.min(gameUser.getUserMoney(),computerUser.getUserMoney()) + "");
}
game.setGameMoney(Integer.parseInt(T_gameMoney.getText()));
}
});
//赌注加事件
b_moneyAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if((Integer.parseInt(T_gameMoney.getText()) + 10) < computerUser.getUserMoney() &&
(Integer.parseInt(T_gameMoney.getText()) + 10) < gameUser.getUserMoney()){
T_gameMoney.setText(Integer.parseInt(T_gameMoney.getText()) + 10 + "");
game.setGameMoney(Integer.parseInt(T_gameMoney.getText()));
}
}
});
//赌注减事件
b_moneySub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if((Integer.parseInt(T_gameMoney.getText()) - 10) > 0)
T_gameMoney.setText(Integer.parseInt(T_gameMoney.getText()) - 10 + "");
else
T_gameMoney.setText("10");
game.setGameMoney(Integer.parseInt(T_gameMoney.getText()));
}
});
game.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -