java - 无法从另一个类访问变量

标签 java variables applet

所以,我有一个类,其中设置了一个单选按钮。然后在第二堂课中,我扩展了第一个类并制作了 3 个“if”语句,这些语句将根据单选按钮的输出创建一个小程序。在那些“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
        }
        }
    }

最佳答案

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

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

这将使 displacement 可以被 RadioButton 类中的其他方法访问,但不能被 RadioButtonMain 之类的子类访问。要使其可供 RadioButtonMain 访问,请将字段设为 protected:

protected JRadioButton displacement;

或者,可能更好,让它成为 private 并向 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

关于java - 无法从另一个类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27716672/

相关文章:

java - 分布式 Java 桌面应用程序 mySQL 数据库

java - 字符串和最终

r - R 函数中的变量范围

java - 使 java applet 符合高安全标准

java - Applet:XML 找不到 DTD

java - 抛出异常会改变它的状态吗?

使用 ICU4j 的 Java 僧伽罗语言环境日期格式不起作用

Java FileSystemView 隐藏文件夹

java - 尝试将字符串更改为 altcase

python - 如何在python中引用类变量