public void mouseClicked(MouseEvent e){
目前成都創新互聯公司已為上1000家的企業提供了網站建設、域名、雅安服務器托管、網站托管運營、企業網站設計、翁牛特網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。
Object source=e.getSource(); //獲取事件源,即地鼠標簽
if(source instanceof JLabel){ //如果事件是標簽組件
JLabel mouse=(JLabel)source; //強制轉換為JLabel標簽
mouse.setIcon(null); //取消標簽圖標
}
}
});
this.getContentPane().add(mouses[i]); //添加顯示地鼠的標簽到窗體
}
mouses[0].setLocation(253, 300); //設置每個標簽的位置
mouses[1].setLocation(333, 250);
mouses[2].setLocation(388, 296);
mouses[3].setLocation(362, 364);
mouses[4].setLocation(189, 353);
mouses[5].setLocation(240, 409);
final JLabel backLabel=new JLabel(); //創建顯示背景的標簽
backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
this.setBounds(100,100,img.getIconWidth(),img.getIconHeight());
backLabel.setIcon(img); //添加背景到標簽
this.getContentPane().add(backLabel); //添加背景標簽到窗體
}
/**
* 線程的核心方法
*/
public void run(){
while(true){ //使用無限循環
try{
Thread.sleep(3000); //使線程休眠3秒
int index=(int)(Math.random()*6); //生成隨機的地鼠索引
if(mouses[index].getIcon()==null){ //如果地鼠標簽沒有設置圖片
mouses[index].setIcon(imgMouse); //為該標簽添加地鼠圖片
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
增加一個count計數,用來統計打中的次數。
點擊一個button的時候,判斷當前點擊的button的顏色是不是紅色,如果是,count++
你做的是iptv游戲嗎 機頂盒游戲嗎 還是模擬器運行的啊
請提供更多詳細信息,方便問題定位朋友
Shrewmouse初始化出錯,
圖片初始化錯了,可以這樣:
Image image = new ImageIcon("graphics/Window2.png").getImage();
public class Game extends Thread{
/**
* @param args
*/
String [][] gameMap = new String[3][3];
public static void main(String[] args) {
// TODO Auto-generated method stub
new Game().start();
}
public void initMap()
{
for (int i = 0; i gameMap.length; i++) {
for (int j = 0; j gameMap[0].length; j++) {
gameMap[i][j] = new String();
gameMap[i][j] = "O" ; //洞,表示沒有老鼠出來
}
}
}
public void printMap()
{
for (int i = 0; i gameMap.length; i++) {
for (int j = 0; j gameMap[0].length; j++) {
System.out.print(gameMap[i][j]);
}
System.out.println();
}
}
public void run()
{
int temp = 0 ;
while(true)
{
initMap();
for (int i = 0; i gameMap.length; i++) {
for (int j = 0; j gameMap[0].length; j++) {
temp = (int)(100 * Math.random());
if(temp=20) //可以調整,讓老鼠出現的概率降低一些
gameMap[i][j] = "@"; //有老鼠出現
}
}
printMap();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
}
}
}
呵呵,線程加數組就可以了,但是不知道能不能滿足你的要求。截圖如下:
1.先在Frame的左側添加應該JLabel用于顯示打擊次數:
JLabel l = new JLabel("hit!");
c.add(l, BorderLayout.WEST);
2.修改你的J_ActionListener類:
a.增加一個 JLabel 屬性 l
b.添加一個參數是 JLabel 的構造函數,將傳入的 JLabel實例賦值給屬性 this.l = l
c.修改actionPerformed方法,讓他將修改 l.text的值 l.setText("打了" + (++m_count) + "次")
3.最后在創建J_ActionListener的時候將界面上的JLabel賦值進去:
J_ActionListener a = new J_ActionListener(l);
完整代碼如下(類名首個字母要大寫,這個是約定,我就斗膽先修改下了。。):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
class J_ActionListener implements ActionListener {
int m_count = 0;
JLabel l;
public J_ActionListener(JLabel l) {
this.l = l;
}
public void actionPerformed(ActionEvent e) {
l.setText("打了" + (++m_count) + "次");
}
}
public class Datz extends JFrame {
private static final long serialVersionUID = 1L;
public Datz() {
super("打地鼠");
JButton b = new JButton("打一下");
JLabel l = new JLabel("hit!");
Container c = getContentPane();
c.add(b, BorderLayout.CENTER);
c.add(l, BorderLayout.WEST);
J_ActionListener a = new J_ActionListener(l);
b.addActionListener(a);
}
public static void main(String args[]) {
Datz app = new Datz();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(100, 80);
app.setVisible(true);
}
}