2012-12-04 70 views
0

我正在嘗試編寫生命遊戲。但是,無論我多麼努力地嘗試,我似乎無法使用按鈕將Cell組件添加到框架上。如果我創建一個新框架並添加它們,它們確實會出現,但即便如此,我似乎也無法添加多個組件。無法將組件添加到擴展JFrame的類中

因此,我如何將組件添加到框架上,以及如何添加多個組件? 謝謝!

這裏是我的代碼:

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.Rectangle; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

public class Visuals extends JFrame implements ActionListener{ 

    private static Board b; 
    private GameOfLife g; 
    private JButton nextGen; 
    private JButton random; 

    public Visuals(Board b){ 
     super("The Game of Life"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     JPanel panel=new JPanel(); 
     nextGen = new JButton("Next Generation"); 
     panel.add(nextGen); 
     nextGen.addActionListener(this); 

     random = new JButton("Random"); 
     panel.add(random); 
     random.addActionListener(this); 
     this.add(panel); 

     this.setSize(800, 700); 
     this.setVisible(true); 


     /*for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){ 
      for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){ 
       Cell c= new Cell(xShift,yShift,i,j); 
       this.add(c); 
      } 
     }*/ 
     Cell c= new Cell(100,100,1,1); 
     Cell d= new Cell(200,200,1,1); 
     this.add(c); 
     this.add(d); 

     JFrame frame=new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(800, 700); 
     frame.setVisible(true); 

     frame.add(c); 
     frame.add(d); 

    } 

    public void paint(Graphics g){ 
     super.paint(g); 
     for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){ 
      for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){  
       if(b.getSpot(i, j)==1){ 
        g.setColor(Color.red); 
        g.fillRect(xShift, yShift, 10, 10); 
       } 
       else{ 
        g.setColor(Color.white); 
        g.fillRect(xShift, yShift, 10, 10); 
       } 
      } 
     } 
    } 

    public static void main(String[] cnbe){ 
     b= new Board (30,30); 
     Visuals v = new Visuals(b); 
    } 

    public void actionPerformed(ActionEvent event) { 
     Object cause = event.getSource(); 
     if(cause == nextGen){ 
      GameOfLife g=new GameOfLife(b); 
      g.nextGeneration(); 
      System.out.println(g); 
      this.repaint(); 
     } 
     if(cause == random){ 
      GameOfLife g=new GameOfLife(b); 
      g.random(); 
      System.out.println(g); 
      this.repaint(); 
     } 
    } 

    public class Cell extends JComponent{ 
     int row; 
     int col; 
     int x; 
     int y; 
     Rectangle r; 

     public Cell(int x, int y, int row, int col){ 
      r= new Rectangle(x,y,10,10); 
       this.row=row; 
       this.col=col; 
       this.x=x; 
       this.y=y; 

     } 


     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      g.drawString("DRAW ME",200,200); 
      Graphics2D g2= (Graphics2D) g; 
      g2.setColor(Color.green); 
      r= new Rectangle(x,y,100,100); 
      g2.fill(r); 

      /*if(b.getSpot(row, col)==1){ 

       g.setColor(Color.red); 
       g.fillRect(x, y, 10, 10); 
      } 
      else{ 

       g.setColor(Color.white); 
       g.fillRect(x, y, 10, 10); 
      }*/ 
     } 

     class MousePressListener implements MouseListener{ 

      public void mousePressed(MouseEvent event) { 
       /*int x= event.getX(); 
       int y= event.getY(); 
       if(x>r.getX() && x<r.getX()+10 && y>r.getY() && y<r.getY()+10){ 
        if(b.getSpot(row, col)==1){ 
         b.setSpot(row, col, 0); 
         repaint(); 
        } 
        else{ 
         b.setSpot(row, col, 1); 
         repaint(); 
        } 
       }*/ 
      } 

      public void mouseReleased(MouseEvent arg0) { 
      } 
      public void mouseClicked(MouseEvent arg0) { 
      } 
      public void mouseEntered(MouseEvent arg0) { 
      } 
      public void mouseExited(MouseEvent arg0) { 
      } 

     } 


    } 

} 
+3

您與幀佈局管理器發生衝突。默認情況下,一個'JFrame'將使用'BorderLayout',每次你添加一個新的組件到框架時,它將取代你之前添加的任何東西(到中心位置)。查看[佈局管理器視覺指南](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)和[使用佈局管理器](http://docs.oracle.com)。 COM/JavaSE的/教程/ uiswing /佈局/ using.html)。 – MadProgrammer

+0

此外,組件只能屬於一個容器,因此當您將單元格添加到第一個框架,然後將其添加到第二個框架時,它們將從第一個框架開始移除 – MadProgrammer

+2

爲什麼此代碼使用多個框架?看看[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/a/9554657/418556) –

回答

0

您使用佈局管理器的一個問題。每次向JFrame添加組件時,都會覆蓋已有的組件。我對生命遊戲不太熟悉,但我不認爲你希望你的細胞成爲JComponents。您可以代表您的主板以布爾數組,代表着活着和死的狀態,這樣你的視覺構造變爲:

public Visuals(Board b) { 
    super("The Game of Life"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Container container = getContentPane(); // 1 
    JPanel panel = new JPanel(new FlowLayout()); // 2 
    nextGen = new JButton("Next Generation"); 
    panel.add(nextGen); 
    nextGen.addActionListener(this); 

    random = new JButton("Random"); 
    panel.add(random); 
    random.addActionListener(this); 
    container.add(panel); // 3 

    setSize(800, 700); 
    setVisible(true); 

} 

在你的主板類,你將代表董事會作爲數組:

public class Board{ 
    private boolean[][] board; 

    public Board(int x, int y) { 

     board = new boolean[x][y]; 
    } 
} 

然後在GameOfLife類的nextGeneration方法中,執行您正在執行的任何計算並設置板上位置的真/假(或活/死)狀態。

你Visuals.paint方法就變成這樣的:

public void paint(Graphics g) { 
    super.paint(g); 
    for (int i = 0, yShift = 80; i < b.getRow(); i++, yShift += 20) { 
     for (int j = 0, xShift = 30; j < b.getCol(); j++, xShift += 20) { 
      if (b.isAlive(i, j)) { 
       g.setColor(Color.red); 
       g.fillRect(xShift, yShift, 10, 10); 
      } else { 
       g.setColor(Color.white); 
       g.fillRect(xShift, yShift, 10, 10); 
      } 
     } 
    } 
} 

iaAlive在該位置剛好返回板陣列的價值:

public boolean isAlive(int i, int j) { 
    return board[i][j]; 
} 

但是就像我說的,我不熟悉遊戲。

+0

謝謝大家,我用GridLayout解決了我的問題,所以我甚至不必擔心關於我的組件的x和y。順便說一句羅伯特,我用JComponents來爲它們添加鼠標監聽器。 – user1874103