简体   繁体   English

为我的程序构建我的第一个GUI(JPanel)并经历了如此艰难的时光。 GUI可以使用,但不适用于我的程序

[英]Building my first GUI(JPanel) for my program and having such a hard time. GUI works but not with my Program

Ok. 好。 So I'm supposed to build a GUI for my connect 4 program. 因此,我应该为我的connect 4程序构建一个GUI。 (Please excuse the chunks of code here and there) I have shortened this program for your reading and I have excluded some code that doesn't have issues. (请原谅此处的代码块)为了您的阅读,我已缩短了该程序的时间,并排除了一些没有问题的代码。 I'm trying to get my JPanel down to the Connect4(). 我试图将我的JPanel降到Connect4()。 How can I access public TestPane() so I can update the GUI from Connect4(). 如何访问公共TestPane(),以便可以从Connect4()更新GUI。

I am not allowed to use anything Static. 我不允许使用任何静态的东西。 LINE 153 WAS WHERE I WAS GOING TO ATTEMPT TO UPDATE THE GUI 153行是我要尝试更新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();
  }
}

Try repaint() + revalidate() on JPanel after you added/removed/changed any element on it. 在添加/删除/更改其上的任何元素后,请尝试在JPanel上尝试repaint()+ revalidate()。 Also read about MVC and best practices when creating desktop JAVA app. 在创建桌面JAVA应用程序时,还请阅读有关MVC和最佳做法的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM