我想弄清楚如何訪問我的其他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類的構造函數。否則,如何訪問其他方法內的一個方法中聲明的變量。 –