保存 JButton 对象的数组 [英] Array that Holds JButton Objects

查看:21
本文介绍了保存 JButton 对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

import javax.swing.*;导入 java.awt.GridLayout;导入 java.awt.BorderLayout;公共类计算器{//声明所有计算器的组件.JPanel 窗口内容;JTextField 显示字段;JButton button0;JButton button1;JButton button2;JButton button3;JButton button4;JButton button5;JButton button6;JButton button7;JButton button8;JButton button9;JButton buttonPoint;JButton按钮添加;JButton buttonEqual;JPanel pl;//Constructor 在内存中创建组件并使用 Borderlayout 的组合将其添加到框架中.计算器(){windowContent=new JPanel();//为这个面板设置布局管理器BorderLayout bl = new BorderLayout();windowContent.setLayout(bl);//创建显示字段并将其放置在窗口的北部区域displayField = new JTextField(30);windowContent.add("North",displayField);//创建按钮字段并将其放置在窗口的北部区域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("=");//使用 GridLayout 创建面板,该面板将包含 12 个按钮 - 10 个数字按钮,以及带有点的按钮//和等号.pl = new JPanel();GridLayout gl =new GridLayout(4,3);pl.setLayout(gl);//将窗口控件添加到面板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);//将面板pl添加到窗口的中心区域windowContent.add("Center",pl);//创建框架并设置其内容窗格JFrame frame = new JFrame("计算器");frame.setContentPane(windowContent);//将窗口的大小设置为足够大以容纳所有控件.框架.pack();//最后,显示窗口frame.setVisible(true);}公共静态无效主(字符串 [] args){计算器 calc = new Calculator();}}

以下是准确措辞的练习:

<块引用>

修改 Calculator.java 类,使 10 元素数组中的所有数字按钮保持如下声明:

Buttons[] numButtons= new Buttons[10];

替换以

开头的10行

button0=new JButton("0");

用一个循环创建按钮并将它们存储在这个数组中.

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

<块引用>

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

我改为尝试这个:

JButton[] 按钮 = 新 JButton[10]

然后将每个按钮添加到数组中,如下所示:

buttons[0] = "button0";

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

<块引用>

标记buttons"的语法错误,删除这个标记

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

解决方案

您正在做的是在需要 JButton 时尝试将数组空间设置为字符串.

你应该这样做

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

代替

buttons[0] = "button0";

我就是这样做的

import javax.swing.*;公开课测试{公共静态无效主(字符串 [] args){JButton[] 按钮 = 新 JButton[10];button[0] = new JButton("0");System.out.println(buttons[0].getText());}}

并得到

<预><代码>0

用于输出,因此您的错误不在该行中.

代码

计算器.java

import javax.swing.*;导入 java.awt.GridLayout;导入 java.awt.BorderLayout;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;公共类计算器{//声明所有计算器的组件.JPanel 窗口内容;JTextField 显示字段;JButton 按钮[];JButton buttonPoint;JButton按钮添加;JButton buttonEqual;JPanel pl;//Constructor 在内存中创建组件并使用 Borderlayout 的组合将其添加到框架中.计算器(){windowContent=new JPanel();按钮 = 新 JButton[10];//为这个面板设置布局管理器BorderLayout bl = new BorderLayout();windowContent.setLayout(bl);//创建显示字段并将其放置在窗口的北部区域displayField = new JTextField(30);windowContent.add("North",displayField);//创建按钮字段并将其放置在窗口的北部区域for(int i = 0; i <10; i++) {button[i] = new JButton(String.valueOf(i));}buttonAdd=new JButton("+");buttonPoint = new JButton(".");buttonEqual=new JButton("=");//使用 GridLayout 创建面板,该面板将包含 12 个按钮 - 10 个数字按钮,以及带有点的按钮//和等号.pl = new JPanel();GridLayout gl =new GridLayout(4,3);pl.setLayout(gl);//将窗口控件添加到面板pl.for(int i = 0; i <10; i++) {pl.add(buttons[i]);}pl.add(buttonAdd);pl.add(buttonPoint);pl.add(buttonEqual);//将面板pl添加到窗口的中心区域windowContent.add("Center",pl);//创建框架并设置其内容窗格JFrame frame = new JFrame("计算器");frame.setContentPane(windowContent);//将窗口的大小设置为足够大以容纳所有控件.框架.pack();//最后,显示窗口frame.setVisible(true);}公共静态无效主(字符串 [] args){计算器 calc = new Calculator();}}

Ok, so I'm attempting to do an exercise from a book I'm using to learn Java. Here is the code that I have so far:

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

Here is the exercise in exact wording:

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.

Ok, so I tried declaring the array with the Buttons[] numbuttons line, but that just gave me the error:

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

I instead tried this:

JButton[] buttons = new JButton[10]

And then added each button to the array like this:

buttons[0] = "button0";

Doing this didn't give me an error when I declared the array, but when I wrote the buttons[0] line, I got this error:

Syntax error on token "buttons",delete this token

So, I need help figuring out how to do this. Also, the book can be found here: http://myflex.org/books/java4kids/JavaKid811.pdf and the practice is on page 73. I apologize if I'm listing to much information. It's just because I'm very new to Java and I'm not sure what is necessary. Help is appreciated. Thanks.

解决方案

What you are doing is trying to set the array space to a string when you need a JButton.

You should be doing this

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

instead of

buttons[0] = "button0";

EDIT:

I just did this

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

}

and got

0 

for an output so your error isn't in that line.

EDIT: Code

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

这篇关于保存 JButton 对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