2013-07-08 2 views
1

나는 하나의 주 GamePanel과 더불어, JFrame 있습니다내 JPanel에서 내 자식 JPanels가 이동하거나 잘못 배치되는 이유는 무엇입니까?

import javax.swing.*; 

public class GameFrame extends JFrame { 

    public static void main(String[] args) { 

     JFrame gameFrame = new JFrame("Ultimate Tic Tac Toe"); 

     gameFrame.setResizable(false); 
     gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gameFrame.add(new GamePanel()); 
     gameFrame.pack(); 
     gameFrame.setVisible(true); 

    } 
} 

의 GamePanel은 BorderLayout을 가지고 있으며, 나는 setPreferredSize(new Dimension(800,800)) 있습니다

공용 클래스의 GamePanel가 JPanel의가에서의 ActionListener {

private BoardPanel boardPanel; //the main game, comprised of minigames 
private static TrackingPanel trackingPanel; //keeps track of score, turn, and stuff 
private ArrayList<MiniGame> miniGames; 

public GamePanel() { 

    super(new BorderLayout()); 
    setFocusable(true); 
    setPreferredSize(new Dimension(800,800)); 

    //create panel that will hold the 9 mini games 
    boardPanel = new BoardPanel(this); 

    //add actionListeners to each button 
    miniGames = boardPanel.getMiniGames(); 
    for (MiniGame mini : miniGames) { 
     for (SquareButton button : mini.getSquares()) 
      button.addActionListener(this); 
    } 

    add(trackingPanel, BorderLayout.NORTH); 
    add(boardPanel, BorderLayout.CENTER); 
}//end constructor 

를 구현 확장을 CENTER는 BoardPanel이고 GridLayout(3,3)이므로 BoardPanel에 9 개의 하위 MiniGame 01이 있습니다.. 여기 내 문제가 있습니다 : 아래와 같은 MiniGames를 추가하는 것은 정상적으로 작동했지만 아무 이유없이 그들은 부모 주위에 밀려 나와 BoardPanel 및 창 밖으로 표시됩니다. 이상적으로는 레이아웃을 변경하지 않고이 작업을 수행하고 싶습니다. BoardPanel의 코드는 다음과 같습니다.

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

public class BoardPanel extends JPanel { 

private ArrayList<MiniGame> miniGames; 

public BoardPanel(GamePanel gp) { 
    super(new GridLayout(3,3)); 
      setBorder(BorderFactory.createEmptyBorder(200,200,200,200)); 

    miniGames = new ArrayList<MiniGame>(9); 
    for(int i = 1; i<=9; i++) 
     miniGames.add(new MiniGame(gp, i)); 

    for(MiniGame mini : miniGames) 
     add(mini); 
} 

나는 패널 모두 추가되는 알고 (내가 그들의 국경 두께를 변화에 의해 확인. 또한, 빈 국경 크기가 미니 게임 패널의 이상을 알 수 증가) 표시됩니다. 그래서, 대신 격자와 같은 디스플레이의 : 나머지는 부모 패널 외부에있는

1 
    2 
    3 
     ... 

:

1 2 3 
4 5 6 
7 8 9 

그들은 아래로 대각선으로 표시하고 있습니다. 그 도움이된다면

다음은 미니 게임 생성자의 모든

public MiniGame(GamePanel gp, int num) { 

    super(new GridLayout(3,3)); 
    setFocusable(true); 

    trackingPanel = gp.getTrackingPanel(); 
    panelNum = num; 

    if(panelNum==1) 
     setBorder(BorderFactory.createMatteBorder(0,0,2,2,Color.BLACK)); 
    else if(panelNum==2) 
     setBorder(BorderFactory.createMatteBorder(0,2,2,2,Color.BLACK)); 
    else if(panelNum==3) 
     setBorder(BorderFactory.createMatteBorder(0,2,2,0,Color.BLACK)); 
    else if(panelNum==4) 
     setBorder(BorderFactory.createMatteBorder(2,0,2,2,Color.BLACK)); 
    else if(panelNum==5) 
     setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK)); 
    else if(panelNum==6) 
     setBorder(BorderFactory.createMatteBorder(2,2,2,0,Color.BLACK)); 
    else if(panelNum==7) 
     setBorder(BorderFactory.createMatteBorder(2,0,0,2,Color.BLACK)); 
    else if(panelNum==8) 
     setBorder(BorderFactory.createMatteBorder(2,2,0,2,Color.BLACK)); 
    else 
     setBorder(BorderFactory.createMatteBorder(2,2,0,0,Color.BLACK)); 

    squares = new SquareButton[9]; 

    //create squares and add squares to mini game 
    for (int i = 0; i < squares.length; i++) { 
     squares[i] = new SquareButton(i); 
     add(squares[i]); 
    } 
    } 

감사합니다!

+1

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –

+0

뒤로 작업 나는 getX()와 getY()를 호출했다는 것을 깨달았다. 나는 그것들이 JPanels의 기본 함수라고 생각하고 그것을 재정의했다. –

답변

1

거꾸로 작동 내가 getX() 및 getY()라고 부른 두 가지 기능임을 깨달았습니다. 나는 그것들이 JPanels의 기본 함수라고 생각하고 그것을 재정의했다.

+0

다행 이군. :) –

0

"MiniGame 생성자"는 {add (squares [i]);}를 호출하지만 MiniGame에서 추가 작업을 수행하지 않음을 보여줍니다. 모든 코드를 표시하지 않기 때문에 확장 된 코드를 알 수 없습니다. 자체적으로 UI 클래스 인 경우 레이아웃 관리자가 해당 컨테이너에서 활성화되어있을 가능성이 큽니다.

+0

죄송합니다. 더 많은 코드를 포함해야합니다. MiniGame은 JPanel을 확장하므로 버튼을 패널에 추가하는 중이었습니다. –