gpt4 book ai didi

Java Gui get.Text();不工作

转载 作者:行者123 更新时间:2023-12-02 04:44:52 24 4
gpt4 key购买 nike

我得到了下面的代码来获取文本,但我有一个问题。它给我一个错误,说它无法解决。我是 java gui 的新手。希望可以有人帮帮我。我尝试使用 nameP.getText();,但没有成功。我不确定为什么会出现错误?

 private class createListener implements ActionListener{
public void actionPerformed(ActionEvent event){

JFrame frame = new JFrame("Create Player");
JPanel panel = new JPanel();
JPanel mainpanel = new JPanel();
JButton create;
JLabel welcome = new JLabel("Create Player");
JLabel name = new JLabel("Enter Player Name");
JTextField nameP = new JTextField();
JLabel pass = new JLabel("Enter Player Password");
JTextField passwd = new JTextField();
JLabel chips = new JLabel("Enter Player Chips");
JTextField chipsP = new JTextField();
buttonCreate = new JButton("Create Player");

setSize(400,350);
setLocation(500,280);
panel.setLayout(new GridLayout(0,1,10,10));

panel.add(name);
panel.add(nameP);
panel.add(pass);
panel.add(passwd);
panel.add(chips);
panel.add(chipsP);
panel.add(buttonCreate);
mainpanel.add(panel);

getContentPane().removeAll();
getContentPane().add(mainpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
buttonCreate.addActionListener(new createListener());
}

}
private class playerListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String name = nameP.getText();

}
}

最佳答案

nameP 变量是本地,因此您无法在 actionPerformed 之外、该范围之外引用它nameP 不存在。您可以将其声明为 class member如果您想在其他地方使用它,则可以使用变量。

JTextField nameP;

private class createListener implements ActionListener{
public void actionPerformed(ActionEvent event){
nameP = new JTextField();
}
}

private class playerListener implements ActionListener{
public void actionPerformed(ActionEvent event){
String name = nameP.getText();
}

关于Java Gui get.Text();不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com