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

  • SEARCH
  • Home
  • 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 8715063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:45:27+00:00 2026-06-13T05:45:27+00:00

thanks for looking at this for me, heres my problems: My If statement in

  • 0

thanks for looking at this for me, heres my problems:

My If statement in my MyFrame class is saying “MyShape is abstract, cannot be instantiated”
i also do not know how to complete my actionlistener so that it creates a instance of the shape i chose from the jmenu. and lastly sets the dimension of my Jslider to the size and area of my sqaure and adds it to a JtextField.

  1. how do i fix the error “MyShape is abstract, cannot be instantiated” ?
  2. any tips on how to complete my if statement so that the shape i select is linked to my Jslider so it value is added to that shape ie a square or circle class?
  3. how do i then out put the 2 methods on finding the legth and area of that class ie the square or circle to 2 Jtext fields?

I would love any help you can throw my way, if you can please explain what i need to do it a easy way for a noob at programming like me could understand 🙂


MyFrame


    package assignment;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;


    public class MyFrame extends javax.swing.JFrame {


    public MyFrame() {


    // Create the menu
    JMenuBar topMenu = new JMenuBar();
    this.setJMenuBar(topMenu);

    //create the menu button "shapes"
    JMenu shapes = new JMenu("Shapes");
    topMenu.add(shapes);
    //Create the 3 shapes for the menu
    JMenuItem square = new JMenuItem("Square");

    JMenuItem circle = new JMenuItem("Circle");
    JMenuItem triangle = new JMenuItem("Triangle");

    //add shapes to menu
    shapes.add(circle);
    shapes.add(triangle);
    shapes.add(square);

    //add the menu
    setJMenuBar(topMenu);


    MyControlPanel pane = new MyControlPanel();
    this.add(pane);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
    }// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MyFrame().setVisible(true);
        }
    });

    class squareAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            JMenuItem clickedMenu = (JMenuItem) e.getSource();
            if (clickedMenu.getText().equals("Square")) {
                MyShape ASquare = new MyShape() {
                };
            } else if (clickedMenu.getText().equals("Circle")) {
                MyShape Circle = new MyShape();
            } else if (clickedMenu.getText().equals("Triangle")) {
                MyShape Triangle = new MyShape();
            }
        }
    }
    }
    }

Jpanel – MyControlPanel

    package assignment;

    //import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.JTextField;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;

    public class MyControlPanel extends javax.swing.JPanel {

JSlider slider;
JLabel sliderLabel;
JLabel sliderdimension;
JLabel blank;
JLabel dl;
JLabel area1;

/**
 * Creates new form MyControlPanel
 */
public MyControlPanel() {


    slider = new JSlider();
    slider.setValue(50);
    slider.addChangeListener(new MyChangeAction());
    slider.setMajorTickSpacing(10);
    slider.setPaintLabels(true);
    slider.setPaintTicks(true);
    slider.setBounds(300, 50, 100, 50);

    sliderLabel = new JLabel("50");
    blank = new JLabel("     ");
    sliderdimension = new JLabel("Shape Dimension:");

    JTextField boundary_length = new JTextField("Boundary Length");
    JTextField area = new JTextField("Area");

    dl = new JLabel("Boundary Length =");
    area1 = new JLabel("Area =");

    setLayout(new BorderLayout());


    JPanel sliderPanel = new JPanel();
    sliderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));


    sliderPanel.add(sliderdimension);
    sliderPanel.add(sliderLabel);
    sliderPanel.add(slider);
    sliderPanel.add(dl);
    sliderPanel.add(boundary_length);
    sliderPanel.add(area1);
    sliderPanel.add(area);
    this.add(sliderPanel, BorderLayout.PAGE_END);



}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>                        
// Variables declaration - do not modify                     
// End of variables declaration                   

public class MyChangeAction implements ChangeListener {

    //complete code here
    public void stateChanged(ChangeEvent ce) {
        int value = slider.getValue();
        String str = Integer.toString(value);
        sliderLabel.setText(str);


    }
    } // end class
    }

