有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java棋盘,奇数/偶数%2面板

我已经成功地获得了一个棋盘,可以使用彩色面板将其组合在一起,但只有当用户为行和列输入奇数时。否则,当输入偶数时,它只显示交替的彩色列。我正在努力想办法写一个短片段,通过使用%2=0,检查它是奇数还是偶数,结果是偶数改变颜色。下面是我的代码。谢谢,别紧张,我对编程很陌生!-)

此外,我还创建了一个单独的ColorPanel类来构建彩色面板,然后将其引入到我的主程序中。我没有费心把代码放在下面

import javax.swing.*;
import java.awt.*;

public class Checkerboard extends JPanel{

public static void main(String[] args) {
    JFrame chBoard = new JFrame();
    chBoard.setTitle("Checkerboard");
    chBoard.setSize(800,800);
    chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
    if (inputStr == null) return;
    int row = Integer.parseInt(inputStr);

    inputStr = JOptionPane.showInputDialog("Number of columns", "5");
    if (inputStr == null) return;
    int col = Integer.parseInt(inputStr);

    Container pane = chBoard.getContentPane();
    pane.setLayout(new GridLayout(row, col));

    Color BoxColor = Color.red;

    for ( int counter = 1;  counter <= row * col;  counter++ )
      {
        if (BoxColor == Color.red)
             BoxColor = Color.black;
        else
             BoxColor = Color.red;

        ColorPanel panel = new ColorPanel(BoxColor);
        pane.add(panel);
      }

    chBoard.setVisible(true);
}
}

共 (2) 个答案

  1. # 1 楼答案

    将循环更改为:

    for ( int x = 0;  x < row;  x++ ) {
        for(int y = 0; y < col; y++) {
            if((x + y)%2 == 0) {
                BoxColor = Color.red;
            } else {
                BoxColor = Color.black;
            }
    
            ...
    
        }
    }
    
  2. # 2 楼答案

    就像我说的,我是编程新手,但我真的很享受这种学习经历。我希望这对其他人的学习经验有所帮助

    无论如何,我想我用单独的ColorPanel类为自己创造了更多的作品。因此,我没有创建单独的ColorPanel类来构建彩色面板,而是将其更改为使用先前存在的JPanel类在主程序内创建面板。因此,与其说:

    ColorPanel panel = new ColorPanel(BoxColor); 
    

    色彩面板类

    我说:

    JPanel panel = new JPanel();
    panel.setBackground(BoxColor);
    

    并删除了附加的ColorPanel类

    抱歉,我只是想解释清楚

    此外,多亏了杰森,他真的帮助我想出了使用这两个工具的主意

    int x & y
    

    数一数

    row & col
    

    然后把它们加在一起,使我能够使用

    %2=0
    

    来确定我是在奇数还是偶数小组

    希望这对某人有帮助!:-)

    最终代码如下所示:

    import javax.swing.*;
    import java.awt.*;
    
    public class Checkerboard extends JPanel{
    
    public static void main(String[] args) {
        JFrame chBoard = new JFrame();
        chBoard.setTitle("Checkerboard");
        chBoard.setSize(800,800);
        chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
        if (inputStr == null) return;
        int row = Integer.parseInt(inputStr);
    
        inputStr = JOptionPane.showInputDialog("Number of columns", "5");
        if (inputStr == null) return;
        int col = Integer.parseInt(inputStr);
    
        Container pane = chBoard.getContentPane();
        pane.setLayout(new GridLayout(row, col));
    
        Color BoxColor = Color.red;
    
        for ( int x = 0;  x < row;  x++ ) {
            for(int y = 0; y < col; y++) {
                if((x + y)%2 == 0) {
                    BoxColor = Color.red;}
                else{
                 BoxColor = Color.black;}
    
            JPanel panel = new JPanel();
            panel.setBackground(BoxColor);
    
            pane.add(panel);
          }
    
        chBoard.setVisible(true);
    }
    }
    }