我只对java足够新.我想用6种不同的颜色填充6x6网格,而不会在同一行或列中出现相同的颜色.在我的代码中,我设置了一个存储在名为buttons的数组中的6x6 JButtons网格.当我按下其中一个JButton时,设置了一个名为paintBox的6x1 JButtons网格.paintBox中的JButtons在程序的顶部声明为fillRed,fillYellow等.当我按下fillRed时,它将从6x6网格红色设置JButton的后轮,但是当我从6x6网格中按下不同的JButton并尝试设置黄色它将它设置为黄色,但也设置原来的JButton设置为红色到黄色.任何帮助都会很棒.谢谢
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Grid4 extends JFrame implements ActionListener
{
private ColourGrid paintBox = null;
private JButton fillRed = new JButton("Red");
private JButton fillYellow = new JButton("Yellow");
private JButton fillBlue = new JButton("Blue");
private JButton fillGreen = new JButton("Green");
private JButton fillPurple = new JButton("Purple");
private JButton fillBrown = new JButton("Brown");
private JButton[] paintButton = {fillRed,fillYellow,fillBlue,fillGreen,fillPurple,fillBrown};
private Color[] colours = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0)};
public …Run Code Online (Sandbox Code Playgroud) 当我按下其中一个JButton时,我的代码会设置一个6x6的JButton数组,弹出一个6x1 JDialog框,提供6种颜色选择.当其中一个被按下时,方块点击打开JDialog框改变颜色.
我想写一段代码,这样你就可以在任何行/列中只有一个方格的每种颜色.在minut中,布尔我阻止你将相同的颜色设置为相同的正方形(当你点击它时它会变成黑色,如果它是任何其他颜色).
代码如下.任何帮助都感激不尽.谢谢
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Grid5 extends JFrame implements ActionListener
{
private ColourChooser paintBox = null;
public static final int ROW = 6;
public static final int COLUMN = 6;
private static Grid5 grid;
public static final String defaultName = "Black";
public JButton[][] buttons; //makes an array called buttons
public static void main(String[] args)// sets up a 6x6 grid
{
int rows = 6;
int cols = 6;
int size = …Run Code Online (Sandbox Code Playgroud)