2016-03-23 16 views
0

Tamam ile değil. Bu yüzden, connect 4 programım için bir GUI oluşturmam gerekiyor. (Lütfen kod parçalarını burada ve orada kullanmayın). Bu programı okumanız için kısalttım ve sorunları olmayan bazı kodları hariç tuttum. JPanel'imi Connect4() 'e indirmeye çalışıyorum. Genel TestPane() 'e nasıl erişebilirim, böylece GUI'yi Connect4'den() güncelleyebilirim.Programım için ilk GUI (JPanel) yapıyorum ve çok zor bir zaman geçiriyorum. GUI çalışır ancak Programım

Statik bir şey kullanmasına izin verilmez. Eğer/kaldırıldı eklendi/Üzerinde herhangi bir unsur değiştirdikten sonra SATIR 153 Ben Jpanel üzerinde() Kullanıcı arabirimi

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" öğesini nerede oluşturdunuz? Lütfen bu bölümü paylaşın. – rdonuk

cevap

1

deneyin rötuş() GÜNCELLENECEK + revalidate DENEMEYİN gidiyordu NEREDE İDİ. Ayrıca masaüstü JAVA uygulaması oluştururken MVC ve en iyi uygulamalar hakkında bilgi edinin.