2014-12-31 41 views
3

所以,我有一個類,其中有一個單選按鈕。然後在第二節課中,我擴展了第一課,並創建了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 
     } 
     } 
    } 
+1

如果沒有RadioButton類的對象,不能訪問位移。你必須爲這個類創建一個對象,然後只有你可以訪問它。還要注意,public static void main()是當時程序的入口點,你的編譯器不知道任何關於位移的東西。 –

回答

2

displacement是您的構造函數中的局部變量。因此它不會在構造函數之外訪問。

如果你希望它是類中的其他方法訪問,將它移出構造,使之實例字段,說JRadioButton displacement;構造以上,在同一個地方,你聲明myListener。事實上,你已經用myListener做了正確的事情,所以你需要用displacement做同樣的事情。

這將使displacement可以通過RadioButton類中的其他方法訪問,但不能訪問RadioButtonMain等子類。爲了使其RadioButtonMain訪問,使現場protected

protected JRadioButton displacement; 

,或者可能會更好,使其private,並添加一個getter方法來RadioButton返回現場,因爲你可能不希望子類改變displacement任何時候他們都會喜歡它。

此外,請確保您在構造函數中更改此:

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)一起工作。請注意,在調用doMain之前將調用RadioButton構造函數,因爲構造函數設置爲displacement,所以這是您想要的。

+0

非常感謝您的辛勤工作! :d –

1

displacement不是一個全局變量來訪問。在method or constructor內您無法訪問。