Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按钮背景不改变_Java_Swing_Colors_Background_Jbutton - Fatal编程技术网

Java 按钮背景不改变

Java 按钮背景不改变,java,swing,colors,background,jbutton,Java,Swing,Colors,Background,Jbutton,我正在尝试制作一个reversi GUI,并尝试使用if循环更改按钮的颜色。但是,当它在该代码上运行时,不会更改颜色: 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.

我正在尝试制作一个reversi GUI,并尝试使用if循环更改按钮的颜色。但是,当它在该代码上运行时,不会更改颜色:

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");
}
这是我的
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]);
    }
}
for(l=0;l对于(y=0;y我不知道您打算在执行流的哪个部分更改按钮的背景色,但乍一看,您的代码看起来是正确的..因为方法setBackground color完全按照它的说明执行

您是试图在组件的任何类型的侦听器的回调中设置背景(使用事件分派线程)还是在另一个线程中设置

还要确保component
isOpaque
属性设置为true。(设置为public void
set不透明(布尔isOpaque)
mutator方法)。有时这可能是问题所在


希望有帮助!

您正在创建多个
Reversi
Reversi x=new Reversi()
,这意味着当您检查游戏状态时,它始终处于默认状态

您似乎对控制台(线性)和基于GUI(事件驱动)的程序的工作方式的不同感到困惑


首先创建
Reversi
的单个实例作为类的实例字段。当发生某些状态更改事件时,使用此实例确定UI或模型应如何更新

可能我缺少一些明显的内容,但我不知道顶层代码块应该做什么,它位于何处,或者如何更新这与你的问题有关。考虑张贴更多的信息和相关的代码,但是在这样做之前,从我们的角度考虑你的问题:没有线索的人,你的程序看起来什么样。只是添加了完整的代码,谢谢你的帮助。是的,我试图改变按钮的颜色,点击它时,它符合标准。所有按钮在开始时都设置为不透明。您的意思是像用户在第二个代码段中使用“butArray[l][y].setOpaque(true)”来初始化按钮一样?
/*
 * 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++;
            }
        }
    }
}
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);


    }
}