简体   繁体   English

按钮背景不变

[英]Button background not changing

I am trying to make a reversi GUI and I am trying to change the buttons colors with an if loop. 我正在尝试制作一个reversi GUI,我试图用if循环改变按钮颜色。 However, when it runs over this code it doesn't change the color: 但是,当它在此代码上运行时,它不会更改颜色:

if(y.board[i][j - 1] == 2)
{
    y.board[i][j] = 1;
    butArray[i][j].setBackground(Color.yellow);
    y.board[i][j - 1] = 1;
    butArray[i][j - 1].setBackground(Color.yellow);
    System.out.println("4");
}

This is my JButton declaring part 这是我的JButton声明部分

for(l=0; l < butArray.length; l++)
{
    for(y=0; y <butArray[l].length; y++)
    {
        butArray[l][y] = new JButton("Xg");
        butArray[l][y].addActionListener(this);
        butArray[l][y].setBackground(Color.white);;
        butArray[l][y].setOpaque(true);
        buttons.add(butArray[l][y]);
    }
}

Here is my full main code 这是我的完整主要代码

/*
 * Jesse Richards
 * CMSC 112
 */

import java.awt.Color;
import java.util.Scanner;

public class Reversi {
    int playerturn;
    int counter;
    int[][] board = new int[8][8];
    Scanner keyboard = new Scanner(System.in);
    int playerOnePoints;
    int playerTwoPoints;


//getting things set
public void setplayerturn(){
        int i;

    if (counter % 2 == 0){
        playerturn = 1;
    }
    else{
        playerturn = 2;
    }
}
//setting arrays
public void setArrays(){
    int i;
    int j;
    for(i = 0;i<8;i++){
        for(j = 0;j<8;j++){
        board[i][j] = 0;

        }
    }

}

//gets what player is going



public void printBoard(){
    int i,j;
    for(i = 0;i<8;i++){
        for(j = 0;j<8;j++){

            System.out.print(board[i][j]);
            System.out.print(" ");
        }
        System.out.println("");
    }


}




public void points(){
    int i,j;
    for(i = 0;i<8;i++){
        for(j = 0;j<8;j++){
            if(board[i][j] == 1){
                playerOnePoints++;
            }
            else{
                playerTwoPoints++;
            }
        }
    }
}

} }

and full main driver 和充分的主要驱动力

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;


public class ReversiDriver extends JFrame implements ActionListener {

    private JPanel score;
    private JPanel buttons;
    JButton[][] butArray = new JButton[8][8];

    public static void main(String[] args) {
        ReversiDriver gui = new ReversiDriver();
        gui.setVisible(true);


        // TODO Auto-generated method stub
        Reversi x = new Reversi();
        int i;
        x.setplayerturn();
        x.setArrays();



//      for(i = 0;i<64;i++){
//          
//          
//          x.printBoard();
//          
//          x.counter++;
//          
//          
//          System.out.println("");
//      }


        x.points();
        System.out.println("Player one has " + x.playerOnePoints +" Points");
        System.out.println("Player two has " + x.playerTwoPoints +" Points");

        if(x.playerOnePoints>x.playerTwoPoints){
            System.out.println("Player One Wins!");

        }   else {
            System.out.println("Player Two Wins!");
            }
        }

