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

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

问题描述

好了,所以我试图做一本书我用学习Java的练习。这里是code,我到目前为止有:

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

下面是确切措辞演习:

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

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

按钮[] = numButtons新的按钮[10];

替换10号线,从开始

BUTTON0 =的新的JButton(0);

与创建按钮,并将其存储在这个数组中循环。

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

好了,我想声明的按钮[] numbuttons行数组,但只是给我的错误:

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

在该行结果多个标记
  -Buttons不能被解析为一个类型结果
  -Buttons不能被解析为一个类型

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]

,然后加入每个按钮像这样的数组:

And then added each button to the array like this:

buttons[0] = "button0";

这样做并没有给我一个错误,当我宣布阵列,但是当我写的键[0] 行,我得到这个错误:

令牌按钮语法错误,删除此标记

Syntax error on token "buttons",delete this token

所以,我需要帮助搞清楚如何做到这一点。此外,这本书可以在这里找到: http://myflex.org/books/java4kids/JavaKid811。 PDF 和实践是73页。
我道歉,如果我挂牌的信息。这只是因为我很新的Java和我不知道什么是必要的。帮助是AP preciated。谢谢你。

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.

推荐答案

你在做什么是试图阵列空间设置为一个字符串,当你需要一个JButton。

What you are doing is trying to set the array space to a string when you need a 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 

对于输出,所以你的错误是不是在该行。

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

编辑:code

Calculator.java

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天全站免登陆