Java 保存 JButton 对象的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9814566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array that Holds JButton Objects
提问by Hyman Davis
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:
好的,所以我正在尝试从我用来学习 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();
}
}
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.
修改 Calculator.java 类,以保持 10 元素数组中的所有数字按钮声明如下:
Buttons[] numButtons= new Buttons[10];
替换 10 行从
button0=new JButton("0");
用一个循环创建按钮并将它们存储在这个数组中。
Ok, so I tried declaring the array with the Buttons[] numbuttons line
, but that just gave me the error:
好的,所以我尝试使用 声明数组Buttons[] numbuttons line
,但这只是给了我错误:
Multiple markers at this line
-Buttons can not be resolved to a type
-Buttons can not be resolved to a type
此行的多个标记
-Buttons 无法解析为类型
-Buttons 无法解析为类型
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:
当我声明数组时,这样做并没有给我一个错误,但是当我写这buttons[0]
行时,我得到了这个错误:
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.pdfand 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.
所以,我需要帮助弄清楚如何做到这一点。此外,这本书可以在这里找到:http: //myflex.org/books/java4kids/JavaKid811.pdf,实践在第 73 页。如果我列出了太多信息,我深表歉意。这只是因为我对 Java 很陌生,我不确定什么是必要的。帮助表示赞赏。谢谢。
采纳答案by twain249
What you are doing is trying to set the array space to a string when you need a JButton.
您正在做的是在需要 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
计算器.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();
}
}
回答by Mayur
you should use
你应该使用
buttons[0] = button0;
and not
并不是
buttons[0] = "button0";
回答by Raevik
If I understand your problem, you need the loop to instantiate and store the JButtons.
如果我理解你的问题,你需要循环来实例化和存储 JButton。
for (int i=0; i<10; i++) {
numButton[i] = new JButton(String.valueOf(i));
}
You need to convert the loop control variable into a String argument for the JButton constructor.
您需要将循环控制变量转换为 JButton 构造函数的 String 参数。
回答by rob
JButton[] buttons = new JButton[10]
The line above is correct, but I see two points of confusion:
上面的行是正确的,但我看到了两个混淆点:
This line:
buttons[0] = "button0";
should instead be as follows:
buttons[0] = new JButton("button0");
The reason is that in your code, you're trying to assign a
String
tobuttons[0]
, instead of the expectedJButton
.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
Buttons
is not a standard Java class. Do a case-sensitive search forButtons
and replace all matches withJButton
.
这一行:
buttons[0] = "button0";
应该如下:
buttons[0] = new JButton("button0");
原因是在您的代码中,您试图将 分配
String
给buttons[0]
,而不是预期的JButton
。好的,所以我尝试使用 Buttons[] numbuttons 行声明数组,但这给了我错误:此行的多个标记 -Buttons 无法解析为类型 -Buttons 无法解析为类型
Buttons
不是标准的 Java 类。进行区分大小写的搜索Buttons
并将所有匹配项替换为JButton
.
If you still have problems, please copy and paste the exact codefor each variation that is not working.
如果您仍然有问题,请复制并粘贴每个不起作用的变体的确切代码。