Java 包含JButton对象的数组

Java 包含JButton对象的数组,java,arrays,swing,jbutton,Java,Arrays,Swing,Jbutton,好的,我正试着从一本我用来学习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

好的,我正试着从一本我用来学习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=新按钮[10]

更换从以下位置开始的10条线路:

button0=新的JButton(“0”)

创建按钮并将其存储在此数组中的循环

好的,所以我尝试用
按钮[]numbuttons行
声明数组,但这只是给了我一个错误:

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

相反,我尝试了以下方法:

JButton[] buttons = new JButton[10]
然后将每个按钮添加到数组中,如下所示:

buttons[0] = "button0";
当我声明数组时,这样做并没有给我一个错误,但是当我编写
按钮[0]
行时,我得到了这个错误:

令牌“按钮”上出现语法错误,请删除此令牌

所以,我需要你帮我弄清楚怎么做。此外,这本书可以在这里找到:实践在第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 
以使您的错误不在该行中

编辑:代码

Calculator.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();
    }
}
import javax.swing.*;
导入java.awt.GridLayout;
导入java.awt.BorderLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类计算器{
//所有计算器组件的声明。
JPanel窗口内容;
JTextField显示字段;
JButton按钮[];
JButton按钮点;
b按钮按钮d;
JButton按钮相等;
JPanel pl;
//构造函数在内存中创建组件,并使用Borderlayout的组合将组件添加到框架中。
计算器(){
windowContent=newjpanel();
按钮=新的JButton[10];
//设置此面板的布局管理器
BorderLayout bl=新的BorderLayout();
windowContent.setLayout(bl);
//创建显示字段并将其放置在窗口的北部区域
displayField=新的JTextField(30);
添加(“北”,显示字段);
//创建按钮字段并将其放置在窗口的北部区域
对于(int i=0;i<10;i++){
按钮[i]=新的JButton(String.valueOf(i));
}
ButtonAd=新的JButton(“+”);
buttonPoint=新的JButton(“.”);
buttonEqual=新的JButton(“=”);
//使用GridLayout创建面板,该面板将包含12个按钮(10个数字按钮)和带点的按钮
//和等号。
pl=新的JPanel();
GridLayout gl=新的GridLayout(4,3);
pl.setLayout(总图);
//将窗口控件添加到面板pl。
对于(int i=0;i<10;i++){
pl.add(按钮[i]);
}
pl.add(按钮D);
pl.add(按钮点);
pl.add(按钮相等);
//将面板pl添加到窗口的中心区域
添加(“中心”,pl);
//创建框架并设置其内容窗格
JFrame=新的JFrame(“计算器”);
frame.setContentPane(windowContent);
//将窗口的大小设置为足以容纳所有控件。
frame.pack();
//最后,显示窗口
frame.setVisible(true);
}
公共静态void main(字符串[]args){
计算器计算器=新计算器();
}
}
您应该使用

buttons[0] = button0;
而不是

buttons[0] = "button0";

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

for (int i=0; i<10; i++) {
    numButton[i] = new JButton(String.valueOf(i));
}
for(int i=0;i
上面的行是正确的,但我看到两个混淆点:

  • 这一行:

    buttons[0] = "button0";
    
    应改为如下:

    buttons[0] = new JButton("button0");
    
    原因是,在代码中,您试图将
    字符串
    分配给
    按钮[0]
    ,而不是预期的
    JButton

  • 好的,我试着用Buttons[]numbuttons行声明数组, 但这给了我一个错误:在这条线上有多个标记 -按钮无法解析为类型-按钮无法解析为类型

    按钮
    不是标准的Java类。请执行区分大小写的搜索 对于
    按钮
    ,将所有匹配项替换为
    JButton


  • 如果仍然存在问题,请复制并粘贴每个不起作用的变体的确切代码。

    更具体地说,您正在尝试将字符串放入为JButtons制作的数组中。Java不希望您以这种方式混合数据类型,并抛出错误。

    返回与以前相同的错误,即标记“buttons”上的语法错误,删除这个标记你能把代码粘贴到这里让我们看看你到底是如何实现这个数组的吗?好的,那么你认为问题出在哪里?我刚从上面拿了你的代码,把所有的按钮(0-9)改成了一个名为buttons的数组,更新了所有的引用,它编译得很好我把它改成了按钮[1](或按钮[2]等等)。好的,我很确定我刚刚做了所有这些,当我制作数组时,我得到了同样的错误,你能发布你为我制作的整个程序吗?然后我可以看到我做错了什么。当然我会编辑我的答案(我也做了一些事情,使数字键工作起来更有用)嗯,现在的问题不是创建循环,而是创建数组的一个正常部分。有什么帮助吗?\n你能解释一下“数组的一个正常部分”是什么意思吗?
    按钮是一个由
    按钮组成的数组,而不是
    字符串
    
    buttons[0] = new JButton("button0");