Gui bliver ikke opdateret korrekt
HejsaJeg prøver at lave en gui til et TicTacToe spil. Mit problem er at der intet kommer frem på gui'en når et nyt Game bliver lavet. Der kommer først noget frem når jeg maksimere eller minimere gui framen. Og når en label på gui'en ændre sig, så skal jeg igen mini eller maksimere for at få det til at virke.
Er sikker på at det er et eller andet simpelt jeg har misset, da jeg er lidt ny i GUI. Tror jeg har prøvet at placere repaint() over alt i gui'en uden noget held.
Jeg bruger awt ikke swing, så svaret skal helst være awt.
Ved ikke helt hvilken del af koden der bedst kan bruges til at hjælpe med at besvarer mit problem, så jeg poster hele koden her.
På forhånd tak
--------------------------
import java.awt.*;
import java.awt.event.*;
import TicTacToe.*;
public class Gui extends Frame implements WindowListener, ItemListener,
ActionListener{
String [] userInput = null;
TicTacToe ttt=null;
boolean correctSymbol;
MenuBar mainMenuBar;
Menu gameMenu,optionMenu;
MenuItem newGame,exitGame;
CheckboxMenuItem displayPlayerLabel;
MainWindow mainWindow = new MainWindow("TicTacToe");
OutputBox outputBox = new OutputBox(mainWindow, "TicTacToe");
String[] playerInfoBoxStrings = { "Player One Name", "Player Two Name", "Player One Symbol (X or Y)"};
MultiInputBox playerInfoBox = new MultiInputBox(mainWindow,playerInfoBoxStrings);
Button[][] p2Buttons = new Button[3][3];
String playerOneName, playerTwoName, playerOneSymbol, playerTwoSymbol;
Label topLabel,nextPlayerLabel;
Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER));
Panel p2 = new Panel(new GridLayout(3,3,10,10));
Panel p3 = new Panel(new GridLayout(3, 3));
private void setupMenus(){
displayPlayerLabel = new CheckboxMenuItem("Display Players", true);
optionMenu = new Menu("Option");
optionMenu.add(displayPlayerLabel);
newGame = new MenuItem("New");
exitGame = new MenuItem("Exit");
gameMenu = new Menu("Game");
gameMenu.add(newGame);
gameMenu.add(exitGame);
mainMenuBar = new MenuBar();
mainMenuBar.add(gameMenu);
mainMenuBar.add(optionMenu);
this.setMenuBar(mainMenuBar);
gameMenu.addActionListener(this);
displayPlayerLabel.addItemListener(this);
addWindowListener(this);
}
private void topLabel(){
topLabel = new Label("IT IS YOUR TURN");
nextPlayerLabel = new Label(ttt.getNextPlayer()+":");
nextPlayerLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,18));
p1.add(nextPlayerLabel);
p1.add(topLabel);
addWindowListener(this);
}
private void createNewGameBoard(){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
p2Buttons[i][j] = new Button();
p2Buttons[i][j].setActionCommand(String.valueOf(3 * i+j));
p2Buttons[i][j].setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 18));
p2.add(p2Buttons[i][j]);
p2Buttons[i][j].addActionListener(this);
}
}
}
private void initplayerPanel(){
playerOneName = ttt.getPlayerOneName();
playerTwoName = ttt.getPlayerTwoName();
playerOneSymbol = (ttt.getPlayerOneSymbol());
playerTwoSymbol = (ttt.getPlayerTwoSymbol());
Label playerLabel = new Label("PLAYER");
playerLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 18));
Label playerOneLabel = new Label("One");
Label playerTwoLabel = new Label("Two");
Label nameLabel= new Label("Name");
nameLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 18));
Label nameOneLabel= new Label(playerOneName);
Label nameTwoLabel= new Label(playerTwoName);
Label symbolLabel = new Label("Symbol");
symbolLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 18));
Label symbolOneLabel = new Label(playerOneSymbol);
Label symbolTwoLabel = new Label(playerTwoSymbol);
p3.add(playerLabel);
p3.add(nameLabel);
p3.add(symbolLabel);
p3.add(playerOneLabel);
p3.add(nameOneLabel);
p3.add(symbolOneLabel);
p3.add(playerTwoLabel);
p3.add(nameTwoLabel);
p3.add(symbolTwoLabel);
}
Gui(String title, int width, int height){
super(title);
this.setLayout(new GridLayout(3,1));
this.setSize(width,height);
this.setupMenus();
this.add(p1);
this.add(p2);
this.add(p3);
this.addWindowListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Exit")){
System.exit(0);
}else if(e.getActionCommand().equals("New")){
correctSymbol = false;
do {
try {
userInput = playerInfoBox.getInputs();
ttt = new TicTacToe(userInput[0], userInput[1], Convert.toChar(userInput[2]));
outputBox.setVisible(false);
p1.removeAll();
p2.removeAll();
p3.removeAll();
topLabel();
createNewGameBoard();
initplayerPanel();
p2.setEnabled(true);
correctSymbol = true;
}catch (Exception f) {
outputBox.setVisible(true);
outputBox.printLine(f.toString());
}
}while(!correctSymbol);
} else if (e.getActionCommand().equals("0")){
try{
p2Buttons[0][0].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(1, 1);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
}else if (e.getActionCommand().equals("0")){
try{
p2Buttons[0][0].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(1, 1);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("1")){
try{
p2Buttons[0][1].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(1, 2);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("2")){
try{
p2Buttons[0][2].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(1, 3);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("3")){
try{
p2Buttons[1][0].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(2, 1);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("4")){
try{
p2Buttons[1][1].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(2, 2);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("5")){
try{
p2Buttons[1][2].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(2, 3);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("6")){
try{
p2Buttons[2][0].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(3, 1);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("7")){
try{
p2Buttons[2][1].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(3, 2);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
} else if (e.getActionCommand().equals("8")){
try{
p2Buttons[2][2].setLabel(ttt.getNextPlayerSymbol());
ttt.playAHand(3, 3);
}catch (Exception f){
outputBox.setVisible(true);
outputBox.printLine(f.toString()); }
}
if(ttt.winnerHand()){
nextPlayerLabel = new Label("CONGRATULATION! " + ttt.getLastPlayer() +
" YOU ARE THE WINNER");
nextPlayerLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,18));
nextPlayerLabel.setForeground(Color.red);
p1.removeAll();
p1.add(nextPlayerLabel);
this.repaint();
p2.setEnabled(false);
}
else if (ttt.tieGame()){
nextPlayerLabel = new Label("THIS GAME RESULTED IN A TIE");
nextPlayerLabel.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,18));
nextPlayerLabel.setForeground(Color.red);
p1.removeAll();
p1.add(nextPlayerLabel);
this.repaint();
}
nextPlayerLabel = new Label(ttt.getNextPlayer());
nextPlayerLabel.repaint();
this.repaint();
}
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED){
if(e.getSource()== displayPlayerLabel){
p3.setVisible(true);
}
}else{
if(e.getSource()== displayPlayerLabel){
p3.setVisible(false);
}
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}