MyShape


    package assignment;

    public abstract class MyShape
 {

double thelength;
double thearea;



public abstract double computeBoundaryLength(double Length);

public abstract double computeArea (double Length);
 }

ASquare


    package assignment;

    public class ASquare extends MyShape {

@Override
public double computeBoundaryLength(double Length) 
{
    thelength=(4*Length);
    return thelength;
}

@Override
public double computeArea(double Length) 
{
    thearea=(Length*Length);
    return thearea;
}

}

class shapeAction implements ActionListener{
public void actionPerformed(ActionEvent e){

            JMenuItem clickedMenu = (JMenuItem)e.getSource();
            if (clickedMenu.getText().equals("Square")){
                 //implement abstract methods
            MyShape ASquare = new ASquare();  
            ASquare.addActionListener(new shapeAction());
            }
            else if (clickedMenu.getText().equals("Circle")){ 
                 //implement abstract methods
                MyShape ACircle = new ACircle();
            ACircle.addActionListener(new shapeAction());
            }
            else if (clickedMenu.getText().equals("Triangle")){ 
                 //implement abstract methods
                MyShape ATriangle = new ATriangle();
            ATriangle.addActionListener(new shapeAction());   
  • 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-06-13T05:45:28+00:00Added an answer on June 13, 2026 at 5:45 am
    • Your squareAction is meant to be the class that listens to all the possible menu selections (one for each kind of shape), so it shoud be named ShapeMenuAction or something like that. If you want ShapeMenuAction‘s actionPerformed to be executed when a menu option is selected, you have create one instance of it and add it to each MenuItem with addActionListener method on them. Otherwise, nothing will happen.

    In MyFrame:

    JMenuItem square = new JMenuItem("Square");
    square.addActionListener(new ShapeAction());
    JMenuItem circle = new JMenuItem("Circle");
    circle.addActionListener(new ShapeAction());
    JMenuItem triangle = new JMenuItem("Triangle");
    triangle.addActionListener(new ShapeAction());
    
    • Inside your actionPerformed you have to instantiate a concrete subclass of MyShape. Now you are doing nothing but instantiating a shape, you have to do something with the object before the method ends, right?. Create a setShape method in your MyControlPanel and call it from actionPerformed, passing the newly created shape to it.

    In ShapeAction:

        public void actionPerformed(ActionEvent e) {
           JMenuItem clickedMenu = (JMenuItem) e.getSource();
           if (clickedMenu.getText().equals("Square")) {
               MyShape aSquare = new ASquare();
               myControlPanel.setShape(aSquare);
           } else if (clickedMenu.getText().equals("Circle")) {
               MyShape aCircle = new ACircle();
               myControlPanel.setShape(aCircle);             
           } else if (clickedMenu.getText().equals("Triangle")) {                 
               MyShape aTriangle = new MyShape();  
               myControlPanel.setShape(aTriangle);           
           }         
        } 
    

    You have to create a MyControPanel named myControlPanel in MyFrame:

    private MyControlPanel myControlPanel
    

    and add it with

    myControlPanel = new MyControlPanel();
    getContentPane().add(myControlPanel);
    

    before

    pack();
    
    • In MyControlPanel now you can update your labels according to the slider value and the selected shape, everytime one of them changes.

      private MyShape shape;
      public void set Shape(MyShape shape) {
      this.shape = shape;
      updateLabels();
      }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for looking on this problem. I have a page that is totally valid
Thanks for looking at this question. I wanted to know how can I use
and thanks for looking at this question. I am porting a Linux (2.6.37) driver
Hi and thanks for looking at this newbi question. I have looked for anykind
thanks in advance for looking at this. Essentially, I'm calling didInsertElement() and binding an
Thanks for looking, all helpful answers are voted up. This is my markup. I'm
First of all this is a homework assignment so I'm looking for assistance, not
thank you for looking i got this example from my book but i cant
Thanks for looking. All sincerely helpful answers are voted up. I use a password
I'm looking for an open source javascript engine for .NET. Thanks.

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.