JButton 的 2D 数组的问题

发布于 2024-08-03 19:12:45 字数 1575 浏览 4 评论 0原文

我需要将数字(二维数组)放在 JButton 的左上角。但是,我得到的数字结果顺序错误(颠倒),与数组 topLeftNum 顺序不同。如何使号码显示为相同的订单?请帮我解决我的问题..谢谢

final int ROWS = 12;

final int COLS = 12;

final static int topLeftNum[][]= {
    {-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},
    {6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},
    {-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},
    {9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},
    {0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},
    {0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},
    {17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},
    {0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},
    {22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},
    {-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},
    {26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},
    {-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}
    };

public static void main(String[] args){

  SwingUtilities.invokeLater(new Runnable() {
     public void run(){
           new Lat1();
      }
   });
}

public Lat1() {
  //d = f.getText();
  //answer = d.charAt(0);
  this.getContentPane().setLayout(new GridLayout(ROWS, COLS));
  for (int j=0; j<ROWS; j++) {
    for (int i=0; i<COLS; i++)  {
       this.getContentPane().add(new Box(i, //the boxes index
                (topLeftNum[j][i] < 0) ? Color.BLACK : Color.WHITE, //pick the color
                topLeftNum[j][i], //the topleft number
                (char)(65+i), //the char inside
                this), //the action listener for the button
                i); //the index to place it on the container
       }
    }
    this.pack();
    this.setVisible(true);
}

I need to put number(2d array) on the top left of JButton. But, i got the number result in wrong ordder (upside down), not as the same as the array topLeftNum order. How to make it so the number will show up as the same order?Please help me solve my problem..Thanks

final int ROWS = 12;

final int COLS = 12;

final static int topLeftNum[][]= {
    {-1, 1, 0, 2, 0, 0, 3, -1, 4, 0, 5, 0},
    {6, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, -1},
    {-1, 0, -1, 0, -1, 7, 0, 0, 8, -1, 0, -1},
    {9, 0, 0, 0, 10, -1, -1, -1, 11, 0, 0, -1},
    {0, -1, -1, 12, 0, 0, 13, -1, 0, -1, -1, -1},
    {0, -1, 14, -1, 0, -1, 0, -1, 15, 0, 0, 16},
    {17, 0, 0, 18, 0, -1, 19, 20, 0, -1, -1, 0},
    {0, -1, 0, 0, -1, 21, 0, 0, 0, -1, -1, 0},
    {22, 23, 0, 0, -1, 0, -1, 0, -1,24, 0, 0},
    {-1, 0, -1, 25, 0, 0, -1, 0, -1, 0, -1, -1},
    {26, 0, 0, -1, -1, 0, -1, 27, 0, 0, 0, -1},
    {-1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1}
    };

public static void main(String[] args){

  SwingUtilities.invokeLater(new Runnable() {
     public void run(){
           new Lat1();
      }
   });
}

public Lat1() {
  //d = f.getText();
  //answer = d.charAt(0);
  this.getContentPane().setLayout(new GridLayout(ROWS, COLS));
  for (int j=0; j<ROWS; j++) {
    for (int i=0; i<COLS; i++)  {
       this.getContentPane().add(new Box(i, //the boxes index
                (topLeftNum[j][i] < 0) ? Color.BLACK : Color.WHITE, //pick the color
                topLeftNum[j][i], //the topleft number
                (char)(65+i), //the char inside
                this), //the action listener for the button
                i); //the index to place it on the container
       }
    }
    this.pack();
    this.setVisible(true);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2024-08-10 19:12:45

我); //将其放置在容器上的索引

这一行是问题所在,删除“i”即可正常工作。

i); //the index to place it on the container

this line is the problem, remove "i" and it works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文