我想弄清楚如何访问我的其他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();
}
}
您的radio1变量在构造函数中声明,即在方法中声明。怎么样,一个方法可以访问在另一个方法中声明的变量。 –
'if(radio1 == true)'?这是不正确的,除非radio1是一个布尔值。它看起来像是一个单选按钮... –
请创建您的TextField变量作为CharacterCreater类的属性或传递给Listener类的构造函数。否则,如何访问其他方法内的一个方法中声明的变量。 –