Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从其他类访问变量_Java_Variables_Applet - Fatal编程技术网

Java 无法从其他类访问变量

Java 无法从其他类访问变量,java,variables,applet,Java,Variables,Applet,所以,我有一节课,里面有一个单选按钮。然后在第二个类中,我扩展了第一个类,并做了3个“if”语句,根据单选按钮的输出创建一个小程序。在那些“如果”语句中,它表示变量无法解析。如何解决这些问题?请告诉我我的代码中是否还有其他错误。非常感谢,:D 谢谢,任何帮助都会大有帮助 // The First Class Code: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Radio

所以,我有一节课,里面有一个单选按钮。然后在第二个类中,我扩展了第一个类,并做了3个“if”语句,根据单选按钮的输出创建一个小程序。在那些“如果”语句中,它表示变量无法解析。如何解决这些问题?请告诉我我的代码中是否还有其他错误。非常感谢,:D

谢谢,任何帮助都会大有帮助

//  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);
   }
} 
我的第二个类,带有if语句

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

位移
不是可访问的全局变量。在
方法或构造函数中
无法访问。

置换
是构造函数中的局部变量。因此,在构造函数外部无法访问它

如果您希望类中的其他方法可以访问它,请将其移出构造函数,并通过说
JRadioButton displacement使其成为实例字段
位于构造函数上方,与声明myListener的位置相同。事实上,您已经用
myListener
做了正确的事情,所以您需要用
置换
做同样的事情

这将使
RadioButton
类中的其他方法可以访问
displacement
,但不能访问像
RadioButtonMain
这样的子类。要使
RadioButtonMain
可以访问该字段,请将该字段设置为受保护的:

protected JRadioButton displacement;
或者,可能更好的方法是将其设置为
私有
,并向
RadioButton
添加一个getter方法以返回字段,因为您可能不希望子类在任何时候更改
置换

此外,请确保在构造函数中更改此选项:

JRadioButton displacement = new JRadioButton("Displacement");
为此:

displacement = new JRadioButton("Displacement");
这样就不会有与字段同名的局部变量

最后,请注意
main
方法是静态的。因此,即使它是在
RadioButtonMain
中定义的,它也无法访问
RadioButtonMain
的任何字段,包括
displacement
。让它像这样:

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

这将为您提供一个要使用的
RadioButtonMain
(它也是
RadioButton
)。请注意,
RadioButton
构造函数将在调用
doMain
之前被调用,这是您想要的,因为构造函数设置了
displacement

如果没有类RadioButton的对象,则无法访问displacement。您必须为该类创建一个对象,然后只有您才能访问它。还要注意的是,publicstaticvoidmain()是程序的入口点,此时编译器对位移一无所知。