java - 包含 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;
    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();
    }
}

这里是精确措辞的练习:

Modify the class Calculator.java to keep all numeric buttons in the 10-element array declared as follows:

Buttons[] numButtons= new Buttons[10];

Replace 10 lines that start from

button0=new JButton("0");

with a loop that creates the buttons and store them in this array.

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

Multiple markers at this line
-Buttons can not be resolved to a type
-Buttons can not be resolved to a type

我改为尝试这个:

JButton[] buttons = new JButton[10]

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

buttons[0] = "button0";

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

Syntax error on token "buttons",delete this token

所以,我需要帮助弄清楚如何做到这一点。此外,这本书可以在这里找到: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();
    }
}

关于java - 包含 JButton 对象的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814566/

相关文章:

arrays - 使用数组将文本从一个工作表传递到另一个工作表

java - 更改 JTextPane EditorKit

java - 将 Integer[] 转换为 int[] 的最佳方法是什么

javascript - d3 无法读取未定义的属性 'classed'

java - 将 2 个标签放入 jframe 边框布局的中间

java - 如何将 JTextField 中的特定数据更新到数据库中?

java - 如何在 Android 中最好地做到这一点?

java - 单击可展开 ListView

java - 使用 Jsoup 检索 sessionId cookie 时出现问题

java - 如何使用Play返回415不支持的媒体类型?