Java getting textField.getText() onclick



我正试图弄清楚如何访问我的其他JFrame字段来创建对象onclick。这是我的密码。

我在互联网上搜索过,我能找到的只是人们在onclick中引用textField.getText(),但当我编译时

java:114:错误:找不到符号。

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();
    }
}

不同的类,不同的范围。基本上,类ButtonListener中不存在nameTextField。将其作为参数传入。

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

最新更新