当前位置: 首页 > 面试题库 >

包含JButton对象的数组

太叔鹏云
2023-03-14
问题内容

好的,所以我试图从我用来学习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还是很陌生,所以我不确定什么是必要的。感谢帮助。谢谢。


问题答案:

您正在做的是尝试在需要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();
    }
}


 类似资料:
  • 我希望在每个数组中获取多个对象,但我的代码显示了单个对象的操作。下面是我的PHP代码,我如何添加另一个循环来获取多个对象 这是显示单个物体的结果

  • 我从accuweather获得了以下带有json的代码 我尝试通过Jackson将此对象解析为POJO 我有json中指定的所有模型,如、数组、,由组成(在json中命名为最小值和最大值)等,它们都有私有字段和公共构造函数、getter和setter。但是我没有一些字段,因为我想省略它们(Day、night、EpochDate、Source)。 当我运行程序时,我得到了错误 com.fasterx

  • 我使用Nodejs,有一个对象包含一个object数组(API请求的结果)和一个简单的值数组。 它们看起来是这样的: 如何将包含对象数组的对象与简单数组进行比较? 我要找的是,从简单数组中获取ID,并将它们与另一个“复杂”对象进行比较,以检索属性以及简单数组中的相应ID和另一个对象属性,然后将结果存储在一个对象数组中。 简单来说,我希望有这样的结果: 当从API请求中检索“复杂”对象时,我尝试了以

  • 我用的是Gson。toJSON方法。我的pojo包含一个属性作为URL字符串。奇怪的是Gson转换器改变了URL字符 输出为:/myApp/myAction。html?方法\U003Drooter\u0026cmd\u003d1 预期输出为:/myApp/myAction。html?方法=路由器

  • 问题内容: 我使用 PostgreSQL 9.5 和Rails5。我想查询下面显示的包含JSON对象数组的列,以返回包含并执行计数的所有JSON数组元素。我使用 的 SQL 显示在json数据下方。运行查询只会返回一个空数组。 这是我的数据: 我想要其中一个sql查询返回: 和另一个查询以返回数组,以便我可以在应用程序中对其调用count,还可以遍历它以创建表单: 我试过这个SQL查询: 我也试过

  • 我使用PostgreSQL 9.5和Rails 5。我想查询下面显示的包含JSON对象数组的jsonb列,以返回包含的所有JSON数组元素,并执行计数 我使用的SQL显示在json数据下面。运行查询只返回一个空数组。 我尝试了这里和这里建议的查询。 这就是我的jsonb数据的样子: 我希望其中一个sql查询返回: 和另一个返回数组的查询,以便我可以在我的应用程序中调用count并循环使用它来创建表