Can't access variable from another class

So I have one class that has a radio button set. Then in the second grade, I expanded on the first class and made 3 "ifs" which will create an applet based on the output of the switch. These "if" statements say that the variables cannot be resolved. How can I solve them? And please tell me if there are any errors in my code. Thanks a million, D.

Thanks, any help will help a lot.

//  The First Class Code:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RadioButton extends JPanel {

    static JFrame frame;

   JLabel pic;
   RadioListener myListener = null;
   public RadioButton() {



       // Create the radio buttons
       JRadioButton displacement = new JRadioButton("Displacement");
       displacement.setMnemonic(KeyEvent.VK_N);
       displacement.setSelected(true);
        //Displacement Button, set to automatically be clicked

       JRadioButton accel = new JRadioButton("Acceleration");
       accel.setMnemonic(KeyEvent.VK_A);
       accel.setActionCommand("acceleration");
        //Acceleration Button

       JRadioButton time = new JRadioButton("Change in time");
       time.setMnemonic(KeyEvent.VK_S);
       time.setActionCommand("deltaT");
        //The change in time button


       // Creates the group of buttons
       ButtonGroup group = new ButtonGroup();
       group.add(displacement);
       group.add(accel);
       group.add(time);

              myListener = new RadioListener();
                displacement.addActionListener(myListener);
                accel.addActionListener(myListener);
                time.addActionListener(myListener);


      // Set up the picture label
       pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg"));          //Set the Default Image

       pic.setPreferredSize(new Dimension(177, 122)); 


       // Puts the radio buttons down
       JPanel panel = new JPanel();
       panel.setLayout(new GridLayout(0, 1));
       panel.add(displacement);
       panel.add(accel);
       panel.add(time);


       setLayout(new BorderLayout());
       add(panel, BorderLayout.WEST);
       add(pic, BorderLayout.CENTER);
       setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
   } 



   //Listening to the buttons
   class RadioListener implements ActionListener { 
       public void actionPerformed(ActionEvent e) {
           pic.setIcon(new ImageIcon(""+e.getActionCommand() 
                                         + ".jpg"));
       }
   }

   public static void main(String s[]) {
        frame = new JFrame("∆x = Vavg * time");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        frame.getContentPane().add(new RadioButton(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
   }
} 

      

My second class, with if statements

    import java.lang.Object;
    import java.awt.Graphics;


    public class RadioButtonMain extends RadioButton {

        public static void main(String [ ] args) {
        if ( displacement.isSelected())
          {
    //Option 1 for applet
          }

        if ( accel.isSelected()) {
            //Option 2 for applet
        }

        else {
            //Option 3 for applet
        }
        }
    }

      

+3


source to share


2 answers


displacement

is a local variable in your constructor. Therefore, it will not be available outside of the constructor.

If you want it to be available to other methods in the class, take it out of the constructor and make it an instance field by pointing JRadioButton displacement;

above the constructor in the same place where you declare myListener

. In fact, you've already done the right thing with myListener

, so you need to do the same with displacement

.

This will make it displacement

available to other methods in the class RadioButton

, but not to subclasses such as RadioButtonMain

. To make it available for RadioButtonMain

, enter the field protected

:

protected JRadioButton displacement;

      

or perhaps better, do it private

and add a getter method in RadioButton

to get the field back as you probably don't want the subclasses to change displacement

anytime they want.

Also, make sure you change this in your constructor:

JRadioButton displacement = new JRadioButton("Displacement");

      



:

displacement = new JRadioButton("Displacement");

      

so that you don't have a local variable with the same name as the field.

Finally, note that the method main

is static. Therefore, although it is defined internally RadioButtonMain

, it will not have access to any fields RadioButtonMain

including displacement

. Do it something like this:

 public static void main(String [ ] args) {
     new RadioButtonMain().doMain();
 }

 public void doMain() {
    if ( displacement.isSelected())
      {
//Option 1 for applet
      }

    if ( accel.isSelected()) {
        //Option 2 for applet
    }

    else {
        //Option 3 for applet
    }
    }
}

      

This gives you RadioButtonMain

(which is also RadioButton

) to work with. Note that the constructor RadioButton

will be called before being called doMain

, which is what you need, because the constructor sets displacement

.

+2


source


displacement

is not a global variable to access. within method or constructor

you cannot access.



+1


source







All Articles