我有麻烦添加一个actionlistener到我的JTextField。我需要从用户输入的文本到一个字符串,所以我可以使用它。GridBagLayout中的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();
}
});
...
文档监听器是否允许我将用户输入存储在变量中?如何? –