【问题标题】:Array that Holds JButton Objects保存 JButton 对象的数组
【发布时间】:2012-04-06 13:24:54
【问题描述】:

好的,所以我正在尝试从我用来学习 Java 的书中做一个练习。这是我到目前为止的代码:

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class Calculator {
    //Declaration of all calculator's components.
    JPanel windowContent;
    JTextField displayField;
    JButton button0;
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    JButton button5;
    JButton button6;
    JButton button7;
    JButton button8;
    JButton button9;
    JButton buttonPoint;
    JButton buttonAdd;
    JButton buttonEqual;
    JPanel pl;

    //Constructor creates the components in memory and adds the to the frame using combination of Borderlayout.
    Calculator() {
        windowContent= new JPanel();

    // Set the layout manager for this panel
        BorderLayout bl = new BorderLayout();
        windowContent.setLayout(bl);

    //Create the display field and place it in the North area of the window
        displayField = new JTextField(30);
        windowContent.add("North",displayField);

    //Create button field and place it in the North area of the window
        button0=new JButton("0");
        button1=new JButton("1");
        button2=new JButton("2");
        button3=new JButton("3");
        button4=new JButton("4");
        button5=new JButton("5");
        button6=new JButton("6");
        button7=new JButton("7");
        button8=new JButton("8");
        button9=new JButton("9");
        buttonAdd=new JButton("+");
        buttonPoint = new JButton(".");
        buttonEqual=new JButton("=");

    //Create the panel with the GridLayout that will contain 12 buttons - 10 numeric ones, and button with the points
    //and the equal sign.
        pl = new JPanel ();
        GridLayout gl =new GridLayout(4,3);
        pl.setLayout(gl);
    //Add window controls to the panel pl.
        pl.add(button1);
        pl.add(button2);
        pl.add(button3);
        pl.add(button4);
        pl.add(button5);
        pl.add(button6);
        pl.add(button7);
        pl.add(button8);
        pl.add(button9);
        pl.add(buttonAdd);
        pl.add(buttonPoint);
        pl.add(buttonEqual);

    //Add the panel pl to the center area of the window
        windowContent.add("Center",pl);
    //Create the frame and set its content pane
        JFrame frame = new JFrame("Calculator");
        frame.setContentPane(windowContent);
    //set the size of the window to be big enough to accomodate all controls.
        frame.pack();
    //Finnaly, display the window
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
    }
}

这是练习的准确措辞:

修改类 Calculator.java 以将所有数字按钮保留在 10 元素数组中,声明如下:

Buttons[] numButtons= new Buttons[10];

替换从

开始的10行

button0=new JButton("0");

使用循环创建按钮并将它们存储在此数组中。

好的,所以我尝试使用Buttons[] numbuttons line 声明数组,但这只是给了我错误:

此行有多个标记
-按钮无法解析为类型
-按钮无法解析为类型

我改为尝试这个:

JButton[] buttons = new JButton[10]

然后像这样将每个按钮添加到数组中:

buttons[0] = "button0";

这样做并没有在我声明数组时给我一个错误,但是当我写buttons[0] 行时,我得到了这个错误:

标记“按钮”的语法错误,删除此标记

所以,我需要帮助弄清楚如何做到这一点。此外,这本书可以在这里找到:http://myflex.org/books/java4kids/JavaKid811.pdf,练习在第 73 页。 如果我列出了很多信息,我很抱歉。这只是因为我对 Java 很陌生,我不确定什么是必要的。帮助表示赞赏。谢谢。

【问题讨论】:

  • buttonsJButtons 的数组,而不是 Strings

标签: java arrays swing jbutton


【解决方案1】:

当您需要 JButton 时,您正在尝试将数组空间设置为字符串。

你应该这样做

buttons[0] = new JButton("0");

而不是

buttons[0] = "button0";

编辑:

我刚刚做了这个

import javax.swing.*;

public class test {

    public static void main(String[] args) {
        JButton[] buttons = new JButton[10];

        buttons[0] = new JButton("0");

        System.out.println(buttons[0].getText());
    }

}

得到了

0 

对于输出,因此您的错误不在该行中。

编辑:代码

