ma33699619
java制作小游戏21点,想放背景图,但试了好久只能加背景色

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Window;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.WindowConstants;

public class MyFrame extends JFrame {

//界面布置

private JButton btnRestart=new JButton("重新开局");

private JButton btnYao=new JButton("要牌");

private JButton btnBuYao=new JButton("不要");

private JLabel lblResult=new JLabel("请点击重新开局 开始游戏");

private JLabel lblComputer=new JLabel("电脑的牌:");

private JLabel lblPlayer=new JLabel("玩家的牌:");

private JLabel lblPlayerDianShu=new JLabel("");

private JLabel lblComputerDianShu=new JLabel("");

//全新的一副牌,共52张

private ArrayList *本站禁止HTML标签噢* allCard=new ArrayList *本站禁止HTML标签噢* ();

//玩家手上的牌

private ArrayList *本站禁止HTML标签噢* playerCardList=new ArrayList *本站禁止HTML标签噢* ();

//电脑手上的牌

private ArrayList *本站禁止HTML标签噢* computerCardList=new ArrayList *本站禁止HTML标签噢* ();

//玩家牌的坐标

private int playerCard_x=220;

//电脑牌的坐标

private int computerCard_x=220;

public MyFrame() {

//界面属性设置

this.setTitle("21点游戏");

this.setSize(800, 600);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

this.setResizable(false);

this.setLayout(null);

this.setBackground(Color.LIGHT_GRAY);

this.getContentPane().setBackground(Color.LIGHT_GRAY);

btnRestart.setSize(100, 25);

btnRestart.setLocation(150, 500);

btnRestart.addActionListener(new btnResult_click());

this.add(btnRestart);

btnYao.setSize(100, 25);

btnYao.setLocation(350, 500);

btnYao.addActionListener(new btnYao_click());

btnYao.setEnabled(false);

this.add(btnYao);

btnBuYao.setSize(100, 25);

btnBuYao.setLocation(550, 500);

btnBuYao.addActionListener(new btnBuYao_click());

btnBuYao.setEnabled(false);

this.add(btnBuYao);

lblResult.setSize(300, 25);

lblResult.setLocation(125, 450);

this.add(lblResult);

lblComputer.setSize(100, 25);

lblComputer.setLocation(100, 85);

this.add(lblComputer);

lblPlayer.setSize(100, 25);

lblPlayer.setLocation(100, 285);

this.add(lblPlayer);

lblPlayerDianShu.setSize(100, 25);

lblPlayerDianShu.setLocation(200, 375);

lblPlayerDianShu.setVisible(false);

this.add(lblPlayerDianShu);

lblComputerDianShu.setSize(100, 25);

lblComputerDianShu.setLocation(200, 175);

lblComputerDianShu.setVisible(false);

this.add(lblComputerDianShu);

}

/**

* 生成一幅新的牌,共52张

*/

private void createAllCard() {

String[] hs={"黑","红","梅","方"};

for(int i=1;i<13;i++) {

for(int k=0;k<hs.length;k++) {

Card c=new Card();

c.setShuZi(i);

c.setHuaSe(hs[k]);

c.setZhengMain(false);

//设置点数

int dianShu=i;

if(dianShu>10) {

dianShu=10;

}

c.setDianShu(dianShu);

allCard.add(c);

}

}

}

/**

* 重新开局 按钮的响应

* @author student

*/

class btnResult_click implements ActionListener {

public void actionPerformed(ActionEvent e) {

//初始化游戏

initGame();

//生成新的牌

createAllCard();

//发给玩家一张

sendToPlayerOneCard();

//发给电脑一张

sendToComputerOneCard();

lblComputerDianShu.setVisible(false);

lblPlayerDianShu.setVisible(false);

}

}

/**

* 要牌 按钮的响应

*/

class btnYao_click implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

// 给玩家发一张牌

sendToPlayerOneCard();

//给电脑发一张牌

int computerDianShuHe=calculateComputerDianShu();

if(computerDianShuHe<=16) {

sendToComputerOneCard();

}

}

}

/**

* 发给玩家一张牌

*/

