2016-03-23 5 views
0

Ok。だから私は私のconnect 4プログラムのためのGUIを構築するはずです。 (私はあなたの読書のためにこのプログラムを短縮し、私は問題のないいくつかのコードを除外しました。 JPanelをConnect4()にダウンさせようとしています。 Connect4()からGUIを更新できるように、私はどのようにしてPublic TestPane()にアクセスできますか?私のプログラムのための私の最初のGUI(JPanel)を構築し、そのような苦労している。 GUIは動作しますが、私のプログラムでは動作しません

静的なものは使用できません。追加した後、私はGUI

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 
import javax.swing.border.MatteBorder; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 
import java.awt.GridLayout; 

public class Connect4 extends JPanel{ 



public boolean col1 = false; 
public int buttonPressed = 10; 


public class TestPane extends JPanel{ 

    public TestPane() { 
     setLayout(new GridBagLayout()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     for (int row = 0; row < 7; row++) { 
      for (int col = 0; col < 8; col++) { 
       //setBackground(Color.BLUE); 
       gbc.gridx = col; 
       gbc.gridy = row; 

       CellPane cellPane = new CellPane(); 
       Border border = null; 
       if (row < 7) { 
        if (col < 8) { 
         border = new MatteBorder(1, 1, 1, 1, Color.BLACK); 

        } else { 
         border = new MatteBorder(1, 1, 1, 1, Color.BLACK); 
        } 
       } else { 
        if (col < 8) { 
         border = new MatteBorder(1, 1, 1, 1, Color.BLACK); 
        } else { 
         border = new MatteBorder(1, 1, 1, 1, Color.BLACK); 
        } 
       } 
       cellPane.setBorder(border); 


       add(cellPane, gbc); 

      } 

//JUST TO TEST THAT THE SQUARES ARE WORKING. THEY ARE BUT NOT WITH MY PROGRAM 
      gbc.gridx = 3; 
      gbc.gridy = 2; 
      CellPane cellPaneP1 = new CellPane(); 

      cellPaneP1.setBackground(Color.BLUE); 
      add(cellPaneP1,gbc); 

      gbc.gridx = 5; 
      gbc.gridy = 4; 
      CellPane cellPaneP2 = new CellPane(); 

      cellPaneP2.setBackground(Color.RED); 
      add(cellPaneP2,gbc); 

     } 

    } 
} 



public Connect4(){ 
     JOptionPane.showMessageDialog(null, "Welcome to Connect Four\nThis game will require 2 players\nPlease Enter your names"); 
    Player p1 = new Player(); 
    Player p2 = new Player(); 

    String p1Name = JOptionPane.showInputDialog("Enter your name Player 1"); 


    p1.setName(p1Name); 

    String p2Name = JOptionPane.showInputDialog("Enter your name Player 2"); 

    p2.setName(p2Name); 
    JOptionPane.showMessageDialog(null,p1.getName()+ " vs " + p2.getName()+". \nThis is going to be EPIC!!!"); 
    System.out.println(p1.getName()+ " vs " + p2.getName()+". \nThis is going to be EPIC!!!"); 

    int winner =0; 

    //Create Our board 
    Board con4Bor= new Board(); 
    //con4Bor.setSize(7,8);] 

    //Fill our board with '_' to represent empty spaces 
    con4Bor.fillBoard(); 

    //Randomly Select Player to go first 
    int i = 0; 
    int p1p2 = (int)(Math.random()*2+1); 
    if(p1p2 == 1) 
     System.out.println(p1.getName() + " was selected at random to go first"); 
    else 
     System.out.println(p2.getName() + " was selected at random to go first"); 

    JButton column1 = new JButton(new AbstractAction("Column 1"){ 

     @Override 
     public void actionPerformed(ActionEvent a){ 
      buttonPressed = 1; 
     } 

    }); 

    while(winner == 0){ 
     if(p1p2 == 3){ 
     p1p2--; 
     } 
     con4Bor.printOutBoard(); 
     //printDiag(c4b); 


     int playerSelection = 10; 
     //System.out.println(p1p2); 
     if(p1p2 == 1){ 
     System.out.println(p1.getName()+": it is now your turn\nplease choose a column"); 
     } 
     else{ 
     System.out.println(p2.getName()+": it is now your turn\nplease choose a column"); 
     } 

     System.out.println("Which Column do you want to insert? Column 1, 2, 3, 4, 5, 6, 7 or 8?"); 

     playerSelection = 1; 

     while(playerSelection != 1 && buttonPressed != 2 && buttonPressed != 3 && buttonPressed != 4 && buttonPressed != 5 && buttonPressed != 6 && buttonPressed != 7 && buttonPressed != 8){ 
      System.out.println(buttonPressed); 
      playerSelection = 1;//buttonPressed; 

     } 


     if(playerSelection == 1){ 
******************************************************************************** 
******************************************************************************** 
***This is where I was poorly attempting to update my GUI if someone selected column 1   *** 
******************************************************************************** 
******************************************************************************** 
      i = 0; 
      con4Bor.insertCol(i, playerSelection-1, p1p2); 

     } 


     //WINNER DETECTION 
      if(p1p2 == 1){   
      if(con4Bor.weHaveAHorizontalWinner() == true || con4Bor.weHaveAVeritcalWinner() == true || con4Bor.weHaveADiagonalWinner()==true){ 
       con4Bor.printOutBoard(); 
       System.out.println(p1.getName()+" Wins!!!"); 
       winner++; 
       }else{ 
        p1p2 =3; 
       } 
      } 

      if(p1p2 == 2){ 
       if(con4Bor.weHaveAHorizontalWinner() == true || con4Bor.weHaveAVeritcalWinner() == true || con4Bor.weHaveADiagonalWinner()==true){ 

        con4Bor.printOutBoard(); 
        System.out.println(p2.getName()+" Wins!!!"); 
        winner++; 
       }else{ 
        p1p2--; 
       } 
      } 
     } 
} 

public class CellPane extends JPanel { 

    private Color defaultBackground; 

    public CellPane() { 
     defaultBackground = getBackground(); 
     } 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(100, 100); 
    } 
} 

public static void main (String [] args){ 
     new tester(); 
    } 
} 
+0

あなたは 'TestPane'をどこで作成しましたか?その部分も共有してください。 – rdonuk

答えて

1

てみ再描画を(更新しようとするつもりは)+(再検証された場所 LINE 153はJPanelの上)WAS /削除/その上で任意の要素を変更しました。また、デスクトップJavaアプリケーションを作成する際のMVCとベストプラクティスについても読んでください。

関連する問題