2015-12-22 45 views
-1

我想弄清楚如何訪問我的其他JFrame字段來創建一個對象onclick。這是我的代碼。Java獲取textField.getText()onclick

我在網上搜索和所有我能找到的是人引用textField.getText()在他們的onclick,但我編譯時

java:114:error: cannot find symbol.

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

public class CharacterCreator extends JFrame{ 

    public CharacterCreator(){ 
     final int WINDOW_WIDTH = 600; 
     final int WINDOW_HEIGHT = 400; 

     JFrame window = new JFrame(); 

     window.setTitle("Create Character"); 
     window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setLayout(new BorderLayout()); 

     JPanel panel1 = new JPanel(); 
     JPanel panel2 = new JPanel(); 
     JPanel panel3 = new JPanel(); 
     JPanel panel4 = new JPanel(); 
     JPanel panel5 = new JPanel(); 

     //panel 1 
     JRadioButton radio1 = new JRadioButton("Human", true); 
     JRadioButton radio2 = new JRadioButton("Elf"); 
     JRadioButton radio3 = new JRadioButton("Dwarf"); 

     ButtonGroup raceGroup = new ButtonGroup(); 
     raceGroup.add(radio1); 
     raceGroup.add(radio2); 
     raceGroup.add(radio3); 

     panel1.setLayout(new GridLayout(3,1)); 
     panel1.add(radio1); 
     panel1.add(radio2); 
     panel1.add(radio3); 


     //panel 2 
     JLabel nameLabel = new JLabel("Name: "); 
     JTextField nameTextField = new JTextField(15); 

     panel2.add(nameLabel); 
     panel2.add(nameTextField); 

     //panel 3 
     JRadioButton radio4 = new JRadioButton("Male", true); 
     JRadioButton radio5 = new JRadioButton("Female"); 

     ButtonGroup genderGroup = new ButtonGroup(); 
     genderGroup.add(radio4); 
     genderGroup.add(radio5); 

     panel3.setLayout(new GridLayout(2,1)); 
     panel3.add(radio4); 
     panel3.add(radio5); 

     //panel 4 
     JRadioButton radioWarrior = new JRadioButton("Warrior", true); 
     JLabel warriorLabel1 = new JLabel(" The warrior is a battle focused character,"); 
     JLabel warriorLabel2 = new JLabel("they focuses on strength, defense, and battle tactics."); 
     JRadioButton radioThief = new JRadioButton("Thief"); 
     JLabel thiefLabel1 = new JLabel(" The thief is a stealth focused character,"); 
     JLabel thiefLabel2 = new JLabel("they focus on agility, cunning, and hope on luck."); 
     JRadioButton radioMage = new JRadioButton("Mage"); 
     JLabel mageLabel1 = new JLabel(" The mage is a magic focused character,"); 
     JLabel mageLabel2 = new JLabel("they focus on intellegense, wisdom, and mage tools."); 

     ButtonGroup professionGroup = new ButtonGroup(); 
     professionGroup.add(radioWarrior); 
     professionGroup.add(radioThief); 
     professionGroup.add(radioMage); 

     panel4.setLayout(new GridLayout(9,1)); 
     panel4.add(radioWarrior); 
     panel4.add(warriorLabel1); 
     panel4.add(warriorLabel2); 
     panel4.add(radioThief); 
     panel4.add(thiefLabel1); 
     panel4.add(thiefLabel2); 
     panel4.add(radioMage); 
     panel4.add(mageLabel1); 
     panel4.add(mageLabel2); 

     //panel 5  
     JButton btnCreate = new JButton("Create");  
     btnCreate.addActionListener(new ButtonListener()); 
     panel5.add(btnCreate); 

     //add panels 
     window.add(panel1, BorderLayout.WEST); 
     window.add(panel2, BorderLayout.NORTH); 
     window.add(panel3, BorderLayout.EAST); 
     window.add(panel4, BorderLayout.CENTER); 
     window.add(panel5, BorderLayout.SOUTH); 

     window.pack(); 
     window.setVisible(true); 

     /*character cha = new character();  
     try{ 

     }catch(Exception e){ 

     }*/ 
    } 

    private class ButtonListener implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      //String s = nameTextField.getText(); 
      //character cha = new character(); 
      //cha.SetName(nameTextField.getText()); 
      if(radio1==true){ 
       JOptionPane.showMessageDialog(null, "Warrior"); 
      } 
     } 
    } 

    public static void main(String[] args){ 
     new CharacterCreator(); 
    } 
} 
+2

您的radio1變量在構造函數中聲明,即在方法中聲明。怎麼樣,一個方法可以訪問在另一個方法中聲明的變量。 –

+0

'if(radio1 == true)'?這是不正確的,除非radio1是一個布爾值。它看起來像是一個單選按鈕... –

+0

請創建您的TextField變量作爲CharacterCreater類的屬性或傳遞給Listener類的構造函數。否則,如何訪問其他方法內的一個方法中聲明的變量。 –

回答

-1

不同階層,不同的範圍。基本上nameTextField不存在於你的類ButtonListener中。將它作爲參數傳入。

private class ButtonListener implements ActionListener{ 
     TextField nameField; 
     public ButtonListener(TextField nameField){ 
      this.nameField = nameField; 
     } 

     public void actionPerformed(ActionEvent e){ 
      String s = nameField.getText(); 
      // more action 
     } 
} 

當您添加的ActionListener,

//... 
btnCreate.addActionListener(new ButtonListener(nameTextField)); 

還需要在RadioGroup通過爲您試圖訪問其屬性爲好。

+0

不正確。該類是一個內部類,它屬於外部類的範圍,並且可以訪問任何範圍爲外部類的東西。在這種情況下,'TextField'的作用域是該方法,而不是類。 – njzk2

+0

@ njzk2 - 將此答案中的類替換爲方法,同樣的概念恰好適用於我的要點 –

+0

。在目前的形式下,你的回答是錯誤的。 – njzk2