2013-03-13 11 views
0

我正在製作一個簡單的掃雷遊戲(頂部和其他東西沒有菜單)我需要的只是一個9x9板,當每個瓦片點擊時,如果沒有附近的炸彈, ,或者說有多少炸彈靠近它,或者說B是炸彈。Java如何在JFrame上安裝兩個JPanel

我已經完全正確地完成了,它的工作原理非常完美,我的問題是我需要放入一個從0開始的計時器,並在玩遊戲時不斷從0開始計數,直到我點擊停止的炸彈。然後,我會在董事會的下方放置一個用戶可以輸入姓名的地方,並在其下方有一個名爲「提交」的按鈕,用於指定該人的姓名和時間。 (注意玩家可以在任何時候做到這一點)

我的問題是,每當我嘗試添加一個JPanel或JLabel或任何真的到我的框架它不會放在它上,我的9x9網格不斷放大我的窗戶。我應該如何添加計時器顯示和提交名稱部分,以便我可以在窗口中看到它!

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 




public class MS extends JFrame implements ActionListener{ 

    static JFrame bframe; 
    static JPanel p; 
    static double[] bomb; 


    public MS() { 
     p = new JPanel(new GridLayout(9,10)); 

     JButton btn; 
     bomb = new double[81]; 
     for(int i=0; i<81; i++) 
      bomb[i] = Math.random(); 

     for (int i=0; i< 81; i++) { 
      btn = new JButton(); 
      btn.addActionListener(this); 
      btn.setActionCommand(Integer.toString(i)); 
      p.add(btn); 
     } 
    } 

    public static void main(String[] args) { 
     bframe=new MS(); //CREATE me and 
     bframe.add(timelabel); 
     bframe.add(p);  //add the JPanel 

     bframe.setLocation(32,32);    //where my upper left hand corner goes 
       //bframe.setSize(64*width, 64*height); 
       bframe.setSize(500,500); 
     bframe.setVisible(true);    //I start out invisible 
     bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //need this for the window manager 
    } 


    public void actionPerformed(ActionEvent e) { 

     JButton jb = (JButton) e.getSource(); 
     int temp = Integer.parseInt(e.getActionCommand()); 

     System.out.printf("%s\n", e.getActionCommand());   

     int count = 0; 
     String total = ""; 

     if(bomb[temp] > 0.15) { 
      //for top line 
      if((temp!=0) && (temp!=1) && (temp!=2) && (temp!=3) && (temp!=4) &&(temp!=5) && (temp!=6) && (temp!=7) && (temp!=8)) { 
       if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72)) 
        if(bomb[temp-10] <= 0.15) 
         count++; 
       if(bomb[temp-9] <= 0.15) 
        count++; 
       if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80)) 
        if(bomb[temp-8] <= 0.15) 
         count++; 
      } 
      if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72)) 
       if(bomb[temp-1] <= 0.15) 
        count++;  
      if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80)) 
       if(bomb[temp+1] <= 0.15) 
        count++; 

      if((temp!=72) && (temp!=73) && (temp!=74) && (temp!=75) && (temp!=76) && (temp!=77) && (temp!=78) && (temp!=79) && (temp!=80)) { 

       if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72)) 
        if(bomb[temp+8] <= 0.15) 
         count++; 
       if(bomb[temp+9] <= 0.15) 
        count++; 
       if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80)) 
        if(bomb[temp+10] <= 0.15) 
         count++; 
      } 
      if(count==0) 
       jb.setText(""); 

      else { 
       total=Integer.toString(count); 
       jb.setText(total);    
       if(count==1) 
        jb.setForeground(Color.blue); 
       if(count==2) 
        jb.setForeground(Color.green); 
       if(count==3) 
        jb.setForeground(Color.red); 
      } 
     } 
     else 
      jb.setText("B"); 
    } 
}    

爲清楚: 什麼,我想知道的是,爲什麼我的9x9的網格框的比例與窗口,我怎麼能解決這個問題,我如何才能增加它的另一個1個或2 jpanels這樣我就可以完成我的項目的其他部分。

+1

你是否真的這是你正在使用的代碼?從我看來,這甚至不應該編譯。例如,沒有構造函數匹配'新的MS(9,9)','gameOver(boolean)'沒有右大括號。 – 2013-03-13 21:16:10

+0

哦,是的,我只是刪除了一些東西,以便我的程序更短,你都可以看到它更容易,我只是忘了刪除一些東西 – 2013-03-13 21:18:02

+0

@KurtE如果你需要計時器的幫助,你需要使用計時器類。按照這個問題來獲得幫助。 [擺動計時器不停](http://stackoverflow.com/questions/14409868/swing-timer-not-stopping/14410163#14410163) – Smit 2013-03-13 21:24:15

回答

0

嘗試使用這種(未經測試):

public Test() { 
    setContentPane(new JPanel(new BorderLayout())); 
    JPanel p = new JPanel(new GridLayout(9, 9)); 
    JButton btn; 
    bomb = new double[81]; 
    for (int i = 0; i < 81; i++) 
     bomb[i] = Math.random(); 
    for (int i = 0; i < 81; i++) { 
     btn = new JButton(); 
     btn.addActionListener(this); 
     btn.setActionCommand(Integer.toString(i)); 
     p.add(btn); 
    } 
    getContentPane().add(p, BorderLayout.CENTER); // add the JPanel 
    JPanel timerPanel = new JPanel(); 
    timerPanel.add(timelabel); 
    getContentPane().add(timerPanel, BorderLayout.EAST); 
    setLocation(32, 32); // where my upper left hand corner goes 
    // bframe.setSize(64*width, 64*height); 
    setSize(500, 500); 
    setVisible(true); // I start out invisible 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // need this for the window manager 
} 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new Test(); 
     } 
    }); 
} 

所有Swing有關的活動應在事件指派線程,這就是爲什麼你應該使用invokeLater(Runnable)方法付諸表決。您需要使用另一個父面板,以便您可以將網格放置在中心位置,並在左邊放置timerPanelBorderLayout可能是最好的佈局管理器。它將網格面板添加到中心,並將timerPanel添加到左側。任何與時間有關的事情都應該被添加到timerPanel。任何其他部件應加入這樣的:

getContentPane().add(yourComponent, constraint); 

其中constraintBorderLayout.NORTH之一,BorderLayout.SOUTHBorderLayout.WESTBorderLayout.EAST等希望這有助於!

+0

當您調用「timerPanel.add(timelabel,BorderLayout.EAST);」時出現問題 – 2013-03-13 23:09:16

+0

@KurtE對不起,看我的編輯。 – 2013-03-13 23:17:46

+0

在你的主函數中,我必須做javax.swing嗎?事情?或者我應該做「新MS();」 – 2013-03-13 23:24:48