0

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();
                }
            });

...
4

2 に答える 2

1
public void actionPerformed(ActionEvent e)
{
    //  Look Ma, a one-liner!
    String chance1 = JOptionPane.showInputDialog(someComponent, "Value Bet");
}

オプション ペインのワンライナー

オーバーロードされたメソッドをさらに調べて外観を微調整し、堅牢性のためにnullチェックを追加し、テキスト フィールド (BNI) の代わりにスピナーを使用することを検討してください。

于 2012-06-01T07:15:22.463 に答える
1

ActionListenerの代わりになぜ使用しているのKeyListenerですか?

KeyListenerまたは以下を使用する必要があります。

tf.getDocument().addDocumentListener(documentListener);

ドキュメントリスナー

于 2012-05-31T22:29:29.513 に答える