Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:52:39+00:00 2026-05-31T16:52:39+00:00

Ok, so I’m attempting to do an exercise from a book I’m using to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:52:41+00:00Added an answer on May 31, 2026 at 4:52 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.