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.Gri
-
you should use
buttons[0] = button0;
and not
buttons[0] = "button0";
讨论(0)
-
If I understand your problem, you need the loop to instantiate and store the JButtons.
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.
讨论(0)
-
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();
}
}
讨论(0)
-
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
to buttons[0]
, instead of the expected JButton
.
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
for Buttons
and replace all matches with JButton
.
If you still have problems, please copy and paste the exact code for each variation that is not working.
讨论(0)
- 热议问题