简体   繁体   中英

Cannot Access Variable From Another Class

So, I have one class, with a radio button set up in it. Then in a second class, I extended the first class and made 3 "if" statements that will create an applet depending on the output of the radio button. In those "if" statements, it says that the variables cannot be resolved. How do I get these resolved? And please tell me if there are there any other errors in my code. Thanks a million, :D.

Thanks, any help will help greatly.

//  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 the 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
        }
        }
    }

displacement is a local variable in your constructor. Therefore it won't be accessible outside the constructor.

If you want it to be accessible to other methods in the class, move it out of the constructor and make it an instance field, by saying 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 thing with displacement .

This will make displacement accessible to other methods in the RadioButton class, but not to subclasses like RadioButtonMain . To make it accessible to RadioButtonMain , make the field protected :

protected JRadioButton displacement;

or, probably better, make it private and add a getter method to RadioButton to return the field, since you probably don't want subclasses to change displacement any time they feel like it.

Also, make sure you change this in your constructor:

JRadioButton displacement = new JRadioButton("Displacement");

to this:

displacement = new JRadioButton("Displacement");

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

Finally, note that the main method is static. So even though it's defined inside RadioButtonMain , it won't have access to any fields of RadioButtonMain , including displacement . Make 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 a RadioButtonMain (which is also a RadioButton ) to work with. Note that the RadioButton constructor will be called before doMain is called, which is what you want because the constructor sets up displacement .

displacement is not a global variable to access. within the method or constructor you cant access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM