【发布时间】:2012-06-06 05:05:48
【问题描述】:
我在向我的 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();
}
});
...
【问题讨论】:
标签: java swing actionlistener jtextfield gridbaglayout