java - 使用工厂返回多个 JButton 数组...?

标签 java arrays return jbutton

我是新的 Java 学生,我需要一些代码方面的帮助。

我正在编写一个包含 JButton 数组的方法,并且我正在使用工厂方法来制作它们。现在的问题是,我不知道如何返回按钮(它们有不同的颜色,所以我不得不将它们分开)。我总共有 4 个数组,我需要以某种方式将它们全部返回...

这是我的代码

private JButton[] createButtons() {
    JButton [] numberButton      =   null ;
    JButton [] operatorButtons   =   null ;
    JButton [] controlButtons    =   null ;
    JButton [] equalsButton      =   null ;

    numberButton                 =   new JButton[10] ;
    operatorButtons              =   new JButton[6]  ;
    controlButtons               =   new JButton[2]  ;
    equalsButton                 =   new JButton[1]  ;

    int index                    =   0 ;

    String   [] calcButtons   =   {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "Sqrt", "%",
                                     "C", "BS", "="} ;

    for (index = 0; index < 10; index++)
    numberButton[index]   =   ButtonFactory.createButton(calcButtons[index], Color.LIGHT_GRAY) ; 
    for (index = 10; index < 17; index++)
    operatorButtons[6]    =   ButtonFactory.createButton(calcButtons[index], Color.CYAN) ; 
    for (index = 17; index < 20; index++)
    controlButtons[2]     =   ButtonFactory.createButton(calcButtons[index], Color.MAGENTA) ;
    for (index = 19; index == 20; index++) 
    equalsButton[1]       =   ButtonFactory.createButton(calcButtons[index], Color.GREEN) ; 

    return (JButton[]) new Object[]{numberButton, numberButton, operatorButtons, controlButtons, equalsButton} ;


}

我在这里读到,我可以像在底部那样创建一个对象,它可以工作,但事实并非如此......

此外,我可能需要一些帮助,将 actionListener 和 mouseListener 添加到每个数组。

谢谢!!

使用一个数组编辑整个新方法

private JButton[] createButtons() {
    JButton [] calcButtons        =   null ;
    calcButtons                  =   new JButton[BUTTON_COLUMNS * BUTTON_ROWS] ;
    int index                    =   0 ;

    String   [] numButtons       =   {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."} ;
    String   [] operButtons      =   {"+", ".", "-", "*", "/", "Sqrt", "%"} ;
    String   [] contButtons      =   {"C", "BS"} ;
    String   [] equalButton      =   {"="} ;


    for (int i = 0; i < numButtons.length ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(numButtons[index], Color.LIGHT_GRAY)) ;
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for (int i = 0; i < operButtons.length ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(operButtons[index], Color.CYAN)) ;
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for (int i = 0; i < contButtons.length; i++) {
    calcButtons[i].add(ButtonFactory.createButton(contButtons[index], Color.MAGENTA)) ; 
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for(int i = 0; i < equalButton.length; i++){
    calcButtons[i].add(ButtonFactory.createButton(equalButton[index], Color.GREEN)) ; 
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ;}


    return calcButtons ;

}

但是我的按钮仍然为空,我不知道出了什么问题。我认为它与 calcButtons 数组有关。

基本上

我希望能够做到这一点

好吧,基本上我希望能够做到这一点

calcButtons[0].add(ButtonFactory.createButton("0", Color.LIGHT_GRAY)) ;
        calcButtons[1].add(ButtonFactory.createButton("1", Color.LIGHT_GRAY)) ;
        calcButtons[2].add(ButtonFactory.createButton("2", Color.LIGHT_GRAY)) ;
        calcButtons[3].add(ButtonFactory.createButton("3", Color.LIGHT_GRAY)) ;
        calcButtons[4].add(ButtonFactory.createButton("3", Color.LIGHT_GRAY)) ; 

使用数组(包括操作和内容)...

编辑

你的意思是这样的吗?

for (int i = numButtons.length; i < (numButtons.length + operButtons.length) ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(operButtons[index++], Color.CYAN)) ;
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

因为在我将 for 循环更改为这个之后,我仍然得到 null。我什至数过它们和所有的东西。可能和index++有关系吗?

最佳答案

  • 您在 for 循环内对数组索引进行硬编码,这意味着代码将会失败,因为您将重复将 JButton 分配给同一数组项,而留下另一个数组项为空。您必须在 JButton 和 String calcButtons 数组中使用变量索引。
  • 考虑使用全部以 i 开头的 for 循环和一个称为索引的单独索引,并在每个 for 循环的每次迭代中递增索引。
  • 然后使用 i 作为按钮数组索引,使用索引作为字符串数组索引。
  • 不要使用神奇的数字,因为它们最终会咬你的屁股。使用数组的长度来确定 for 循环何时完成。
  • 此外,您最终返回的数组通常是正确的想法,但它不应该是 Object[],而应该是 JButton[][]

例如,

int index = 0;
for (int i = 0; i < numberButton.length; i++) {
  // fill numberButton[i] using calcButtons[index]
  index++;
}
for (int i = 0; i < operatorButtons.length; i++) {
  // fill in operatorButtons[i], use calcButtons[index]
  index++;
}

/// ... etc
return new JButton[][]{numberButton, ..... etc...};

____________________________________>____

编辑
关于您的最新代码,让我们在脑海中浏览一下您的代码的一部分......

  for (int i = 0; i < numButtons.length; i++) {

     // Line (A)
     calcButtons[i].add(ButtonFactory.createButton(numButtons[index], Color.LIGHT_GRAY));
     index++;
     calcButtons[i].addMouseListener(this);
     calcButtons[i].addActionListener(this);
  }

  for (int i = 0; i < operButtons.length; i++) {

     // Line (B)
     calcButtons[i].add(ButtonFactory.createButton(operButtons[index], Color.CYAN));
     index++;
     calcButtons[i].addMouseListener(this);
     calcButtons[i].addActionListener(this);
  }

那么注释 (A) 下的行会发生什么?:您创建新的 JButton,并将它们分配给数组的数组位置 0 到 numberButton.length,基本上填充 calcButtons 数组的开始部分。好的,到目前为止一切顺利。

但是在注释 (B) 和所有类似行下会发生什么?:您创建新的 JButton,并将它们分配到数组位置 0 到operatorButtons.length 的位置。

但是这样可以吗?不,这并不是因为您要重新分配数组开头的所有 JButton。这是你想做的吗?不会,因为它会将所有数组项留在数组的末尾和中间 null。您想要前进到之前分配的项目下方的数组 JButton 项目。有没有解决的办法?是的,只需使用您已经使用的变量即可。

我会让你弄清楚如何做到这一点 - 我知道你可以。

关于java - 使用工厂返回多个 JButton 数组...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715095/

相关文章:

java - 如何将 double 转换为对象?

java - 观看目录时仅触发一次事件

java - 如何使用 Spring Boot 和 Angular 自动运行日常 PL/SQL 脚本(Oracle)?

javascript - 无法将数组从 jQuery 发送到 MVC 4 Controller

c++ - 在 C++ 中传递和返回具有特殊约束 (NxN) 类型安全的二维数组(矩阵)的最优雅方式

python - 引发错误与返回 False python3

c++ - 如何返回转发引用?

java - mediaPlayer CompletionListener 在完成歌曲之前播放下一首歌曲

python - 在 python 3 中使用 bool 条件向量选择 numpy 子数组的简单方法

javascript - 如何在返回数组的 return 语句中使用三元运算符