private void sendToPlayerOneCard() {

Random rand=new Random();

int index=rand.nextInt(allCard.size());

Card c=allCard.get(index);

playerCardList.add(c);

allCard.remove(index);

c.setZhengMain(true);

c.loadImage();

JLabel lbl=c.getCardLabel();

lbl.setLocation(playerCard_x, 250);

this.add(lbl);

this.repaint();

playerCard_x+=78;

}

/**

* 发给电脑一张牌

*/

private void sendToComputerOneCard() {

Random rand=new Random();

int index=rand.nextInt(allCard.size());

Card c=allCard.get(index);

computerCardList.add(c);

allCard.remove(index);

c.setZhengMain(false);

c.loadImage();

JLabel lbl=c.getCardLabel();

lbl.setLocation(computerCard_x, 50);

this.add(lbl);

this.repaint();

computerCard_x+=78;

}

/**

* 翻开电脑手上的所有牌

*/

private void fanKaiComputerCard() {

for (int i=0;i<computerCardList.size();i++) {

Card c=computerCardList.get(i);

c.setZhengMain(true);

c.loadImage();

}

this.repaint();

}

/**

* 清除窗体上所有的牌的JLabel控件

*/

private void initGame() {

//清除电脑的牌的JLabel控件

for(int i=0;i<computerCardList.size();i++) {

Card c=computerCardList.get(i);

JLabel lbl=c.getCardLabel();

this.remove(lbl);

}

//清除玩家的牌的JLabel控件

for(int i=0;i<playerCardList.size();i++) {

Card c=playerCardList.get(i);

JLabel lbl=c.getCardLabel();

this.remove(lbl);

}

//清除各个数组中的元素

computerCardList.clear();

playerCardList.clear();

allCard.clear();

//重置牌的横坐标

computerCard_x=220;

playerCard_x=220;

//设置按钮的可用性

btnRestart.setEnabled(false);

btnYao.setEnabled(true);

btnBuYao.setEnabled(true);

}

/**

* 计算电脑手上的牌的点数和

*/

private int calculateComputerDianShu() {

int sum=0;//点数和

for(int i=0;i<computerCardList.size();i++) {

Card c=computerCardList.get(i);

sum+=c.getDianShu();

}

return sum;

}

/**

* 计算玩家手上的牌的点数和

*/

private int calculatePlayerDianShu() {

int sum=0;

for(int i=0;i<playerCardList.size();i++) {

Card c=playerCardList.get(i);

sum+=c.getDianShu();

}

return sum;

}

/**

* 不要 按钮的响应

*/

class btnBuYao_click implements ActionListener {

//给电脑补全牌

public void actionPerformed(ActionEvent e) {

while (true) {

int computerDianShuHe = calculateComputerDianShu();

if (computerDianShuHe<=16) {

sendToComputerOneCard();

} else {

break;

}

}

//把电脑牌翻过来

fanKaiComputerCard();

//判断输赢

int C=calculateComputerDianShu();

int P=calculatePlayerDianShu();

if (C>P && C<=21) {

lblResult.setText("很遗憾,你输了!!");

} else if (C<=21 && P>21) {

lblResult.setText("你爆点了!很遗憾,你输了!!");

} else if (C==P) {

lblResult.setText("平局");

} else if (C>21 && P>21) {

lblResult.setText("平局");

} else if (P>C && P<=21) {

lblResult.setText("恭喜你,你胜了!!");

} else if (P<=21 && C>21) {

lblResult.setText("电脑爆点了!恭喜你,你胜了!!");

}

//重新开始后的按钮的可用性

btnRestart.setEnabled(true);

btnYao.setEnabled(false);

btnBuYao.setEnabled(false);

//显示玩家和电脑的点数和

lblComputerDianShu.setVisible(true);

lblComputerDianShu.setText("电脑点数和:"+calculateComputerDianShu());

lblPlayerDianShu.setVisible(true);

lblPlayerDianShu.setText("玩家点数和:"+calculatePlayerDianShu());

}

}

}

#21t 求大神帮看下怎么加背景图上去,有其它美化也可以

a294594994
建一个特大的
展开Biu

建一个特大的ImageView......

[查看全文]