计算器.java

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
    //Declaration of all calculator's components.
    JPanel windowContent;
    JTextField displayField;
    JButton buttons[];
    JButton buttonPoint;
    JButton buttonAdd;
    JButton buttonEqual;
    JPanel pl;

    //Constructor creates the components in memory and adds the to the frame using combination of Borderlayout.
    Calculator() {
        windowContent= new JPanel();
        buttons = new JButton[10];

    // Set the layout manager for this panel
        BorderLayout bl = new BorderLayout();
        windowContent.setLayout(bl);

    //Create the display field and place it in the North area of the window
        displayField = new JTextField(30);
        windowContent.add("North",displayField);

    //Create button field and place it in the North area of the window
        for(int i = 0; i < 10; i++) {
            buttons[i] = new JButton(String.valueOf(i));
        }

        buttonAdd=new JButton("+");
        buttonPoint = new JButton(".");
        buttonEqual=new JButton("=");

    //Create the panel with the GridLayout that will contain 12 buttons - 10 numeric ones, and button with the points
    //and the equal sign.
        pl = new JPanel ();
        GridLayout gl =new GridLayout(4,3);
        pl.setLayout(gl);
    //Add window controls to the panel pl.

        for(int i = 0; i < 10; i++) {
            pl.add(buttons[i]);
        }
        pl.add(buttonAdd);
        pl.add(buttonPoint);
        pl.add(buttonEqual);

    //Add the panel pl to the center area of the window
        windowContent.add("Center",pl);
    //Create the frame and set its content pane
        JFrame frame = new JFrame("Calculator");
        frame.setContentPane(windowContent);
    //set the size of the window to be big enough to accomodate all controls.
        frame.pack();
    //Finnaly, display the window
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
    }
}

【讨论】:

  • 好的,你认为问题出在哪里?
  • 我刚刚将您的代码从上面更改了所有按钮 (0-9) 到一个名为按钮的数组中,更新了所有引用并且它编译得很好。
  • 到处都是按钮 1(或按钮 2 等),我将其更改为按钮 [1](或按钮 [2] 等)。
  • 好的,我很确定我刚刚做了所有这些,并且在我制作阵列时遇到了同样的错误,你能发布你为我制作的整个程序吗?然后我可以看到我做错了什么。
  • 当然我会编辑我的答案(我还做了一些让它更有用的事情现在数字键工作了)
【解决方案2】:
JButton[] buttons = new JButton[10]

上面这行是正确的,但我看到了两点混乱:

  1. 这一行:

    buttons[0] = "button0";
    

    应该改为:

    buttons[0] = new JButton("button0");
    

    原因是在您的代码中,您尝试将String 分配给buttons[0],而不是预期的JButton

  2. 好的,所以我尝试使用 Buttons[] numbuttons 行声明数组, 但这只是给了我错误:此行有多个标记 - 按钮无法解析为类型 - 按钮无法解析为类型

    Buttons 不是标准的 Java 类。进行区分大小写的搜索 对于Buttons 并将所有匹配项替换为JButton

如果您仍有问题,请复制并粘贴每个不起作用的变体的确切代码

【讨论】:

    【解决方案3】:

    如果我理解您的问题,您需要循环来实例化和存储 JButton。

    for (int i=0; i<10; i++) {
        numButton[i] = new JButton(String.valueOf(i));
    }
    

    您需要将循环控制变量转换为 JButton 构造函数的字符串参数。

    【讨论】:

    • 嗯,现在,问题不在于创建循环,而在于创建数组的一个正常部分。有什么帮助吗?\
    • 你能解释一下“数组的一个正常部分”是什么意思吗?
    【解决方案4】:

    你应该使用

    buttons[0] = button0;
    

    而不是

    buttons[0] = "button0";
    

    【讨论】:

    • 返回和以前一样的错误,标记“按钮”的语法错误,删除这个标记
    • 您能否将代码粘贴到此处,以便我们查看您是如何实现数组的。
    【解决方案5】:

    更具体地说,您试图将字符串放入为 JButtons 制作的数组中。 Java 不喜欢你以这种方式混合数据类型,并引发错误。

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2021-04-24
      • 2016-02-21
      • 1970-01-01
      • 2018-07-12
      • 2016-10-04
      • 2017-01-13
      • 2020-12-19
      • 2017-05-14
      相关资源
      最近更新 更多