确定。所以我应该为我的connect 4程序构建一个GUI。 (请原谅这里和那里的代码块)我已经缩短了这个程序供您阅读,我已经排除了一些没有问题的代码。我试图让我的JPanel进入Connect4()。如何访问公共TestPane(),以便我可以从Connect4()更新GUI。
我不允许使用任何静态内容。 LINE 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();
}
}
答案 0 :(得分:1)
在添加/删除/更改其上的任何元素后,尝试在JPanel上重绘()+ revalidate()。还可以在创建桌面JAVA应用程序时阅读有关MVC和最佳实践的内容。