    @Override
    public void actionPerformed(ActionEvent e) {

        System.out.println("BUG");
        //importing methods
        Reversi x = new Reversi();
        int i;
        int j;
        int l,y;
        // TODO Auto-generated method stub
        //if-else
        //e.getSource() --> return the name of button

        for(l=0; l < butArray.length; l++){
            for(y=0; y <butArray[l].length; y++){
                if(e.getSource() == butArray[l][y]){
                    //button was clicked1
                    playerOneTurn(l,y);

                }
            }
        }

        //e.getactionCommand()

        String but = e.getActionCommand();

    }
    public boolean playerOneTurn(int i, int j){
        int x;

        Reversi y = new Reversi();

        y.board[3][3] = 1;
        y.board[3][4] = 2;
        y.board[4][3] = 2;
        y.board[4][4] = 1;

        if(y.board[i - 1][j] == 0){
            y.board[i][j] = 1;
            butArray[i][j].setBackground(Color.yellow);
            y.board[i -1][j] = 1;
            butArray[i - 1][j].setBackground(Color.yellow);
            System.out.println("1");
        }
        if(y.board[i + 1][j] == 2){
            y.board[i][j] = 1;
            butArray[i][j].setBackground(Color.yellow);
            y.board[i + 1][j] = 1;
            butArray[i + 1][j].setBackground(Color.yellow);
            System.out.println("2");
        }
        if(y.board[i][j + 1] == 2){
            y.board[i][j] = 1;
            butArray[i][j].setBackground(Color.yellow);
            y.board[i][j + 1] = 1;
            butArray[i][j + 1].setBackground(Color.yellow);
            System.out.println("3");
        }
        if(y.board[i][j - 1] == 2){
            y.board[i][j] = 1;
            butArray[i][j].setBackground(Color.yellow);
            y.board[i][j - 1] = 1;
            butArray[i][j - 1].setBackground(Color.yellow);
            System.out.println("4");
        }

        if(y.board[i][j - 1] == 2){
            y.board[i][j] = 1;
            butArray[i][j].setBackground(Color.yellow);
            y.board[i][j - 1] = 1;
        butArray[i][j - 1].setBackground(Color.yellow);
            System.out.println("5");
        }
        System.out.println("JEse");

        return false;

    }
    public ReversiDriver(){
        super("Menu Demonstration");
        setSize(400, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2, 2));

        score = new JPanel();
        score.setBackground(Color.black);
        add(score,BorderLayout.SOUTH);

        buttons = new JPanel();
        add(buttons, BorderLayout.NORTH);



        int y;
        int l;

        //arrange buttons
        for(l=0; l < butArray.length; l++){
            for(y=0; y <butArray[l].length; y++){
                butArray[l][y] = new JButton("Xg");
                butArray[l][y].addActionListener(this);
                butArray[l][y].setBackground(Color.white);;
                butArray[l][y].setOpaque(true);
                buttons.add(butArray[l][y]);
            }
        }

        //set four start buttons
        butArray[3][3].setBackground(Color.yellow);
        butArray[3][4].setBackground(Color.blue);
        butArray[4][3].setBackground(Color.blue);
        butArray[4][4].setBackground(Color.yellow);


    }
}

You're creating multiple instances of Reversi , Reversi x = new Reversi() , which means that when you inspect the state of the game, it's always at its default state. 你正在创建Reversi多个实例, Reversi x = new Reversi() ,这意味着当你检查游戏的状态时,它总是处于默认状态。

You seem to be confused over the difference of how a console (linear) and GUI based (event driven) program works. 您似乎对控制台(线性)和基于GUI(事件驱动)程序的工作方式的不同感到困惑。

Start by creating a single instance of your Reversi as an instance field of your class. 首先创建一个Reversi的单个实例作为类的实例字段。 When some state change event occurs, use this instance to determine how the UI or model should be updated 发生某些状态更改事件时,请使用此实例确定应如何更新UI或模型

I do not know in what part of the execution flow you intend to change the background color of a button, but at first sight, you code looks correct.. because the method setBackground color do exactly what it states. 我不知道您打算在哪个部分执行流程中更改按钮的背景颜色,但乍一看,您的代码看起来是正确的...因为方法setBackground颜色完全按照它所声明的方式执行。

Are you trying to set the background in a callback of any kind of listener of a component (Using the Event Dispatch Thread) ? 您是否尝试在组件的任何类型的侦听器的回调中设置背景(使用事件调度线程)? or you are setting in another thread? 或者你正在设置另一个线程?

Also be sure that the component isOpaque property is set to true. 还要确保组件isOpaque属性设置为true。 (set thought public void setOpaque(boolean isOpaque) mutator method). (设置思想public void setOpaque(boolean isOpaque) mutator方法)。 Sometimes this can be the problem. 有时这可能是问题所在。

Hope it helps! 希望能帮助到你!

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

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