我在向我的 JTextField 添加 actionlistener 时遇到了麻烦。我需要将用户输入的文本转换为字符串,以便我可以使用它。
谁能告诉我我做错了什么或者我应该怎么做。
这是代码:
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
JFrame frame = new JFrame("Value Bet");
frame.setVisible(true);
frame.setSize(500,300);
GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
JLabel label;
JTextField tf;
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
...
tf = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
frame.add(tf, c);
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String chance1 = tf.getText();
}
});
...