2013-04-18 82 views
0

我遇到了麻煩,我的表單有一個網格按鈕出現。該表格應該顯示一個將用作簡單戰艦遊戲平臺的按鈕網格。Java Swing Button沒有出現在表單

這是小組玩家電網

package view; 

import java.util.ArrayList; 
import java.util.List; 

import javax.swing.JPanel; 

public class PlayerPanel extends JPanel { 

    private List<PositionButton> ButtonList; 


    public PlayerPanel() 
    { 

     ButtonList = new ArrayList<PositionButton>(); 
     for(int i = 0;i < 10;i++) 
      for(int j = 0;j < 10;j++) 
      { 
       ButtonList.add(new PositionButton(i,j)); 
      } 
     while(ButtonList.iterator().hasNext()) 
     { 
      this.add(ButtonList.iterator().next()); 
     } 
     while(ButtonList.iterator().hasNext()) 
     { 
      ButtonList.iterator().next().setVisible(true); 
     } 
    } 

} 

代碼這是實際的表單類:

package view; 

import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class GameForm { 


    private JFrame theFrame; 
    private JPanel BoardPanel; 
    private PlayerPanel p1Panel; 
    private PlayerPanel p2Panel; 



    public GameForm() 
    { 
     //set up the Frame 
     theFrame = new JFrame(); 
     theFrame.setSize(new Dimension(800,500)); 
     theFrame.setVisible(true); 
     theFrame.setResizable(false); 
     theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //set up the board panel 
     BoardPanel = new JPanel(); 
     theFrame.add(this.BoardPanel); 
     //set up the first players board 
     p1Panel = new PlayerPanel(); 
     p1Panel.setLayout(new GridLayout(0,0)); 
     this.BoardPanel.add(p1Panel); 
     p1Panel.setSize(new Dimension(400,250)); 
     //set up the second players boards 
     p2Panel = new PlayerPanel(); 
     p2Panel.setLayout(new GridLayout(0,0)); 
     this.BoardPanel.add(p2Panel); 
     p2Panel.setSize(new Dimension(400,250)); 

     BoardPanel.setVisible(true); 
     p1Panel.setVisible(true); 
     p2Panel.setVisible(true); 
    } 
} 

這是對個人按鈕

代碼
package view; 

import javax.swing.JButton; 

public class PositionButton extends JButton {  

    private int x; 
    private int y;  

    public PositionButton(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
     this.setVisible(true); 
    } 


    public int getX() 
    { 
     return this.x; 
    } 

    public int getY() 
    { 
     return this.y; 
    }    
} 

我很努力地找出爲什麼我的按鈕沒有出現。提前謝謝你的幫助。使用

+1

爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 (它應該是一個源文件,其中包括一個'main(String [])') –

回答

1

一個與主要問題代碼是PositionButton是覆蓋getX()getY()。看起來你希望使用這些方法來確定GridLayout中按鈕的座標,但你會來命名它們。例如。 getXCoord()

試試看這個SSCCE的提示。

import java.awt.*; 
import javax.swing.*; 
import java.util.ArrayList; 

public class GameForm { 

    private JFrame theFrame; 
    private JPanel BoardPanel; 
    private PlayerPanel p1Panel; 
    private PlayerPanel p2Panel; 

    public static void main(String[] args) { 
     new GameForm(); 
    } 

    public GameForm() 
    { 
     //set up the Frame 
     theFrame = new JFrame(); 
     //set up the board panel 
     BoardPanel = new JPanel(new BorderLayout()); 
     BoardPanel.setBackground(Color.BLUE); 
     theFrame.add(this.BoardPanel); 
     //set up the first players board 
     p1Panel = new PlayerPanel(); 
     p1Panel.setBackground(Color.RED); 
     this.BoardPanel.add(p1Panel); 

     theFrame.setSize(new Dimension(800,500)); 
     theFrame.setVisible(true); 
     theFrame.setResizable(false); 
     theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

class PlayerPanel extends JPanel { 

    private java.util.List<PositionButton> ButtonList; 

    public PlayerPanel() 
    { 

     ButtonList = new ArrayList<PositionButton>(); 
     for(int i = 0;i < 10;i++) 
      for(int j = 0;j < 10;j++) 
      { 
       PositionButton pb = new PositionButton(i,j); 
       ButtonList.add(pb); 
       add(pb); 
      } 
    } 
} 

class PositionButton extends JButton { 

    private int x; 
    private int y; 

    public PositionButton(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
     this.setVisible(true); 
    } 


    public int getXCoord() 
    { 
     return this.x; 
    } 

    public int getYCoord() 
    { 
     return this.y; 
    } 
} 
+0

非常感謝,這個答案幫助了我很多,現在我的UI已經完全運行。 –